Fopen implementation code:
The fopen () function opens a file or URL.
If opening fails, this function returns FALSE.
The Code is as follows:
<? Php tutorial
$ Handle = fopen ("http://www.example.com/", "rb ");
$ Contents = "";
While (! Feof ($ handle )){
$ Contents. = fread ($ handle, 8192 );
}
Fclose ($ handle );
?>
The Code is as follows:
<? Php
// For PHP 5 and later versions
$ Handle = fopen ("http://www.example.com/", "rb ");
$ Contents = stream_get_contents ($ handle );
Fclose ($ handle );
?>
| Parameters |
Description |
| Filename |
Required. Specifies the file or URL to open. |
| Mode |
Required. Specifies the access type of the file/stream. Possible values are shown in the table below. |
| Include_path |
Optional. If you also need to retrieve the file in include_path, you can set this parameter to 1 or TRUE. |
| Context |
Optional. Specifies the file handle environment. Context is a set of options that can modify the behavior of a stream. |
Possible value of the mode Parameter
| Mode |
Description |
| "R" |
Open the file in read-only mode and point the file pointer to the file header. |
| "R +" |
Open in read/write mode and point the file pointer to the file header. |
| "W" |
Open in writing mode, point the file pointer to the file header, and cut the file size to zero. If the file does not exist, try to create it. |
| "W +" |
Open in read/write mode, point the file pointer to the file header, and cut the file size to zero. If the file does not exist, try to create it. |
| "" |
Open in writing mode and point the file pointer to the end of the file. If the file does not exist, try to create it. |
| "A +" |
Open in read/write mode and point the file pointer to the end of the file. If the file does not exist, try to create it. |
| "X" |
Create and open the file in writing mode, and point the file pointer to the file header. If the file already exists, fopen () fails to be called, returns FALSE, and generates an E_WARNING-level error message. If the file does not exist, try to create it. This is equivalent to specifying the O_EXCL | O_CREAT mark for the underlying open (2) System Call. This option is supported by PHP 4.3.2 and later versions and can only be used for local files. |
| "X +" |
Create and open the file in read/write mode, and point the file pointer to the file header. If the file already exists, fopen () fails to be called, returns FALSE, and generates an E_WARNING-level error message. If the file does not exist, try to create it. This is equivalent to specifying the O_EXCL | O_CREAT mark for the underlying open (2) System Call. This option is supported by PHP 4.3.2 and later versions and can only be used for local files. |
2. curl implementation code:
The Code is as follows:
<? Php
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-close a curl session
Curl_copy_handle-copy all content and parameters of a curl connection Resource
Curl_errno-A number containing the current session error message is returned.
Curl_error-returns a string containing the current session error message.
Curl_exec-execute a curl session
Curl_getinfo-obtains the information of a curl connection resource handle.
Curl_init-initialize a curl session
Curl_multi_add_handle-Add a separate curl handle resource to the curl batch processing session
Curl_multi_close-close a batch processing handle Resource
Curl_multi_exec-parse a curl batch handle
Curl_multi_getcontent-return the obtained output text stream
Curl_multi_info_read-obtains the transmission information of the currently resolved curl.
Curl_multi_init-initialize a curl batch processing handle Resource
Curl_multi_remove_handle-remove a handle resource from the curl batch processing handle.
Curl_multi_select-Get all the sockets associated with the cURL extension, which can then be "selected"
Curl_setopt_array-set session parameters for a curl in the form of an array
Curl_setopt-set session parameters for a curl
Curl_version-obtain curl-related version information
The function curl_init () initializes a curl session. The unique parameter of the curl_init () function is optional, indicating a url address.
The role of the curl_exec () function is to execute a curl session. The unique parameter is the handle returned by the curl_init () function.
The function curl_close () is used to close a curl session. The only parameter 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); // convert the encoding method to 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 |
Required. Specifies the file to be read. |
| Include_path |
Optional. If you want to search for files in include_path, you can set this parameter to "1 ". |
| Context |
Optional. Specifies the file handle environment. Context is a set of options that can modify the behavior of a stream. If null is used, this parameter is ignored. |
| Start |
Optional. Specifies the location where the file starts reading. This parameter is newly added in PHP 5.1. |
| Max_length |
Optional. The number of bytes to read. This parameter is newly added in PHP 5.1. |
Another way to open a webpage
The Code is as follows:
<? Php
$ 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 abve */
$ Fp = fopen ('HTTP: // www.baidu.com ', 'R', false, $ context );
Fpassthru ($ fp );
Fclose ($ fp );
?>