Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why curl is failing in my PHP script for posting to Twitter? [duplicate]

Tags:

php

curl

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.

like image 867
Questioner Avatar asked Mar 12 '23 22:03

Questioner


1 Answers

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.
like image 105
blue112 Avatar answered Mar 29 '23 23:03

blue112