fopen is a very good function that collects the content of the remote server and saves it locally. It can also open local files. Let’s take a look at the failed to open stream when using the fopen function: HTTP Solution to request failed problem.
$handle = fopen ("http://www.zhutiai.com/c5-03/", "rb");
$contents = "";
while (!feof($handle)) {
$contents .= fread($handle, 8192);
}
fclose($handle);
echo $contents; //Output the obtained content.
?>
// For PHP 5 and above you can use the following code
$handle = fopen("http://mb.bKjia.c0m", "rb");
$contents = stream_get_contents($handle);
fclose($handle);
echo $contents;
?>
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, by default there is ";" in front of it, that is, remove it) Yes. ) 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 changed it to Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0) to simulate the browser
Use PHP’s CURL module to retrieve the PHP homepage and save it to a file
$ch = curl_init("http://www.bKjia.c0m/");
$fp = fopen("php_homepage.txt", "w");
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
?>
List of curl related functions:
curl_init — Initialize a CURL session
curl_setopt — Set an option for CURL calls
curl_exec — Execute a CURL session
curl_close — Close a CURL session
curl_version — Returns the current CURL version
1>curl_init — Initialize a CURL session
Description
int curl_init ([string url])
The curl_init() function will initialize a new session and return a CURL handle for use by the curl_setopt(), curl_exec(), and curl_close() functions. If the optional parameter is provided, the CURLOPT_URL option will be set to the value of this parameter. You can set it manually using the curl_setopt() function.
Example 1. Initialize a new CURL session and retrieve a web page
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, “http://www.zhutiai.com/”);
curl_setopt ($ch, CURLOPT_HEADER, 0);
curl_exec ($ch);
curl_close ($ch);
?>