Failed to open stream: HTTP request failed

Source: Internet
Author: User

Fopen is a very good function to collect remote server content and save it to a local location. It can also open local files, next let's take a look at the solution to the failed to open stream: HTTP request failed problem when using the fopen function.

<? Php tutorial
$ 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.
?>

// The following code can be used for PHP 5 and later versions:

<? Php
$ Handle = fopen ("http://mb.bKjia. c0m", "rb ");
$ Contents = stream_get_contents ($ handle );
Fclose ($ handle );
Echo $ contents;
?>


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.

Use the php curl module to retrieve the PHP homepage and save it to a file.

<? Php
$ 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 initializes a new session and returns 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 is set to the value of this parameter. You can use the curl_setopt () function for manual settings.

Example 1. initialize a new CURL session and retrieve a webpage

<? Php

$ Ch = curl_init ();
Curl_setopt ($ ch, CURLOPT_URL, "http://www.zhutiai.com /");
Curl_setopt ($ ch, CURLOPT_HEADER, 0 );
Curl_exec ($ ch );
Curl_close ($ ch );
?>

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.