我使用PHP curl方法获取字符串类型响应。要创建我使用的请求:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, $data);
$response = curl_exec($ch);
$err = curl_error($ch);
curl_close($ch);
if($response === false)
throw new Exception(__CLASS__."::".__FUNCTION__."_".$err);
return $response;
为什么我总是收到一个bool(true)响应,而不是从另一方回复的字符串?
由于
答案 0 :(得分:10)
既然你已经有了
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
你的代码中的。 curl_exec应该已经返回页面的内容而不是BOOL。
这是我使用的库的片段。正如所指出的那样,这可能不需要,但它帮助了我一次......
//The content - if true, will not download the contents
curl_setopt($ch, CURLOPT_NOBODY, false);
它似乎也有一些与CURLOPT_NOBODY相关的错误(这可能解释了为什么你有这个问题):