[英]PHP cURL working locally, error 77 on AWS server
最新更新:腳本通過 SSH shell 作為“php script.php”成功運行,是管理員用戶。 當 nginx 用戶運行時,curl 命令無法執行 https 請求。 所以我想這是 nginx 用戶無法正確使用 curl 的問題。 我檢查了十分之一的配置文件,但找不到可以更改的地方。
我正在嘗試發布以下 JSON
{
"where": {
"userId": "97"
},
"data": {
"alert": "Joe Masilotti answered a question you follow",
"badge": "Increment","eventId":"3","questionId":"8954","answerId":"977"
}
}
使用以下代碼
$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);
它適用於我的本地 Apache。 我在 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:
使用以下命令從 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 擴展已在服務器上正確設置,並且可以在其他腳本中使用。 謝謝!
更新:腳本“script.php”在從命令行作為“php script.php”在服務器上運行時成功運行,而在“ http://www.mysite.com/script.php ”的瀏覽器上請求時失敗
更新:這是我的 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
我在亞馬遜AMI linux上有同樣的錯誤。
我通過在/etc/php.d/curl.ini上設置curl.cainfo來解決
檢查我的curl.ini https://gist.github.com/reinaldomendes/97fb2ce8a606ec813c4b
調試2天后回答我自己的問題。 在逐個編譯所有內容之后,我意識到在Amazon Linux服務器的最后一次更新之后 - 由於心跳問題導致NSS出現問題,由於某些破壞的依賴性,程序包管理器無法更新它。
一個解決方案是使用openSSL而不是NSS用cURL重新編譯PHP。 在做這個並花費數小時(也不依賴於包管理的簡易性)之前,我創建了一個新的Amazon Linux服務器來檢查他們是否更新了包管理並且NSS在新的AMI中工作。 他們有。 彈出一個新實例,在其他任何事情之前做了yum更新,NSS是第一個更新的。
試過上面的代碼並且工作了。 所以,現在我將所有代碼庫移動到新實例,將EIP切換到新實例並且一切正常。
我需要結束,停止和啟動Apache。
我自己也不敢相信。 在我的AWS EC2上,我讓yum更新了所有內容但仍無法正常工作。 這是試圖向authorize.net發帖。 結束,得到這個,感謝@JimOHalloran重啟沒有做到這一點。
BAM! 問題已解決 - 我今天花了3個多小時的時間敲打我的腦袋並在網上閱讀有關此問題的每一頁。 我沒有CLUE,這與硬重啟有什么不同,但確實如此。 顯然其他東西(Curl?OpenSSL?)也重新啟動了。
Jsut重新啟動php-fpm,一切正常。
eg: sudo service php-fpm restart
希望可以幫到你。
亞馬遜服務器在內存不足時經常這樣做。 檢查服務器的RAM消耗並將其提升,這些錯誤應該消失。
我剛剛在PHP代碼的EC2實例上遇到了這個問題,該代碼已經開心工作了一年多,發現我必須:
cacert.pem
sudo service httpd restart
) 我在PHP中設置路徑所以不需要更改php.ini
或curl.ini
:
curl_setopt($ch, CURLOPT_CAINFO, '/path/to/cacert.pem');
這個問題花了我1天時間。 解決方案很簡單。
注意:如果您有主機,您應該聯系您的主機提供商
這些命令適用於 Centos 7
運行此命令
sudo yum install -y ca-certificates
sudo vi /etc/php.ini
改變這些
curl.cainfo = "/etc/ssl/certs/ca-bundle.crt"
openssl.cafile="/etc/ssl/certs/ca-bundle.crt"
運行此命令
sudo systemctl reload httpd
sudo systemctl reload nginx
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.