layout: post
title: "PHP获取远程文件的几种方式"
date: 2016-05-18 14:18:15 +0800
comments: true
categories: [php]
接上次的内容,今天结合代码来分析下PHP获取远程文件的几种方式。
- fopen
- 获取本地文件
- 获取到的资源绑定到一个流上面
- file_get_centents
- 获取一些Get获得的数据,也可以POST,
- 可以做断点续传
- socket
- curl
- 可以保存cookie 并且在下一次发送出去
- 做模拟登陆
fopen
<?php
require_once('common.php');
echo "<h2>fopen获取文件测试</h2>";
/*fopen打开本地文件*/
$filename="test.html";
$fp=fopen($filename,"r+");//r+ 只读,+表示如果不存在则创建
$fc="";
while(!feof($fp)){
$fc.=fgets($fp,1024);//获取到的资源绑定到流上。这样来获取流数据
}
fclose($fp);
echo "<h4>输出http_response_header</h4>";
dumpout($http_response_header);
echo "<h3>本地文件:".$fc."</h3>";
/*fopen打开远程url*/
$filename="http://localhost/Login/02/test.html";
$fp=fopen($filename,"r");
echo "<h4>输出stream_get_meta_data</h4>";
dumpArr(stream_get_meta_data($fp));
$fc="";
while(!feof($fp)){
$fc=fgets($fp,1024);
}
fclose($fp);
echo "<h4>输出http_response_header</h4>";
dumpout($http_response_header);
echo "<h3>远程url:".$fc."</h3>";
?>
file_get_centents
<?php
require_once('common.php');
echo "<h2>file_get_contents获取文件测试</h2>";
/*file_get_contents打开本地文件*/
$filename="test.html";
$fc=file_get_contents($filename);
echo "<h3>本地文件:".$fc."</h3>";
/*fopen打开远程url get方法*/
$filename="http://localhost/Login/02/test.php";
$fc=file_get_contents($filename);
echo "<h4>输出http_response_header</h4>";
dumpout($http_response_header);
echo "<h3>get方法获取远程url:".$fc."</h3>";
/*fopen打开远程url post方法*/
$filename="http://localhost/Login/02/test.php";
/*构建请求头信息*/
$post = array ('type' => '1');
$content = http_build_query($post);
$content_length = strlen($content);
$options = array(
'http' => array(
'method' => 'POST',
'header' =>
"Content-type: application/x-www-form-urlencoded\r\n" .
"Content-length: $content_length\r\n",
'content' => $content
)
);
$fc = file_get_contents($filename, false, stream_context_create($options));
echo "<h4>输出http_response_header</h4>";
dumpout($http_response_header);
echo "<h3>post方法获取远程url:".$fc."</h3>";
?>
socket
<?php
require_once('common.php');
/*Accept: */
/* Accept-Language: zh-cn,en-us;q=0.5
Content-Type: application/x-www-form-urlencoded
/*模拟浏览器信息*/
User-Agent: Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; .NET CLR 1.0.3705; .NET CLR 1.1.4322)
Host: 要发送到的主机地址
Content-Length: 发送数据的长度
Pragma: no-cache
Cache-Control: no-cache
username=php&password=iask //post发送的数据
*
*/
/*fsocket模拟get提交*/
echo "<h2>fsocket模拟get提交</h2>";
$url="http://localhost/Login/02/test.php?type=1";
$info = parse_url($url);
$fp = fsockopen($info["host"], 80, $errno, $errstr, 3);
$head = "GET ".$info['path']."?".$info["query"]." HTTP/1.0\r\n";
$head .= "Host: ".$info['host']."\r\n";
$head .= "\r\n";
$write = fputs($fp, $head);
while (!feof($fp)){
$line = fgets($fp);
echo $line."<br>";
}
/*fsocket模拟post提交*/
echo "<h2>fsocket模拟post提交</h2>";
$query="type=1";
$info = parse_url($url);
$fp = fsockopen($info["host"], 80, $errno, $errstr, 3);
$head = "POST ".$info['path']." HTTP/1.0\r\n";
$head .= "Host: ".$info['host']."\r\n";
$head .= "Referer: http://".$info['host'].$info['path']."\r\n";
$head .= "Content-type: application/x-www-form-urlencoded\r\n";
$head .= "Content-Length: ".strlen(trim($query))."\r\n";
$head .= "\r\n";
$head .= trim($query);
$write = fputs($fp, $head);
while (!feof($fp)) {
$line = fgets($fp);
echo $line."<br>";
}
?>
cURL
<?php
require_once('common.php');
/*curl模拟get提交*/
echo "<h2>curl模拟get提交</h2>";
$url="http://localhost/Login/02/test.php?type=1";
//初始化
$ch=curl_init();
//设置选项,包括URL
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
//执行并获取HTML文档内容
$output = curl_exec($ch);
echo "<h2>输出curl_getinfo相关信息</h2>";
$info = curl_getinfo($ch);
dumpArr($info);
//释放curl句柄
curl_close($ch);
//打印获得的数据
print_r($output);
echo "<h2>curl模拟post提交</h2>";
$url="http://localhost/Login/02/test.php";
$post_data = array ("type" => "1");
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// post数据
curl_setopt($ch, CURLOPT_POST, 1);
// post的变量
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
$output = curl_exec($ch);
echo "<h2>输出curl_getinfo相关信息</h2>";
$info = curl_getinfo($ch);
dumpArr($info);
curl_close($ch);
//打印获得的数据
print_r($output);
?>
上面的文件引用的common.php
<?php
header("Content-type:text/html;charset:utf-8");
/*获取当前访问用户IP*/
function GetIP(){
if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown")){
$ip = getenv("HTTP_CLIENT_IP");
}
else if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown")) {
$ip = getenv("HTTP_X_FORWARDED_FOR");
}
else if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown")) {
$ip = getenv("REMOTE_ADDR");
}
else if (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown")) {
$ip = $_SERVER['REMOTE_ADDR'];
}
else {
$ip = "unknown";
}
return($ip);
}
/*格式化header*/
function parseHeaders( $headers ){
$head = array();
foreach( $headers as $k=>$v ){
$t = explode( ':', $v, 2 );
if( isset( $t[1] ) ){
$head[ trim($t[0]) ] = trim( $t[1] );
}
else{
$head[] = $v;
if( preg_match( "#HTTP/[0-9\.]+\s+([0-9]+)#",$v, $out ) ){
$head['reponse_code'] = intval($out[1]);
}
}
}
return $head;
}
/*数组格式化输出header*/
function dumpout($vars, $label = '', $return = false) {
$vars=parseHeaders($vars);
if (ini_get('html_errors')) {
$content = "<pre>\n";
if ($label != '') {
$content .= "<strong>{$label} :</strong>\n";
}
$content .= htmlspecialchars(print_r($vars, true));
$content .= "\n</pre>\n";
} else {
$content = $label . " :\n" . print_r($vars, true);
}
if ($return) { return $content; }
echo $content;
return null;
}
/*数组格式化输出arr*/
function dumpArr($vars, $label = '', $return = false) {
if (ini_get('html_errors')) {
$content = "<pre>\n";
if ($label != '') {
$content .= "<strong>{$label} :</strong>\n";
}
$content .= htmlspecialchars(print_r($vars, true));
$content .= "\n</pre>\n";
} else {
$content = $label . " :\n" . print_r($vars, true);
}
if ($return) { return $content; }
echo $content;
return null;
}
?>
以上几种方式用得较为多的是cURl,因为它可以保存COOKIE的特性,在PHP的网络爬虫中都离不开它。
网友评论