Home > Backend Development > PHP Tutorial > php open method of reading web page_PHP tutorial

php open method of reading web page_PHP tutorial

WBOY
Release: 2016-07-20 11:09:49
Original
1253 people have browsed it

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 是可以修改流的行为的一套选项。

Possible values ​​of the mode parameter

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"
mode 说明
"r" 只读方式打开,将文件指针指向文件头。
"r+" 读写方式打开,将文件指针指向文件头。
"w" 写入方式打开,将文件指针指向文件头并将文件大小截为零。如果文件不存在则尝试创建之。
"w+" 读写方式打开,将文件指针指向文件头并将文件大小截为零。如果文件不存在则尝试创建之。
"a" 写入方式打开,将文件指针指向文件末尾。如果文件不存在则尝试创建之。
"a+" 读写方式打开,将文件指针指向文件末尾。如果文件不存在则尝试创建之。
"x"

创建并以写入方式打开,将文件指针指向文件头。如果文件已存在,则 fopen() 调用失败并返回 FALSE,并生成一条 E_WARNING 级别的错误信息。如果文件不存在则尝试创建之。

这和给底层的 open(2) 系统调用指定 O_EXCL|O_CREAT 标记是等价的。

此选项被 PHP 4.3.2 以及以后的版本所支持,仅能用于本地文件。

"x+"

创建并以读写方式打开,将文件指针指向文件头。如果文件已存在,则 fopen() 调用失败并返回 FALSE,并生成一条 E_WARNING 级别的错误信息。如果文件不存在则尝试创建之。

这和给底层的 open(2) 系统调用指定 O_EXCL|O_CREAT 标记是等价的。

此选项被 PHP 4.3.2 以及以后的版本所支持,仅能用于本地文件。

Create and open for writing, 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.
"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
参数 描述
path 必需。规定要读取的文件。
include_path 可选。如果也想在 include_path 中搜寻文件的话,可以将该参数设为 "1"。
context

可选。规定文件句柄的环境。

context 是一套可以修改流的行为的选项。若使用 null,则忽略。

start 可选。规定在文件中开始读取的位置。该参数是 PHP 5.1 新加的。
max_length 可选。规定读取的字节数。该参数是 PHP 5.1 新加的。
Optional. Specifies the environment for a file handle. Context is a set of options that can modify the behavior of the stream. If null is used, it is ignored.
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.com

with additional headers shown above */
$fp = fopen('http ://www.baidu.com', 'r', false, $context);
fpassthru($fp);
fclose($fp);
?>


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/444761.htmlTechArticlefopen implementation code: fopen() function opens a file or URL. If the opening fails, this function returns FALSE. The code is as follows: ?php tutorial $handle = fopen (http://www.example.com/, rb); $co...
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