php CURL 执行返回空响应并显示
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16769261/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
CURL execution returns empty response and shows
提问by AnNaMaLaI
I am using CURL in my project, Its working fine locally but if i use the same code its not executing, I tried to debug it. The output as follows :
我在我的项目中使用 CURL,它在本地工作正常,但如果我使用相同的代码,它没有执行,我尝试调试它。输出如下:
Took 0 seconds to send a request to https://www.google.co.in
I used the following sample code:
我使用了以下示例代码:
$handle=curl_init('https://www.google.co.in');
curl_setopt($handle, CURLOPT_VERBOSE, true);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
curl_setopt($handle, CURLOPT_SSL_VERIFYPEER, false);
$content = curl_exec($handle);
if(!curl_errno($handle))
{
$info = curl_getinfo($handle);
echo '<br/> Took ' . $info['total_time'] . ' seconds to send a request to ' . $info['url'];
}
else
{
echo 'Curl error: ' . curl_error($handle);
}
echo $content;
The Problem is in server, I don't know what to enable in php.ini.
问题出在服务器上,我不知道在 php.ini 中启用什么。
Please note that in server CURL and SSL is enabled. If anyone faced similar problems, share solutions please.
请注意,在服务器 CURL 和 SSL 中启用。如果有人遇到类似的问题,请分享解决方案。
采纳答案by Jerin K Alexander
PROBLEM:
Function
curl_exec
is disabled. What to do?SOLUTION:
In order to eliminate this error message you need to do oneof the following things:
- Remove the
curl_exec
string from thedisable_functions
in thephp.ini
file- Ask your hosting provider to remove the string above if you don't have an access to the
php.ini
file- Change hosting provider to one which allows the running of the
curl_exec
function.Here is an example of the entry in
php.ini
.Change from:
disable_functions = exec,passthru,shell_exec,system,proc_open,popen,curl_exec,curl_multi_exec,show_source,eval,posix_getpwuid
To:
disable_functions = exec,passthru,shell_exec,system,proc_open,popen,curl_multi_exec,show_source,eval,posix_getpwuid
问题:
功能
curl_exec
被禁用。该怎么办?解决方案:
为了消除你需要做此错误消息一个下面的事情:
- 删除
curl_exec
从字符串disable_functions
中php.ini
的文件- 如果您无权访问该
php.ini
文件,请要求您的托管服务提供商删除上述字符串- 将主机提供商更改为允许运行该
curl_exec
功能的提供商。这是 中条目的示例
php.ini
。更改自:
disable_functions = exec,passthru,shell_exec,system,proc_open,popen,curl_exec,curl_multi_exec,show_source,eval,posix_getpwuid
到:
disable_functions = exec,passthru,shell_exec,system,proc_open,popen,curl_multi_exec,show_source,eval,posix_getpwuid
回答by icc97
For me in this case, the problem was that I was getting a 302 redirect rather than the actual get request.
在这种情况下,对我来说,问题是我收到的是 302 重定向而不是实际的 get 请求。
After turning on CURLOPT_VERBOSE
from this answer
CURLOPT_VERBOSE
从这个答案开启后
$fp = fopen(dirname(__FILE__).'/errorlog.txt', 'w');
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_STDERR, $fp);
I got the following in the errorlog.txt
:
我在以下内容中得到了errorlog.txt
:
* Connected to www.example.com (X.X.X.X) port 443 (#0)
* successfully set certificate verify locations:
* CAfile: none
CApath: /etc/ssl/certs
* SSL connection using ECDHE-RSA-AES128-GCM-SHA256
* Server certificate:
* subject: OU=Domain Control Validated; OU=COMODO SSL; CN=www.example.com
* start date: 2016-04-28 00:00:00 GMT
* expire date: 2018-04-08 23:59:59 GMT
* issuer: C=GB; ST=Greater Manchester; L=Salford; O=COMODO CA Limited; CN=COMODO RSA Domain Validation Secure Server CA
* SSL certificate verify ok.
> GET /path/to/file.php
HTTP/1.1^M
Host: www.example.com^M
Accept: */*^M
^M
< HTTP/1.1 302 Found^M
* Server nginx is not blacklisted
< Server: nginx^M
< Date: Tue, 20 Sep 2016 02:06:45 GMT^M
< Content-Type: text/html^M
< Transfer-Encoding: chunked^M
< Connection: keep-alive^M
< X-Powered-By: PHP/5.5.9-1ubuntu4.19^M
< Location: https://www.example.com/other/url/^M
< ^M
* Connection #0 to host www.example.com left intact
N.B.
NB
HTTP/1.1 302 Found
(should just be a 200 response)Location: https://www.example.com/other/url/
is different from theGET
request URL
HTTP/1.1 302 Found
(应该只是 200 响应)Location: https://www.example.com/other/url/
与GET
请求 URL不同