我的cURL代码有什么问题(http状态为0)?

时间:2010-12-31 15:50:54

标签: php curl

即使我将发送到我浏览器的网址复制并粘贴,我也会一直获得0状态

<?php


$mainUrl = "https://api.xxxx.com/?";
$co = "xxxxx";
$pa = "xxxx";
$par = "xxxx";
$part= "xxxx";
$partn = "xxxx";
$us= "xxx";
$fields_string;
$fields = array(
            'co'=>urlencode($co),
            'pa'=>urlencode($pa),
            'par'=>urlencode($par),
            'part'=>urlencode($part),
            'partn'=>urlencode($partn),
            'us'=>urlencode($us)
            );

foreach($fields as $key=>$value) { $fields_string .= $key . '=' . $value . '&' ;}

$fields_string = rtrim($fields_string, "&");
$fields_string = "?" . $fields_string;

$url = "https://api.xxxxx.com/" . $fields_string;

$request =  $url; 

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_TIMEOUT,'3');
$content = trim(curl_exec($ch));
$http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);

curl_close($ch);
print $url;
print $http_status;
print $content; 



?>

5 个答案:

答案 0 :(得分:21)

意识到我遇到了SSL问题。只需将CURLOPT_SSL_VERIFYPEER和CURLOPT_SSL_VERIFYHOST设置为false即可。的工作原理。

答案 1 :(得分:17)

仅供参考,如果在远程服务器返回数据之前curl连接超时,您还可以获得状态代码0。在这种情况下,您需要设置curl time out选项以避免这种情况。只需将其发布给状态为0的其他人。

答案 2 :(得分:7)

我遇到了同样的问题,你必须在运行 curl_getinfo($ ch)命令之前运行 curl_exec($ ch)命令。

答案 3 :(得分:4)

所以试试这个你会得到积极的结果我已经把CURLOPT_SSL_VERIFYPEER和CURLOPT_SSL_VERIFYHOST加到了假的

<?php


$mainUrl = "https://api.xxxx.com/?";
$co = "xxxxx";
$pa = "xxxx";
$par = "xxxx";
$part= "xxxx";
$partn = "xxxx";
$us= "xxx";
$fields_string;
$fields = array(
            'co'=>urlencode($co),
            'pa'=>urlencode($pa),
            'par'=>urlencode($par),
            'part'=>urlencode($part),
            'partn'=>urlencode($partn),
            'us'=>urlencode($us)
            );

foreach($fields as $key=>$value) { $fields_string .= $key . '=' . $value . '&' ;}

$fields_string = rtrim($fields_string, "&");
$fields_string = "?" . $fields_string;

$url = "https://api.xxxxx.com/" . $fields_string;

$request =  $url; 

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_TIMEOUT,'3');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,false);
$content = trim(curl_exec($ch));
$http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE);

curl_close($ch);
print $url;
print $http_status;
print $content; 



?>

答案 4 :(得分:3)

在调试curl时,应始终设置CURLOPT_VERBOSE选项。您的超时值看起来很低。