6

So right now, i am attempting to enable php-curl within my apache2 server on Ubuntu 18.04 to allow an iframe to display an external site page. i have been using methods to attempt this that i have found documented in several places:

StackOverflow: How do I install the ext-curl extension with PHP 7?

StackOverflow: install cURL in php 7 (ubuntu14.04)

LinuxConfig.org: How to enable and disable PHP curl module with Apache on Ubuntu Linux

No matter what i seem to do, i cannot get anything sort of curl-related commands to work within php, which is very frustrating. i have ensured that i have used sudo apt-get install curl php7.2-curl which installed without issue, and have then restarted the apache service using sudo service apache2 restart. I have tried to enable the extension in the php.ini using extension=php_curl.dll, and also extension=curl, with no luck. If i try the code given on linuxconfig.org to check the curl module state, it says its disabled.

If i try running my php code, i find in my logs:

PHP Fatal error: Uncaught Error: Call to undefined function curl_init() in /var/www/html/inc.redirect.php:4\nStack trace:\n#0 {main}\n thrown in /var/www/html/inc.redirect.php on line 4

The code in my 'inc.redirect.php' file is as follows:

<?php
if (isset($_GET['url'])) {
    $url = $_GET['url'];
    $ch = curl_init();
    $timeout = 10;
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    $data = curl_exec($ch);
    curl_close($ch);
    echo $data;
}
?>

What am i doing wrong/missing?

UPDATE: looking in the apache2 error.log when i restart the service, i see the following:

PHP Warning: PHP Startup: Unable to load dynamic library '/usr/lib/php/20160303/curl.so' - /usr/lib/php/20160303/curl.so: cannot open shared object file: No such file or directory in Unknown on line 0

Still attempting to dig more into this, and it appears that the curl.so file its looking for is located in '/usr/lib/php/20170718'. What do i have to do to change the php config to look in the proper directory?

2 Answers 2

8

Found the issue.

What was happening was I had both php7.1 and php7.2 enabled within apache2. had to run sudo a2dismod php7.1, restart apache, and afterwards I was able to load my inc.redirect.php page without errors.

1
  • 1
    Awesome. Wow. Been searching for this one for hours. Working now. My problem was proxied images in Rainloop webmail not showing up on a particular server. Now they are.. however a couple still do not show and if I 'Open Image in a New Tab' I get a 404 error... but I think that is another issue, probably not one having to do with this one. This problem arose after a system upgrade to next version of Ubuntu. Basically I had to a2dismod php7.0 and a2enmod php7.2 (the latter said it was already enabled though) Thank you!
    – jdmayfield
    Jun 26, 2019 at 9:59
1

Thanks MathewH, I was strugging for last 2 days but was not able to locate the error, as curl module was installed, php-curl extension was also installed, I was able to run the curl for a url in terminal, but when I run the curl_init() function in a PHP code, it would always show 500 internal server error,

Though I have installed the curl module by following commands:

sudo apt-get install php7.0-curl
sudo /etc/init.d/apache2 restart

as I have php 7.4.6 installed in my system

Then I checked the Apache2 error logs in /var/log/apache2/error_log and I found that curl_init() shows error

Call to undefined function curl_init()

Then searching on google I find the link: PHP: Check if cURL is enabled. which mentioned these 3 codes to check whether curl is loaded or not:

  1. if(function_exists('curl_init') === false)
    {
    
     //curl_init is not defined
        //cURL not enabled
    
    }
    
  2. //Check if "curl" can be found in the array of loaded extensions.
    
    if(in_array('curl', get_loaded_extensions()))
    {
        //cURL module has been loaded
    } 
    else
    {
        //It has not been loaded. Use a fallback.
    }
    
  3. if(extension_loaded('curl'))
    {
        //the extension has been loaded
    }
    

I have added these 3 code in index.php and named the project folder as curl-load-check. After running I found and got confirmed that the curl extension is not loaded.

Then finally I found this link: Cannot enable php-curl on Ubuntu 18.04 & PHP 7.2 and from the answer of MathewH, I found that php 7.2 and php 7.4 were both enabled in apache2. so I disabled php 7.2 by the following commands:

sudo a2dismod php7.2
sudo systemctl restart apache2 

and then finally I checked the php code for checking whether the curl extension is loaded as mentioned above in php codes, it showed finally the curl extension is loaded successfully. I checked now the curl_init() function used in php code is working now without any error. Thanks MathewH, after a lot of struggle, finally you saved my day

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.