4

好的,我想使用 cURL 对 SSL 站点进行 HTTP_POST。我已经将证书导入我的服务器。这是我的代码:

$url  = "https://www.xxx.xxx";
$post = "";# all data that going to send

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0'); 

$exe  = curl_exec($ch);
$getInfo = curl_getinfo($ch);

if ($exe === false) {
    $output = "Error in sending";
    if (curl_error($ch)){
        $output .= "\n". curl_error($ch);
    }
} else if($getInfo['http_code'] != 777){
    $output = "No data returned. Error: " . $getInfo['http_code'];
    if (curl_error($ch)){
        $output .= "\n". curl_error($ch);
    }
}

curl_close($ch);

echo $output;

它一直返回“500”。根据 w3schools,500 表示内部服务器错误。我的服务器有问题吗?如何解决/解决这个问题?

4

3 回答 3

8

某些服务器(尤其是使用 SSL 请求的服务器)在某些请求参数设置不正确的情况下返回 500。

为避免“ 500 错误”(例如),请务必:

  • 如果需要,设置适当的“Referer:”标题,使用

curl_setopt(CURLOPT_REFERER, 'http://site.com/ref_page');

  • 设置正确的“User-Agent:”标头,使用

curl_setopt(CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)');

于 2013-01-07T06:47:54.093 回答
2
$url  = "https://www.xxx.xxx";
$post = "";# all data that going to send

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 5.01; Windows NT    5.0'); 

$exe  = curl_exec($ch);
$getInfo = curl_getinfo($ch);

if ($exe === false) {
$output = "Error in sending";
if (curl_error($ch)){
    $output .= "\n". curl_error($ch);
}
} else if($getInfo['http_code'] != 777){
$output = "No data returned. Error: " . $getInfo['http_code'];//as preline
if (curl_error($ch)){
    $output .= "\n". curl_error($ch);
}
}

curl_close($ch);

echo $output;

顺便说一句:确保安装了 CURL 模块。

于 2010-03-16T09:39:45.627 回答
0

您的服务器或服务器上执行的脚本有问题,可能会引发未处理的异常。

您应该检查服务器的访问和错误日​​志。

于 2010-03-16T09:34:23.950 回答