Home  >  Article  >  Backend Development  >  PHP code to obtain remote web content (fopen, curl tested)_PHP tutorial

PHP code to obtain remote web content (fopen, curl tested)_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:28:31774browse

1. Use of fopen

Copy code 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.
?>

Copy code The code is as follows:

// For PHP 5 and For later versions, you can use the following code
$handle = fopen("http://s.jb51.net", "rb");
$contents = stream_get_contents($handle );
fclose($handle);
echo $contents;
?>

But the above code is prone to failed to open stream: HTTP request failed! error, Solution

Some people say that in php.ini, there are two options: allow_url_fopen =on (indicating that remote files can be opened through url), user_agent="PHP" (indicating which script to access the network through, default Just remove the ";" in front of it) and restart the server.
But some people still have this warning message. There is still one step left to achieve a perfect solution. You have to set the user_agent in php.ini. The default user_agent in php is PHP. We change it to Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0) to simulate the browser
PHP code to obtain remote web content (fopen, curl tested)_PHP tutorial
user_agent="Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"

Encountered at work This problem was finally solved perfectly, so I share it with everyone.
2. Use curl to implement
Copy code 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;
?>


You can use the following code to download under Linux
exec("wget ​​{$url}");

The difference between PHP grabbing external resource functions fopen / file_get_contents / curl

fopen / file_get_contents will re-do the DNS query for each request and does not cache the DNS information.
But CURL will automatically cache DNS information. Requests for web pages or images under the same domain name only require one DNS query.
This greatly reduces the number of DNS queries.
So the performance of CURL is much better than fopen / file_get_contents.

Original content from Script House, please indicate the source when reprinting.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/323602.htmlTechArticle1. The copy code for using fopen is as follows: ?php $handle = fopen ("http://s. jb51.net/", "rb"); $contents = ""; while (!feof($handle)) { $contents .= fread($handle, 8192); } fclos...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn