php cURL提供空白响应体
我在下面的cURL请求中得到了一个空白的响应体,但只在一台机器上(osx运行两个vagrant box)。我已经检查并重新检查了项目代码,它确实在我使用过的所有其他机器上工作。。除了一个。有什么想法吗 我在PHP中的调用是php cURL提供空白响应体,php,curl,zend-framework2,laminas-api-tools,Php,Curl,Zend Framework2,Laminas Api Tools,我在下面的cURL请求中得到了一个空白的响应体,但只在一台机器上(osx运行两个vagrant box)。我已经检查并重新检查了项目代码,它确实在我使用过的所有其他机器上工作。。除了一个。有什么想法吗 我在PHP中的调用是POSTRequestTrusted(/api/login, 输出: HTTP\\/1.1 200 OK\\r\\n日期:周二,2014年6月24日22:41:46 GMT\\r\\n服务器:Apache\\/2.4.9(Ubuntu)\\r\\nX由以下驱动:PHP\\/5
POSTRequestTrusted(/api/login,
输出:
HTTP\\/1.1 200 OK\\r\\n日期:周二,2014年6月24日22:41:46 GMT\\r\\n服务器:Apache\\/2.4.9(Ubuntu)\\r\\nX由以下驱动:PHP\\/5.5.13-2+deb.sury.org~precise+1\\r\\n缓存控制:无存储\\r\\n标签:无缓存\\r\\n内容长度:174 r\\n内容类型:应用程序\\/json“,null
返回值
返回在适当PHP类型的json中编码的值。值true、false和null分别作为true、false和null返回。如果json无法解码或编码的数据深度超过递归限制,则返回null。
你应该检查返回的正文实际上是什么,因为它没有正确解码。你得到的内容长度标题说你应该有174字节的内容。我会调查一下,但是我想知道为什么我的请求在其他机器上工作。它们都是通过Vagrant运行的。json_解码的行为不同租来的..尽管我已经把系统从vagrant中弄出来..这很奇怪,原来是BOM,我需要在运行JSON_解码之前去掉它。。。
public static function POSTRequestTrusted($service_name, $data) {
self::checkApiKey(); //checks the key is set
return self::execCURLRequest("POST", $service_name, array('Content-Type: application/json',
'Accept: application/json',
'Authorization: Basic ' . self::$apiKey )
, $data);
}
public static function execCURLRequest($type, $service_name = null, $custom_headers = null, $data = null) {
//If the verb is unexpected, throw exception
if($type !== "GET"
and $type!== "POST"
and $type!== "DELETE"
and $type!== "PUT"
and $type!== "PATCH" )
{ return -1 ;}
//If calling a service, or just pinging the base hostname:port
if($service_name !== null) {
$full_url = self::$apiBase . $service_name;
} else {
$full_url = self::$apiBase;
}
// initialize cURL
$ch = curl_init($full_url);
// SET the HTTP VERB type, unless it's a POST which doesn't require anything
if($type == 'PUT' or $type == 'DELETE' or $type == 'PATCH'){
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $type);
} else if ($type == 'GET'){
curl_setopt($ch, CURLOPT_HTTPGET, true);
}
//Flag needed to return response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// custom headers are needed for testing trusted client calls.
if($custom_headers !== null){
$full_header = $custom_headers;
} else {
$full_header = self::$STD_HEADER;
}
curl_setopt($ch, CURLOPT_HTTPHEADER, $full_header); //Set the Header
curl_setopt($ch, CURLOPT_HEADER, 1); // return headers?
// If data was present, encode it and append to request
if($data !== null){
$json_data = json_encode($data);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);
}
$response = curl_exec ($ch);
// parse the entire response, down to the header and body
list($response_header, $body) = explode("\r\n\r\n", $response, 2);
$response_body = json_decode($body);
curl_close ($ch);
return array($response_header, $response_body);
$response_body = json_decode($body);
curl_close ($ch);
return array($response_header, $response_body);