[英]PHP CURL post giving an unknown error
I'm trying to get an AJAX response by posting parameters through PHP CURL POST request 我试图通过通过PHP CURL POST请求发布参数来获取AJAX响应
I am in need to getting certain results from a website(data crawling). 我需要从网站上获取某些结果(数据爬网)。
The website is using AJAX calls to get the populate results inside. 该网站正在使用AJAX调用来获取内部填充结果。
I had inspected the AJAX request made in the site through firebug and when I 'POST'ed the same request URL through RESTClient debugger,I got the results as a JSON. 我已经检查了通过Firebug在站点中发出的AJAX请求,并且当我通过RESTClient调试器“发布”相同的请求URL时,得到的结果是JSON。
Then I tried to POST this request URL through PHP CURL POST method, but I'm getting some invalid output. 然后,我尝试通过PHP CURL POST方法发布此请求URL,但是得到了一些无效的输出。
My Code 我的密码
<?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);
?>
OUTPUT OUTPUT
**string(20) "�"**
EDIT 编辑
With reference to the suggestion below, I have added 关于以下建议,我添加了
curl_setopt($ch, CURLOPT_ENCODING, "");
Now I'm getting error as follows, 现在我得到如下错误,
Couldn't resolve host 'www.example.com'
Looks like its gzip
content to me. 在我看来,它的gzip
内容。 Use this header to handle this: 使用此标头来处理此问题:
curl_setopt($ch, CURLOPT_ENCODING, "");
添加此curl_setopt($ ch,CURLOPT_HTTPHEADER,array('x-requested-with'=>'XMLHttpRequest'),#不返回标头
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.