PHP Open Read Page method _php Tutorial

Source: Internet
Author: User
Fopen Implementation code:

The fopen () function opens the file or URL.

If open fails, this function returns FALSE.


The code is as follows:

$handle = fopen ("http://www.example.com/", "RB");
$contents = "";
while (!feof ($handle)) {
$contents. = Fread ($handle, 8192);
}
Fclose ($handle);
?>

The code is as follows:
For PHP 5 and later
$handle = fopen ("http://www.example.com/", "RB");
$contents = Stream_get_contents ($handle);
Fclose ($handle);
?>

Parameters Description
FileName Necessary. Specifies the file or URL to open.
Mode Necessary. Specifies the type of access required to the file/stream. The possible values are shown in the following table.
Include_path Optional. If you also need to retrieve the file in Include_path, you can set the parameter to 1 or TRUE.
Context Optional. Specifies the environment for file handles. Context is a set of options that can modify the behavior of a stream.

Possible values for the mode parameter

mode description
"R" read-only opens, pointing the file pointer to the file header.
"r+" read-write mode opens, pointing the file pointer to the file header.
"W" write open, pointing the file pointer to the file header and truncating the file size to zero. If the file does not exist, try to create it.
"w+" read-write mode opens, pointing the file pointer at the file header and truncating the file size to zero. If the file does not exist, try to create it.
"A" write open, pointing the file pointer to the end of the file. If the file does not exist, try to create it.
"A +" read-write mode opens, pointing the file pointer at the end of the file. If the file does not exist, try to create it. The
"x"

is created and opened as a write, pointing the file pointer to the file header. If the file already exists, the fopen () call fails and returns false, and generates an E_warning level error message. If the file does not exist, try to create it.

This and specify o_excl| for the underlying open (2) system call The o_creat tag is equivalent.

This option is supported by PHP 4.3.2 and later versions and can only be used on local files. The

"x+"

is created and opened as read-write, pointing the file pointer to the file header. If the file already exists, the fopen () call fails and returns false, and generates an E_warning level error message. If the file does not exist, try to create it.

This and specify o_excl| for the underlying open (2) system call The o_creat tag is equivalent.

This option is supported by PHP 4.3.2 and later versions and can only be used on local files.

2.curl Implementation code:
The code is as follows:

function _url ($Date) {
$ch = Curl_init ();
$timeout = 5;
curl_setopt ($ch, Curlopt_url, "$Date");
curl_setopt ($ch, Curlopt_returntransfer, 1);
curl_setopt ($ch, Curlopt_useragent, "mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) ");
curl_setopt ($ch, Curlopt_connecttimeout, $timeout);
$contents = curl_exec ($ch);
Curl_close ($ch);
return $contents;
}
$pageURL = "http://www.baidu.com";
$contents =_url ($pageURL);
?>

curl_close-closing a Curl session
curl_copy_handle-Copy all the contents and parameters of a Curl connection resource
curl_errno-returns a numeric number that contains the current session error information
curl_error-returns a string containing the current session error message
Curl_exec-performing a Curl session
Curl_getinfo-gets the information for a Curl connection resource handle
curl_init-initialization of a curl session
curl_multi_add_handle-Adding a separate curl handle resource to a curl batch session
curl_multi_close-closing a batch handle resource
curl_multi_exec-parsing a Curl batch handle
curl_multi_getcontent-returns the text stream of the obtained output
Curl_multi_info_read-Gets the related transfer information for the currently resolved curl
curl_multi_init-initializing a curl batch handle resource
curl_multi_remove_handle-removing a handle resource from the Curl batch handle resource
Curl_multi_select-get all the sockets associated with the CURL extension, which can and be "selected"
curl_setopt_array-set session parameters as an array for a curl
curl_setopt-setting session parameters for a curl
curl_version-getting the version information about Curl
The Curl_init () function Initializes a curl session, and the only parameter to the Curl_init () function is optional, representing a URL address.
The function of the curl_exec () function is to perform a curl session, and the only argument is the handle returned by the Curl_init () function.
The function of the Curl_close () function is to close a curl session, and the only argument is the handle returned by the Curl_init () function.

Encoding Conversion functions
The code is as follows:

$html = file_get_contents (http://mb.php100.com);
$html = Iconv ("Big5", "Utf-8//ignore", $html); Conversion encoding mode is UTF8
Print $html;
$htm = File ("Http://s.jb51.net");
$h = "";
foreach ($htm as $value)
{
$h. = Iconv ("GB2312", "Utf-8//ignore", $value);
}
Print_r ($h);

file_get_contents (path,include_path,context,start,max_length)

Parameters Description
Path Necessary. Specifies the file to be read.
Include_path Optional. If you also want to search for files in include_path, you can set this parameter to "1".
Context

Optional. Specifies the environment for file handles.

The context is a set of options that modify the behavior of the stream. If NULL is used, it is ignored.

Start Optional. Specifies the location to begin reading in the file. This parameter is new in PHP 5.1.
Max_length Optional. Specifies the number of bytes to read. This parameter is new in PHP 5.1.

Another way to open a Web page
The code is as follows:

$opts = Array (
' HTTP ' =>array (
' Method ' = ' GET ',
' Header ' = ' accept-language:enrn '.
"Cookie:foo=barrn"
)
);
$context = Stream_context_create ($opts);
/* Sends an HTTP request to www.zhutiai.com

With additional headers shown above */
$fp = fopen (' http://www.baidu.com ', ' R ', false, $context);
Fpassthru ($FP);
Fclose ($FP);
?>


http://www.bkjia.com/PHPjc/444761.html www.bkjia.com true http://www.bkjia.com/PHPjc/444761.html techarticle fopen Implementation code: the fopen () function opens the file or URL. If open fails, this function returns FALSE. The code is as follows: PHP tutorial $handle = fopen (http://www.example.com/, RB); $co ...

  • 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.