PHP code for obtaining remote webpage content (fopen, curl tested) _ PHP Tutorial

Source: Internet
Author: User
PHP code for obtaining remote webpage content (fopen, curl tested ). 1. the code for copying fopen code is as follows :? Php $ handlefopen (s.jb51.net, rb); $ contents; while (! Feof ($ handle) {$ contents. fread ($ handle, 8192);} fclos 1. Use of fopen

The code is as follows:


$ Handle = fopen ("http://s.jb51.net/", "rb ");
$ Contents = "";
While (! Feof ($ handle )){
$ Contents. = fread ($ handle, 8192 );
}
Fclose ($ handle );
Echo $ contents; // output the obtained content.
?>


The code is as follows:


// The following code can be used for PHP 5 and later versions:
$ Handle = fopen ("http://s.jb51.net", "rb ");
$ Contents = stream_get_contents ($ handle );
Fclose ($ handle );
Echo $ contents;
?>


However, the above code is prone to failed to open stream: HTTP request failed! Error. Solution

Some people say that in php. in ini, there are two options: allow_url_fopen = on (indicating that remote files can be opened through url), user_agent = "PHP" (indicating which script is used to access the network. by default, there is "; "remove it .) Restart the server.
However, some of them still have this warning message. if you want to use the perfect solution, you still have to set php. user_agent in ini. the default user_agent in php is PHP. we can change it to Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0) to simulate the browser.

User_agent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0 )"

If you encounter this problem at work and solve it perfectly, share it with you.
2. implemented through curl

The code is as follows:


$ Url = "http://s.jb51.net ";
$ Ch = curl_init ();
Curl_setopt ($ ch, CURLOPT_URL, $ url );
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1 );
Curl_setopt ($ ch, CURLOPT_CONNECTTIMEOUT, 10 );
$ Dxycontent = curl_exec ($ ch );
Echo $ dxycontent;
?>



In linux, you can download the following code:
Exec ("wget {$ url }");

Difference between the function fopen/file_get_contents/curl for PHP to capture external resources

Fopen/file_get_contents performs a new DNS query every time a request is sent, and the DNS information is not cached.
However, CURL automatically caches DNS information. Only one DNS query is required for a webpage or image request under the same domain name.
This greatly reduces the number of DNS queries.
Therefore, CURL performance is much better than fopen/file_get_contents.

The original content of the script home. For more information, see the source.

The pipeline code is as follows :? Php $ handle = fopen ("http://s.jb51.net/", "rb"); $ contents = ""; while (! Feof ($ handle) {$ contents. = fread ($ handle, 8192);} fclos...

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.