Home > Backend Development > PHP Tutorial > PHP code to obtain remote web content

PHP code to obtain remote web content

WBOY
Release: 2016-07-25 08:56:45
Original
1329 people have browsed it
This article introduces several methods of obtaining remote web content implemented by PHP, including fopen and curl methods. Friends in need may refer to them.

Share a few pieces of php code to get the content of remote web pages. 1. fopen method

<?php 
$handle = fopen ("http://bbs.it-home.org/", "rb"); 
$contents = ""; 
while (!feof($handle)) { 
$contents .= fread($handle, 8192); 
} 
fclose($handle); 
echo $contents; //输出获取到得内容。 

//以下适用于php5以上版本 
$handle = fopen("http://bbs.it-home.org", "rb"); 
$contents = stream_get_contents($handle); 
fclose($handle); 
echo $contents; 
?>
Copy after login

If an error occurs: failed to open stream: HTTP request failed!.

Solution: In php.ini, there are two options: allow_url_fopen =on (indicates that remote files can be opened through url), user_agent="PHP" (indicates which script is used to access the network, by default there is a ";" in front of it, just remove it. ) Restart the server. As shown below:

Perfect solution: Set the user_agent in php.ini. The default user_agent of php is PHP. We 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)"

2. curl method

<?php 
$url = "http://bbs.it-home.org"; 
$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; 
?> 
Copy after login

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

The differences between PHP’s external resource grabbing functions fopen, file_get_contents, and curl: 1. fopen/file_get_contents will redo the DNS query for each request and does not cache the DNS information. 2. 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. Therefore, the performance of CURL is better than fopen and file_get_contents, and it is recommended to use it.



source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template