使用php curl的方式調用對方提供的接口,收到了如下錯誤提示
HTTP Status 415
The server refused this request because the request entity is in a format not supported by the requested resource for the requested method.
Curl 的代碼片段如下:
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_HEADER, FALSE);
- curl_setopt($ch, CURLOPT_NOBODY, FALSE);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
- curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
- curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
- curl_setopt($ch, CURLOPT_TIMEOUT, 120);
- curl_setopt($ch, CURLOPT_POST, TRUE);
- curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($postData));
- $data = curl_exec($ch);
- curl_close($ch);
多次檢查curl設置已經接口的說明沒有發現問題。對方的服務器使用的是Tomcat 7, 一度懷疑是對方web配置有誤,后來仔細研究文檔,其中提到Response是jason格式文檔,而上述curl中沒有指定Request Header 信息, 所以嘗試加入一個header, 結果問題解決。 代碼如下:
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json; charset=utf-8"));
新聞熱點
疑難解答