PHP CURL帖子给出了未知错误

时间:2014-04-16 07:07:07

标签: php ajax post curl request

  • 我试图通过PHP发布参数来获取AJAX响应 CURL POST请求

  • 我需要从网站获得某些结果(数据 爬行)。

  • 网站正在使用AJAX调用来获取填充结果。

  • 我通过firebug检查了网站上的AJAX请求,当我通过RESTClient调试器发布相同的请求URL时,我得到的结果是JSON。

  • 然后我尝试通过PHP CURL POST方法发布此请求网址,但我收到了一些无效的输出。

我的代码

<?php
$ch = curl_init();
$url = 'http://www.example.com/Hotel-Search?inpAjax=true&responsive=true';
$fields = array(
    'endDate'=>'04/15/2014',
    'hashParam'=>'b2c21a315f0a0fb3f0c39f60XXXXXX',
    'startDate'=>'04/14/2014',
);
$headers = array(
    'Accept:    application/json, text/javascript,text/html'
);

$agent = '  Mozilla/5.0 (Windows NT 5.1; rv:28.0) Gecko/20100101 Firefox/28.0';
curl_setopt($ch, CURLOPT_URL, $url);

curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
//curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($fields));
$result = curl_exec($ch) or die(curl_error($ch));
var_dump($result);

?>

输出

**string(20) "�"** 

修改

参考下面的建议,我已添加

curl_setopt($ch, CURLOPT_ENCODING, "");

现在我收到如下错误,

Couldn't resolve host 'www.example.com'

2 个答案:

答案 0 :(得分:0)

看起来像我的gzip内容。使用此标头来处理此问题:

curl_setopt($ch, CURLOPT_ENCODING, "");

答案 1 :(得分:0)

添加此curl_setopt($ ch,CURLOPT_HTTPHEADER,数组(&#39; x-requested-with&#39; =&gt;&#39; XMLHttpRequest&#39;),#donttt return header