[英]Why curl is failing in my PHP script for posting to Twitter?
我有一个使用Oauth发布到Twitter的PHP脚本。 每当使用它时,都会出现以下错误:
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
我在Stack Overflow上进行了搜索,以查看是否有人已经解决了这个问题,而我所看到的所有答案(例如此处和此处 )仅表示我需要安装curl和php5-curl。 但是,我已经安装了curl和php5-curl:
# 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
另一个答案( 在这里 )说,我需要通过确保没有将引用curl的行注释掉来确保ini
文件中启用curl。 看来我确实启用了curl并没有注释掉:
# grep -n 'extension=curl.so' /etc/php5/conf.d/curl.ini
2:extension=curl.so
据我所知,我已经准备好一切,那为什么卷曲对我来说却失败了?
更新:我想知道这是否有影响-默认情况下,我的服务器运行的是PHP 5.3,但我需要PHP 5.5。 我使用了apt-add-repository ppa:ondrej/php
,然后将其升级到PHP 5.5。 升级PHP时是否可能丢失了curl配置?
更新2:我尝试将这段代码放在服务器上:
echo 'Curl: ', function_exists('curl_init') ? 'Enabled' : 'Disabled';
...当我运行它时,得到以下输出:
卷曲:启用
似乎Curl可能已激活并在我的服务器上运行,并且问题更特定于Twitter Oauth代码。 Twitter的大多数代码来自在线资源-这不是我的-因此我不确定该如何解决。
更新3:我有一个本地测试服务器,并且PHP脚本运行正常。 因此,当前情况是PHP代码在一台服务器上运行,而不在另一台服务器上运行,但是两台服务器都报告Curl已安装。 那么,一台服务器如何能够安装curl而不运行该脚本,而另一台服务器却不能呢?
更新4:根据我在其他站点上看到的信息,我尝试如下安装更多相关的库:
apt-get install curl libcurl3 libcurl3-dev php5-curl php-curl
不幸的是,这并没有解决问题。
该扩展程序似乎未加载。
你需要做什么:
php --info | grep -i curl
检查CLI是否知道扩展名php --info | grep -i curl
php --info | grep -i curl
。 php -m
还将向您显示php-cli知道的模块。
php --info
检查加载了哪个配置文件 <?php phpinfo(); ?>
检查Web应用是否知道它<?php phpinfo(); ?>
<?php phpinfo(); ?>
,并搜索“ CURL”
service apache2 restart
或/etc/init.d/apache2 restart
重启网络服务器。 确实取决于您的发行版/ Web服务器。 似乎仍然没有卷曲。
php -m
,看看curl
弹出。 以下代码:
if (!extension_loaded('curl')) {
die('Curl not loaded');
}
如果确实如此,请检查phpinfo中php.ini的路径
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.