【问题标题】:cURL + HTTP_POST, keep getting 500 error. Has no idea?cURL + HTTP_POST,不断收到 500 错误。不知道吗?
【发布时间】:2011-01-28 00:49:24
【问题描述】:

好的,我想使用 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 表示内部服务器错误。我的服务器有问题吗?如何解决/解决这个问题?

【问题讨论】:

    标签: php ssl post curl http-post


    【解决方案1】:

    如果请求的某些参数设置不正确,某些服务器(尤其是使用 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)');

    【讨论】:

      【解决方案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 模块。

      【讨论】:

      • 对不起。一些拼写错误。我正在编辑代码。我检查了代码,变量名是正确的,它仍然给我“错误:500”。任何想法? (我将编辑我的问题,以避免混淆,谢谢)
      • 那会不会是服务器问题?那500是什么意思?我还是打不通,继续返回 500。Arggggghhh!!!!
      • 您的服务器中可能尚未安装 CURL 模块。
      • 嗨 SpawnCxy,cURL 模块已安装。我能够使用类似的代码发送到非 SSL 站点。不确定它是否与 SSL 相关
      【解决方案3】:

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

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

      【讨论】:

      • 你的意思是,我应该联系我的托管公司吗?
      • 如果您不能直接访问服务器的日志,您可以要求他们将它们发送给您,是的。
      猜你喜欢
      • 2015-08-25
      • 2017-06-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多