简体   繁体   English

PHP cURL 在本地工作,AWS 服务器上出现错误 77

[英]PHP cURL working locally, error 77 on AWS server

LATEST UPDATE: script is running successfully via SSH shell as "php script.php", being an admin user.最新更新:脚本通过 SSH shell 作为“php script.php”成功运行,是管理员用户。 When run by the nginx user, the curl command fails to do the https request.当 nginx 用户运行时,curl 命令无法执行 https 请求。 So I guess it's an issue of nginx user not being able to use curl properly.所以我想这是 nginx 用户无法正确使用 curl 的问题。 I've checked tenths of configuration files and cannot find where this could be changed.我检查了十分之一的配置文件,但找不到可以更改的地方。

I'm trying to post the following JSON我正在尝试发布以下 JSON

{
    "where": {
      "userId": "97"
    },
    "data": {
      "alert": "Joe Masilotti answered a question you follow",
      "badge": "Increment","eventId":"3","questionId":"8954","answerId":"977"
    }
}

with the following code使用以下代码

$c = curl_init();
curl_setopt($c, CURLOPT_TIMEOUT, 30);
curl_setopt($c, CURLOPT_USERAGENT, 'parse.com-php-library/2.0');
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
curl_setopt($c, CURLINFO_HEADER_OUT, true);

curl_setopt($c, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json',
    'X-Parse-Application-Id: '.$this->_appid,
    'X-Parse-REST-API-Key: '.$this->_restkey
)); 
curl_setopt($c, CURLOPT_CUSTOMREQUEST, 'POST');

$postData = str_replace(PHP_EOL,'',$args['data']);
curl_setopt($c, CURLOPT_POSTFIELDS, $postData );
$url = $this->_parseurl . $args['requestUrl'];
curl_setopt($c, CURLOPT_URL, $url);
curl_setopt($c, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($c);

It works on my local Apache.它适用于我的本地 Apache。 I run this on an nginx server running on AWS EC2 and getting this from cURL我在 AWS EC2 上运行的 nginx 服务器上运行它,并从 cURL 获取它

Array
(
    [url] => https://api.parse.com/1/push
    [content_type] => 
    [http_code] => 0
    [header_size] => 0
    [request_size] => 0
    [filetime] => -1
    [ssl_verify_result] => 0
    [redirect_count] => 0
    [total_time] => 0.012399
    [namelookup_time] => 0.01232
    [connect_time] => 0.013486
    [pretransfer_time] => 0
    [size_upload] => 0
    [size_download] => 0
    [speed_download] => 0
    [speed_upload] => 0
    [download_content_length] => -1
    [upload_content_length] => -1
    [starttransfer_time] => 0
    [redirect_time] => 0
    [certinfo] => Array
        (
        )

    [redirect_url] => 
)


cURL error number:77

cURL error:

The same JSON is sent OK from the command line of the AWS EC2 server with the following command使用以下命令从 AWS EC2 服务器的命令行发送相同的 JSON

curl -X POST \
  -H "X-Parse-Application-Id: <...>" \
  -H "X-Parse-REST-API-Key:<..>" \
  -H "Content-Type: application/json" \
  -d '{
        "where": {
          "userId": "97"
        },
        "data": {
          "alert": "Joe answered a question",
          "badge": "Increment","eventId":"3","questionId":"8954","answerId":"977"
        }
      }' \
  https://api.parse.com/1/push

cURL extension is set up properly on the server, and it works in other scripts. cURL 扩展已在服务器上正确设置,并且可以在其他脚本中使用。 Thanks!谢谢!

UPDATE: The script "script.php" runs successfully when run on server from command line as "php script.php", while failing when requested on a browser at " http://www.mysite.com/script.php "更新:脚本“script.php”在从命令行作为“php script.php”在服务器上运行时成功运行,而在“ http://www.mysite.com/script.php ”的浏览器上请求时失败

UPDATE: Here's my curl -V更新:这是我的 curl -V

curl 7.36.0 (x86_64-redhat-linux-gnu) libcurl/7.36.0 NSS/3.16 Basic ECC zlib/1.2.7 libidn/1.18 libssh2/1.4.2
Protocols: dict file ftp ftps gopher http https imap imaps ldap ldaps pop3 pop3s rtsp scp sftp smtp smtps telnet tftp 
Features: AsynchDNS GSS-Negotiate IDN IPv6 Largefile NTLM NTLM_WB SSL libz

and I was trying to check curl differences between my local and server PHP configs, I saw that local curl SSL version is OpenSSL/1.0.1f while on server is NSS/3.14.3.0 and I was trying to check curl differences between my local and server PHP configs, I saw that local curl SSL version is OpenSSL/1.0.1f while on server is NSS/3.14.3.0

I have the same error on amazon AMI linux. 我在亚马逊AMI linux上有同样的错误。

I Solved by setting curl.cainfo on /etc/php.d/curl.ini 我通过在/etc/php.d/curl.ini上设置curl.cainfo来解决

check my curl.ini https://gist.github.com/reinaldomendes/97fb2ce8a606ec813c4b 检查我的curl.ini https://gist.github.com/reinaldomendes/97fb2ce8a606ec813c4b

Answering my own question after 2 days of debugging. 调试2天后回答我自己的问题。 After playing with compiling everything one by one, I realized that after the last update in the Amazon Linux server - caused by the heartbleed issue, there was a problem with NSS, package manager couldn't update it because of some broken dependencies. 在逐个编译所有内容之后,我意识到在Amazon Linux服务器的最后一次更新之后 - 由于心跳问题导致NSS出现问题,由于某些破坏的依赖性,程序包管理器无法更新它。

The one solution was to re-compile PHP with cURL using openSSL instead of NSS. 一个解决方案是使用openSSL而不是NSS用cURL重新编译PHP。 Before doing that and spend hours (as also not relying on the ease of package management), I created a fresh Amazon Linux server to check if they had updated the package management and NSS worked in the new AMIs. 在做这个并花费数小时(也不依赖于包管理的简易性)之前,我创建了一个新的Amazon Linux服务器来检查他们是否更新了包管理并且NSS在新的AMI中工作。 And they had. 他们有。 Popped a new instance, did a yum update before anything else, and NSS was the first to be updated. 弹出一个新实例,在其他任何事情之前做了yum更新,NSS是第一个更新的。

Tried the above code and worked. 试过上面的代码并且工作了。 So, now I'm moving all the codebase to the new instance, switching EIP to the new instance and all good. 所以,现在我将所有代码库移动到新实例,将EIP切换到新实例并且一切正常。

Ends up, stopping and starting Apache was required for me. 我需要结束,停止和启动Apache。

I couldn't believe it myself. 我自己也不敢相信。 On my AWS EC2, I had yum updated everything and still it wasn't working. 在我的AWS EC2上,我让yum更新了所有内容但仍无法正常工作。 This was trying to cUrl post to authorize.net. 这是试图向authorize.net发帖。 Ends up, get this, thanks to @JimOHalloran restart wasn't doing it. 结束,得到这个,感谢@JimOHalloran重启没有做到这一点。

  • apachectl graceful (no) apachectl优雅(不)
  • apachectl restart (no) apachectl重启(否)
  • apachectl stop apachectl停止
  • apachectl start apachectl开始

BAM! BAM! Problem fixed - I spent 3+ hours today banging my head and reading every page online about this problem. 问题已解决 - 我今天花了3个多小时的时间敲打我的脑袋并在网上阅读有关此问题的每一页。 I have NO CLUE what that's different than a hard restart but it did. 我没有CLUE,这与硬重启有什么不同,但确实如此。 Apparently something else (Curl? OpenSSL?) got restarted too then. 显然其他东西(Curl?OpenSSL?)也重新启动了。

Jsut restart the php-fpm,everything is ok. Jsut重新启动php-fpm,一切正常。

eg: sudo service php-fpm restart

Hope can help you. 希望可以帮到你。

Amazon servers do this often when they are running out of memory. 亚马逊服务器在内存不足时经常这样做。 Check your server's RAM consumption and bump it up these errors should go away. 检查服务器的RAM消耗并将其提升,这些错误应该消失。

I just ran into this problem on an EC2 instance in PHP code that had been working happily for over a year and found I had to: 我刚刚在PHP代码的EC2实例上遇到了这个问题,该代码已经开心工作了一年多,发现我必须:

  1. download the latest cacert.pem from https://curl.haxx.se/ca/cacert.pem https://curl.haxx.se/ca/cacert.pem下载最新的cacert.pem
  2. restart Apache ( sudo service httpd restart ) 重启Apache( sudo service httpd restart

I'm setting the path in my PHP so didn't need to change php.ini or curl.ini : 我在PHP中设置路径所以不需要更改php.inicurl.ini

curl_setopt($ch, CURLOPT_CAINFO, '/path/to/cacert.pem');

This problem took me 1 day.这个问题花了我1天时间。 The solution is simple.解决方案很简单。

Note: If you have a hosting, you should contact your hosting provider注意:如果您有主机,您应该联系您的主机提供商

These commands are for Centos 7这些命令适用于 Centos 7

run this commands运行此命令

sudo yum install -y ca-certificates

sudo vi /etc/php.ini

Change these改变这些

curl.cainfo = "/etc/ssl/certs/ca-bundle.crt"

openssl.cafile="/etc/ssl/certs/ca-bundle.crt"

run this commands运行此命令

sudo systemctl reload httpd

sudo systemctl reload nginx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM