Home > Backend Development > PHP Tutorial > failed to open stream: HTTP request failed_PHP教程

failed to open stream: HTTP request failed_PHP教程

WBOY
Release: 2016-07-13 10:54:50
Original
1248 people have browsed it

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);
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632285.htmlTechArticlefopen can collect the content of the remote server and save it locally. It can also open local files. It is a very good tool. function, let’s take a look at what happens when using the fopen function...
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