fopen implementation code:
fopen() function opens a file or URL.
If the opening 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 above
$handle = fopen("http://www .example.com/", "rb");
$contents = stream_get_contents($handle);
fclose($handle);
?>
参数 | 描述 |
---|---|
filename | 必需。规定要打开的文件或 URL。 |
mode | 必需。规定要求到该文件/流的访问类型。可能的值见下表。 |
include_path | 可选。如果也需要在 include_path 中检索文件的话,可以将该参数设为 1 或 TRUE。 |
context | 可选。规定文件句柄的环境。Context 是可以修改流的行为的一套选项。 |
mode | Description | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
"r" | Open in read-only mode, pointing 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 truncate 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 truncate the file size to zero. If the file does not exist, try to create it. | ||||||||||||||||||
"a" | 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" |
| ||||||||||||||||||
"x+" | Create and open in read-write mode, 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 is equivalent to specifying the O_EXCL|O_CREAT flag to the underlying open(2) system call. This option is supported by PHP 4.3.2 and later, and can only be used for 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 — close a curl session
curl_copy_handle — copy all contents and parameters of a curl connection resource
curl_errno — return a numeric number containing the current session error message
curl_error — return a numeric number containing the current session error message string
curl_exec — Execute a curl session
curl_getinfo — Get information about a curl connection resource handle
curl_init — Initialize a curl session
curl_multi_add_handle — Add individual curl handles to a curl batch session Resources
curl_multi_close — Close a batch handle resource
curl_multi_exec — Parse a curl batch handle
curl_multi_getcontent — Return the text stream of the obtained output
curl_multi_info_read — Get the relevant transmission information of the currently parsed curl
curl_multi_init — Initialize a curl batch handle resource
curl_multi_remove_handle — Remove a handle resource in the curl batch handle resource
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 — Get curl-related version information
The role of the curl_init() function is to initialize a curl Session, the only parameter of the curl_init() function is optional and represents a url address.
The curl_exec() function is used to execute a curl session. The only parameter is the handle returned by the curl_init() function.
The curl_close() function is used to close a curl session. The only parameter is the handle returned by the curl_init() function.
Encoding conversion function
The code is as follows:
$html = file_get_contents(http://mb.php100.com);
$html = iconv( "Big5", " UTF-8//IGNORE" , $html); //Convert encoding 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)
Parameter | Description | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
path | Required. 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 |
| ||||||||||||
start | Optional. Specifies the position in the file to begin reading. 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 webpage
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.comwith additional headers shown above */
$fp = fopen('http ://www.baidu.com', 'r', false, $context);
fpassthru($fp);
fclose($fp);
?>