我们发送帖子时有下一个标题:
POST http://www.autonavigator.ru/dispatcher.pl HTTP/1.1
Host: www.autonavigator.ru
Connection: keep-alive
Content-Length: 55
Origin: http://www.autonavigator.ru
X-Request: JSON
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.104 Safari/537.36
Content-type: application/x-www-form-urlencoded; charset=UTF-8
Accept: application/json
X-Requested-With: XMLHttpRequest
Referer: http://www.autonavigator.ru/my/offer_add/
Accept-Encoding: gzip,deflate
Accept-Language: ru-RU,ru;q=0.8,en-US;q=0.6,en;q=0.4
Cookie: region_id=45; city_id=22; user_name=rora%40gmail.com; user_type=user; user_offer_count=1; user_message_count=0; user_no_confirm=1; session_id=WR9q4d41DgD7biTOOsMzgtXfJm83VFQn; USession=WR9q4d41DgD7biTOOsMzgtXfJm83VFQn; _ym_visorc_5781676=b
class=list&method=make&show_all=1&vehicle=car&type=used
我想用curl模拟浏览器。
为此,我使用下一个代码:
$ch = curl_init('http://www.autonavigator.ru/dispatcher.pl');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.104 Safari/537.36");
$headers = array
(
'Accept: application/json',
'Accept-Language: ru-RU,ru;q=0.8,en-US;q=0.6,en;q=0.4',
'Accept-Encoding: gzip,deflate',
'Accept-Charset: windows-1251,utf-8;q=0.7,*;q=0.7'
);
curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);
curl_setopt($ch, CURLOPT_REFERER, "http://www.autonavigator.ru/my/offer_add/");
curl_setopt($ch, CURLOPT_POSTFIELDS, 'class=list&method=make&show_all=1&vehicle=car&type=used');
$result = curl_exec($ch);
curl_close($ch);
var_dump($result);
但结果我们得到了一些错误(http://i.stack.imgur.com/zWkdP.png):
请告诉我,代码中的错误是怎么回事?
答案 0 :(得分:2)
最有可能的内容是gzip压缩,所以你只需要这样做:
curl_setopt($ch,CURLOPT_ENCODING , "gzip");