我是靠谱客的博主 英俊枕头,这篇文章主要介绍PHP获取远程文件的几种方式fopenfile_get_cententssocketcURL,现在分享给大家,希望可以做个参考。


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

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<?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

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<?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-urlencodedrn" . "Content-length: $content_lengthrn", '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

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?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.0rn"; $head .= "Host: ".$info['host']."rn"; $head .= "rn"; $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.0rn"; $head .= "Host: ".$info['host']."rn"; $head .= "Referer: http://".$info['host'].$info['path']."rn"; $head .= "Content-type: application/x-www-form-urlencodedrn"; $head .= "Content-Length: ".strlen(trim($query))."rn"; $head .= "rn"; $head .= trim($query); $write = fputs($fp, $head); while (!feof($fp)) { $line = fgets($fp); echo $line."<br>"; } ?>

cURL

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<?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

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<?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的网络爬虫中都离不开它。

最后

以上就是英俊枕头最近收集整理的关于PHP获取远程文件的几种方式fopenfile_get_cententssocketcURL的全部内容,更多相关PHP获取远程文件内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(59)

评论列表共有 0 条评论

立即
投稿
返回
顶部