5

I have a PHP script that posts to Twitter using Oauth. Whenever I use it, I get the following errors:

PHP Notice: Use of undefined constant CURLOPT_CAINFO - assumed 'CURLOPT_CAINFO' in /var/www/example.com/myWebApp/vendor/abraham/twitteroauth/src/TwitterOAuth.php on line 353
PHP Notice: Use of undefined constant CURLOPT_CONNECTTIMEOUT - assumed 'CURLOPT_CONNECTTIMEOUT' in /var/www/example.com/myWebApp/vendor/abraham/twitteroauth/src/TwitterOAuth.php on line 354
PHP Notice: Use of undefined constant CURLOPT_HEADER - assumed 'CURLOPT_HEADER' in /var/www/example.com/myWebApp/vendor/abraham/twitteroauth/src/TwitterOAuth.php on line 355
PHP Notice: Use of undefined constant CURLOPT_HTTPHEADER - assumed 'CURLOPT_HTTPHEADER' in /var/www/example.com/myWebApp/vendor/abraham/twitteroauth/src/TwitterOAuth.php on line 356
PHP Notice: Use of undefined constant CURLOPT_RETURNTRANSFER - assumed 'CURLOPT_RETURNTRANSFER' in /var/www/example.com/myWebApp/vendor/abraham/twitteroauth/src/TwitterOAuth.php on line 357
PHP Notice: Use of undefined constant CURLOPT_SSL_VERIFYHOST - assumed 'CURLOPT_SSL_VERIFYHOST' in /var/www/example.com/myWebApp/vendor/abraham/twitteroauth/src/TwitterOAuth.php on line 358
PHP Notice: Use of undefined constant CURLOPT_SSL_VERIFYPEER - assumed 'CURLOPT_SSL_VERIFYPEER' in /var/www/example.com/myWebApp/vendor/abraham/twitteroauth/src/TwitterOAuth.php on line 359
PHP Notice: Use of undefined constant CURLOPT_TIMEOUT - assumed 'CURLOPT_TIMEOUT' in /var/www/example.com/myWebApp/vendor/abraham/twitteroauth/src/TwitterOAuth.php on line 360
PHP Notice: Use of undefined constant CURLOPT_URL - assumed 'CURLOPT_URL' in /var/www/example.com/myWebApp/vendor/abraham/twitteroauth/src/TwitterOAuth.php on line 361
PHP Notice: Use of undefined constant CURLOPT_USERAGENT - assumed 'CURLOPT_USERAGENT' in /var/www/example.com/myWebApp/vendor/abraham/twitteroauth/src/TwitterOAuth.php on line 362
PHP Notice: Use of undefined constant CURLOPT_ENCODING - assumed 'CURLOPT_ENCODING' in /var/www/example.com/myWebApp/vendor/abraham/twitteroauth/src/TwitterOAuth.php on line 366
PHP Notice: Use of undefined constant CURLOPT_POST - assumed 'CURLOPT_POST' in /var/www/example.com/myWebApp/vendor/abraham/twitteroauth/src/TwitterOAuth.php on line 381
PHP Notice: Use of undefined constant CURLOPT_POSTFIELDS - assumed 'CURLOPT_POSTFIELDS' in /var/www/example.com/myWebApp/vendor/abraham/twitteroauth/src/TwitterOAuth.php on line 382
PHP Fatal error: Call to undefined function Abraham\TwitterOAuth\curl_init() in /var/www/example.com/myWebApp/vendor/abraham/twitteroauth/src/TwitterOAuth.php on line 397

I searched here on Stack Overflow to see if anyone had already addressed this problem, and all the answers I have seen (here, and here, for example) only say that I need to install curl and php5-curl. However, I already have curl and php5-curl installed:

# apt-get install php5-curl
Reading package lists... Done
Building dependency tree       
Reading state information... Done
php5-curl is already the newest version.
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
# apt-get install curl
Reading package lists... Done
Building dependency tree       
Reading state information... Done
curl is already the newest version.
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
# php -v
PHP 5.5.36-4+deb.sury.org~precise+1 (cli) 
Copyright (c) 1997-2015 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2015 Zend Technologies
    with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2015, by Zend Technologies

Another answer (here) said I need to ensure curl is enabled in my ini files by making sure the lines referencing curl are not commented out. It seems I do have curl enabled and not commented out:

# grep -n 'extension=curl.so' /etc/php5/conf.d/curl.ini
2:extension=curl.so

As far as I can tell I have everything I need in place, so why is curl failing for me?

Update: I wonder if this has an impact - my server was running PHP 5.3 by default, but I needed PHP 5.5. I used the command apt-add-repository ppa:ondrej/php and then upgraded to PHP 5.5. Is it possible that when I upgraded PHP I lost some curl configuration?

Update 2: I tried putting this code on my server:

echo 'Curl: ', function_exists('curl_init') ? 'Enabled' : 'Disabled';

... and when I run it, I get this output:

Curl: Enabled

It seems that Curl might be activated and working on my server, and the problem is more specific to the Twitter Oauth code. Most of that Twitter code came from onlines sources - it's not mine - so I'm not sure what I can do to address it.

Update 3: I have a local testing server, and the PHP script runs fine. So, the current situation is that the PHP code runs on one server but not the other, but both servers report Curl is installed. So how is one server able to have curl installed and not run this script, but the other sever can't?

Update 4: Based on information I saw on other sites, I tried installing more relevant libraries as follows:

apt-get install curl libcurl3 libcurl3-dev php5-curl php-curl

Unfortunately this did not solve the problem.

Community
  • 1
  • 1
Questioner
  • 7,133
  • 16
  • 61
  • 94
  • 1
    Are you sure you have the right `ini` file? The error is what it is, _curl_ is not enabled when that code runs! – bzeaman Jun 22 '16 at 08:28
  • Can you use curl functions in a simple one-line test script? – Álvaro González Jun 22 '16 at 08:36
  • Once you run that one-liner to determine whether `curl_init()` actually exist—if it actually exists, you should find out and share what ` Abraham\TwitterOAuth\curl_init()` is exactly. – Álvaro González Jun 23 '16 at 08:57
  • @miken32, how on earth is this question about a specific server circumstance in any way a duplicate of a whole list of general PHP error outputs? Note especially the updates. – Questioner Jun 30 '16 at 05:36
  • "PHP Fatal error: Call to undefined function" is your problem. There is nothing unique about that error. It means you're trying to call a function that doesn't exist, as the first comment pointed out. – miken32 Jun 30 '16 at 05:41
  • @miken32, you're completely ignoring the fact that the function does seem to exist when I test it in other scripts. Also that php5-curl is installed. You're just trying to dismiss the question expediently without actually looking at the particulars. Tell me... if it's just that the function does not exist as you say, what should I do that I haven't already tried? – Questioner Jun 30 '16 at 06:01
  • @miken32, I updated my test PHP script to look specifcally for the function that the error claims is missing, and the test indicates that the function is present. So, where in the link you provided can I find out why I can get an error that says a function is missing even though it seems to be present when I test it? – Questioner Jun 30 '16 at 06:08

2 Answers2

5

The extension doesn't seem to be loaded.

What you need to do:

  • Check that the CLI knows the extension, using php --info | grep -i curl. php -m will also shows you the modules php-cli knows.
    • If it doesn't, check which configuration file are loaded using php --info
  • Check that the web app knows about it, using <?php phpinfo(); ?>, and searching for "CURL"
    • If it doesn't, restart your webserver, using, for instance service apache2 restart or /etc/init.d/apache2 restart. Really depends of your distribution / web server.
blue112
  • 52,634
  • 3
  • 45
  • 54
  • Tank you for responding. The command `php --info | grep -i curl` returns nothing. The command `php --info` returns a lot of output, so I don't know what to search for. – Questioner Jun 23 '16 at 09:05
  • @Questioner You have to search for "curl". – Álvaro González Jun 23 '16 at 14:07
  • But doesn't the command `php --info | grep -i curl` already search for "curl"? If that command doesn't return any results, then can I expect to fund "curl" by running `php --info` and searching some other way? – Questioner Jun 25 '16 at 07:44
4

It still seems curl isn't loaded.

  • Try running php -m in console and see if curl pops out.
  • In your app before running any curl code try adding:

following code:

if (!extension_loaded('curl')) {
    die('Curl not loaded'); 
}

If it is indeed the case check the path of php.ini in phpinfo

Ruslan Bes
  • 2,715
  • 2
  • 25
  • 32