Use curl_setopt crawl Baidu, Baidu picture anti-theft have what good method?

Source: Internet
Author: User
$url = "Http://www.baidu.com/s?wd=csdn"; $header = Array (        ' user-agent:mozilla/5.0 (Windows NT 5.1) applewebkit/ 537.36 (khtml, like Gecko) chrome/33.0.1750.146 safari/537.36 '); $ch = Curl_init (); $timeout = 10; curl_setopt ($ch, Curlopt_url, $url), curl_setopt ($ch, Curlopt_httpheader, $header); curl_setopt ($ch, curlopt_returnt Ransfer, 1); curl_setopt ($ch, Curlopt_connecttimeout, $timeout); $content = Curl_exec ($ch); if ($content = = FALSE) {
  
   echo "Error:". Curl_error ($ch);} Curl_close ($ch); Echo $content;
  



Reply to discussion (solution)

Forge Referer URLs

The 1 floor has been made very clear.

Forge Referer URLs


curl_setopt ($ch, Curlopt_referer, ' http://www.baidu.com/');
No way.

You have to understand that when the picture is displayed, it's not PHP's curl request, but the browser's request. Browser sent the request, your current URL is what domain name such as www.devtest.com, then the picture of refer is what. So I'm sure I can't see the picture.

One way is, all the pictures of Baidu, you replace the access to write a PHP such as url/image.php?baidu_url=******,***** is the real picture address, and then your image.php in call curl and add refer to crawl.

Http://i7.baidu.com/it/u=1366412274,1504179524&fm=96&s=E840F41219BFE0C844FC8DCA0300F0B3
You are not able to access the code directly (403 error)
But with the cookies you get when you visit Http://www.baidu.com/s?wd=csdn, you can access them in code.

Thank you, we've solved it.

Header (' Content-type:image/jpeg '); $ch = Curl_init (); $url = "Http://t12.baidu.com/it/u=1464136568,2122787600&fm =58&s=3ff6e816c0b4ff90237dc7c40200f024 "; curl_setopt ($ch, Curlopt_url, $url) curl_setopt ($ch, CURLOPT_REFERER, ' http://www.baidu.com '); $content = curl_exec ($ch); Curl_close ($ch); Echo $content;
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.