php - CURL 执行返回空响应并显示

标签 php curl

我在我的项目中使用 CURL,它在本地工作正常,但如果我使用 相同的代码没有执行,我试图调试它。输出如下:

Took 0 seconds to send a request to https://www.google.co.in

我使用了以下示例代码:

$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;

问题出在服务器上,我不知道在 php.ini 中启用什么。

请注意,在服务器中启用了 CURL 和 SSL。如果有人遇到类似问题,请分享解决方案。

最佳答案

在这种情况下,对我来说,问题是我收到了 302 重定向,而不是实际的获取请求。

打开 CURLOPT_VERBOSE 之后 answer

$fp = fopen(dirname(__FILE__).'/errorlog.txt', 'w');
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_STDERR, $fp);

我在 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

注意

  1. HTTP/1.1 302 Found(应该只是一个 200 响应)
  2. Location: https://www.example.com/other/url/ 不同于GET请求URL

关于php - CURL 执行返回空响应并显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16769261/

相关文章:

php - 仅生成刚刚测试的内容的覆盖范围

mongodb - Elasticsearch curl : (7) couldn't connect to host

PHP curl : how to set body to binary data?

javascript - Jquery 自动完成和 MySQL ID 值

PHP Switch case 和 if 语句没有返回正确的值

python - 处理 pycurl 在 Twitter streaming api 上挂起

curl - 如何使用 curl post 获取 HTTP 状态代码

node.js - 混淆 SSL 与 Node 和使用 cURL

javascript - 主干 View /模板无法从 REST 加载

php - 从数据库创建 CSV 文件时启用 PHP 错误