Use fsockopen to retrieve Baidu's search page

Source: Internet
Author: User
Tags cache and cookies
Using fsockopen to get Baidu's search page I use php functions fsockopen and fwrite, fgets and other commands can directly get the content of the http://www.baidu.com home page, but can not get the http://www.baidu.com/s? Wd = Speaker & pn = 20 content, very boring, I think Baidu has taken precautions, but the strange thing is that I have cleared all the cache and cookies in the browser, access http://www.baidu.com/s directly with the address bar? Wd = Speaker & pn = 20, but it can directly get the content, but it can't be obtained using the php function. How does Baidu distinguish browser access from program access? Which of the following experts can tell me why and why.
I have no more points, and I can use money to buy a php solution.


Reply to discussion (solution)

Try adding useragent,
Reference: http://tuzwu.iteye.com/blog/723260

Of course, I tried it. I have completely imitated it by referring to the data intercepted by Fiddler2.

I think your network is faulty.

Question about character set encoding?

It's strange that file_get_contents can be used, but Baidu cannot obtain it using my method. other websites are successful. why?

Character set encoding is okay. I have tried it again and again. The same encoding is acceptable for file_get_contents.

I don't know how you wrote it.

$ Fp = fsockopen ("www.baidu.com", 80, $ errno, $ errstr, 30); $ out = "GET/s? Wd = Speaker & pn = 20 HTTP/1.1 \ r \ n "; $ out. = "Host: www.baidu.com \ r \ n"; $ out. = "Connection: Close \ r \ n"; fwrite ($ fp, $ out); while (! Feof ($ fp) {echo fgets ($ fp, 128);} fclose ($ fp );
This time we don't need to map. we need to paste a data clip.
Resource id #2 HTTP/1.1 200 OK
Date: Fri, 29 Nov 2013 07:21:48 GMT
Server: BWS/1.0.
Content-Length: 109229
Content-Type: text/html; charset = utf-8
Cache-Control: private
BDPAGETYPE: 3
BDUSERID: 0
BDQID: 0xba09dfbd018d026b
Set-Cookie: BDSVRTM = 245; path =/
Set-Cookie: H_PS_PSSID = 4__1462_4181_1_1; path =/; domain = .baidu.com
Set-Cookie: BAIDUID = 93A4FEBC6A24E74B727FEE7001E65B50: FG = 1; expires = Fri, 29-Nov-43 07:21:47 GMT; path =/; domain = .baidu.com
P3P: CP = "oti dsp cor iva our ind com"
Connection: Close

......

> SpeakerChannels

SpeakerQuote center brand Rambler BOSE Mai Bo Hui Wei JBL Philips Fenda Logitech Xiaomi Sannuo innovation Longines more> Price 100 yuan below 101-200 yuan 201-300 yuan 301-500 yuan 501-800 yuan...

Speaker.pcpop.com/soundbox/ 2013-11-11

-Target = "_ blank"
Class = "m"> Baidu snapshot

View

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.