dongsheng4126335 2016-03-10 07:15
浏览 31
已采纳

如何使用cURL获取网站

I am not able to fetch data using cURL in php AS I can do this using curl command in terminal like below -

curl http://www.universalapplianceparts.com/search.aspx?find=w10130694

This command provide expected result in terminal.

I want same result in php using curl

Below is my php code -

$url = "http://www.universalapplianceparts.com/search.aspx?find=W10130694";
$ch = curl_init();  
    curl_setopt($ch, CURLOPT_URL,"http://www.universalapplianceparts.com/");
    curl_setopt($ch, CURLOPT_POST, true);  
    curl_setopt($ch, CURLOPT_HEADER, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $url); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    $output = curl_exec ($ch); 
    curl_close ($ch); 
    var_dump($output);

Please let me know why I am not getting same result using php curl as I am getting in terminal.

Thanks

  • 写回答

3条回答 默认 最新

  • drevls8138 2016-03-10 07:26
    关注

    Try this ::

        $url = "http://www.universalapplianceparts.com/search.aspx?find=W10130694";
    $ch1= curl_init();
    curl_setopt ($ch1, CURLOPT_URL, $url );
    curl_setopt($ch1, CURLOPT_HEADER, 0);
    curl_setopt($ch1,CURLOPT_VERBOSE,1);
    curl_setopt($ch1, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.0.3705; .NET CLR 1.1.4322; Media Center PC 4.0)');
    curl_setopt ($ch1, CURLOPT_REFERER,'http://www.google.com');  //just a fake referer
    curl_setopt($ch1, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch1,CURLOPT_POST,0);
    curl_setopt($ch1, CURLOPT_FOLLOWLOCATION, 20);
    
    $htmlContent= curl_exec($ch1);
    echo $htmlContent;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计