doutingyou2198 2014-04-16 07:07
浏览 80
已采纳

PHP CURL帖子给出了未知错误

  • I'm trying to get an AJAX response by posting parameters through PHP CURL POST request

  • 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.

  • 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.

  • Then I tried to POST this request URL through PHP CURL POST method, but I'm getting some invalid output.

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

**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'
  • 写回答

2条回答 默认 最新

  • drrvnbwle80177811 2014-04-16 07:10
    关注

    Looks like its gzip content to me. Use this header to handle this:

    curl_setopt($ch, CURLOPT_ENCODING, "");
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法