【问题标题】:500 Internal Server Error while using curl使用 curl 时出现 500 内部服务器错误
【发布时间】:2023-03-10 19:21:01
【问题描述】:

我在我的 word press 网站中使用以下代码。我正在尝试通过在我的 php 代码中调用 wcf Web 服务来更改一些功能。下面是我正在使用的代码,它给了我错误

$Url = "http://localhost:8080/Service1.svc/checkUseronHealnt";
$json = "[{\"MOBILE_NO\":\"8745009403,8745009411\"}]"
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $Url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($json));
$curl_response = curl_exec($curl);
if (curl_error($curl)) {
    echo 'error:' . curl_error($curl);
} else {
    echo"Response - " . $curl_response;
}

【问题讨论】:

  • $json = "[{\"MOBILE_NO\":\"8745009403,8745009411\"}]" 此行后缺少分号

标签: php wordpress


【解决方案1】:

$json = "[{\"MOBILE_NO\":\"8745009403,8745009411\"}]" - 语句后缺少分号

从代码中删除多余的大括号{}

你的好代码如下

$Url ="http://localhost:8080/Service1.svc/checkUseronHealnt";
$json = "[{\"MOBILE_NO\":\"8745009403,8745009411\"}]";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $Url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($json));
$curl_response = curl_exec($curl);
if(curl_error($curl)) { 
  echo 'error:' . curl_error($curl); 
}
else {
  echo"Response - ".$curl_response;
}        

【讨论】:

    【解决方案2】:

    $json = "[{\"MOBILE_NO\":\"8745009403,8745009411\"}]"
    ; 在此之后丢失
    curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($json));
    json_encode 在这里不需要。
    除非您提供的代码上方有{,否则最后一个} 也不需要。

    【讨论】:

      最近更新 更多