Search for a web page by simulating a browser capture

Source: Internet
Author: User
Please master, simulate the web page capture web pages such as capture http://map.sogou.com/api/this web page, I write the program, if the network does not bring the following "/", will capture not, but site internet (http://tool.chinaz.com/Tools/PageCode.aspx), without the last "/" can be captured: http://map.sogou.com/api), what's his principle? My code is posted below. please make improvements

function file_get($url){ ob_start(); $ch = curl_init();  curl_setopt($ch, CURLOPT_COOKIEJAR, "./cookie.txt"); curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; InfoPath.1; CIBA)");curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HEADER, FALSE); curl_setopt($ch, CURLOPT_COOKIESESSION, TRUE); curl_setopt($ch, CURLOPT_NOBODY, FALSE); curl_exec($ch); curl_close($ch); $content = ob_get_clean();  return $content;}


Reply to discussion (solution)

Add the following code:

Curl_setopt ($ ch, CURLOPT_FOLLOWLOCATION, 1 );

Because you capture http://map.sogou.com/api, the other response is 301rmb. Add this sentence to capture the html after the jump.

Add the following code:

Curl_setopt ($ ch, CURLOPT_FOLLOWLOCATION, 1 );

Because you capture http://map.sogou.com/api, the other response is 301rmb. Add this sentence to capture the html after the jump.
And cannot be obtained.


Add the following code:

Curl_setopt ($ ch, CURLOPT_FOLLOWLOCATION, 1 );

Because you capture http://map.sogou.com/api, the other response is 301rmb. Add this sentence to capture the html after the jump.
And cannot be obtained.


function file_get($url){ ob_start(); $ch = curl_init();  curl_setopt($ch, CURLOPT_COOKIEJAR, "./cookie.txt"); curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; InfoPath.1; CIBA)");curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HEADER, FALSE); curl_setopt($ch, CURLOPT_COOKIESESSION, TRUE); curl_setopt($ch, CURLOPT_FOLLOWLOCATION,TRUE); curl_setopt($ch, CURLOPT_NOBODY, FALSE); curl_exec($ch); curl_close($ch); $content = ob_get_clean();  return $content;}


I have tested it, and it is OK.

CURLOPT_FOLLOWLOCATION

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.