最新更新:脚本作为管理员用户通过 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 扩展在服务器上正确设置,并且可以在其他脚本中使用。谢谢!
UPDATE:当从命令行作为“php script.php”在服务器上运行时,脚本“script.php”运行成功,而在浏览器上请求“http://www.mysite.com/script.php http://www.mysite.com/script.php"
UPDATE:这是我的卷发-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
我试图检查本地和服务器 PHP 配置之间的curl差异,我看到本地curl SSL版本是OpenSSL/1.0.1f在服务器上时是NSS/3.14.3.0