php fsockopen curl file_get_contents

1. 用 file_get_contents 以 get 方式获取内容:

1 // $url = 'http://www.nowamagic.net';
3 $html file_get_contents($url);
4 echo $html;

2. 用fopen打开url,以get方式获取内容

01 // $url = 'http://www.nowamagic.net';
03 $fp fopen($url'r');
04 stream_get_meta_data($fp);
05 $result '';
06 while(!feof($fp))
07 {
08     $result .= fgets($fp, 1024);
09 }
10 echo "url body: $result";
11 fclose($fp);

3. 用file_get_contents函数,以post方式获取url

这种方法我们之前点了一下,具体可以参考 stream_context_create()模拟POST/GET 这篇文章。

01 $data array(
02     'foo'=>'bar',
03     'baz'=>'boom',
04     'site'=>'www.nowamagic.net',
05     'name'=>'nowa magic');
06      
07 $data = http_build_query($data);
08  
09 //$postdata = http_build_query($data);
10 $options array(
11     'http' => array(
12         'method' => 'POST',
13         'header' => 'Content-type:application/x-www-form-urlencoded',
14         'content' => $data
15         //'timeout' => 60 * 60 // 超时时间(单位:s)
16     )
17 );
18  
20 $context = stream_context_create($options);
21 $result file_get_contents($url, false, $context);
22  
23 echo $result;

4. 用 fsockopen 函数打开url,以get方式获取完整的数据,包括header和body

这种方法在小节前面谈得很多了,这里不厌其烦地再列举一下:

01 // $url = 'http://www.nowamagic.net';
03 function get_url($url,$cookie=false)
04 {
05     $url parse_url($url);
06     $query $url['path']."?".$url['query'];
07     echo "Query:".$query;
08     $fp fsockopen$url['host'], $url['port']?$url['port']:80 , $errno,$errstr, 30);
09     if (!$fp)
10     {
11         return false;
12     }
13     else {
14         $request "GET $query HTTP/1.1\r\n";
15         $request .= "Host: $url[host]\r\n";
16         $request .= "Connection: Close\r\n";
17         if($cookie$request.="Cookie:   $cookie\n";
18         $request.="\r\n";
19         fwrite($fp,$request);
20         $result '';
21         while(!feof($fp))
22         {
23             $result .= @fgets($fp, 1024);
24         }
25         fclose($fp);
26         return $result;
27     }
28 }
29 //获取url的html部分,去掉header
30 function GetUrlHTML($url,$cookie=false)
31 {
32     $rowdata = get_url($url,$cookie);
33     if($rowdata)
34     {
35         $bodystristr($rowdata,"\r\n\r\n");
36         $body=substr($body,4,strlen($body));
37         return $body;
38     }
39  
40     return false;
41 }
42  
43 echo get_url($url);
44  
45 echo GetUrlHTML($url);

程序输出:

01 Query:/php/sock.php?site=nowamagic.netHTTP/1.1 200 OK
02 Date: Wed, 19 Feb 2014 06:06:25 GMT
03 Server: Apache/2.2.3 (CentOS)
04 X-Powered-By: PHP/5.3.3
05 Vary: Accept-Encoding
06 Content-Length: 21
07 Connection: close
08 Content-Type: text/html; charset=UTF-8
09  
10 Welcome to NowaMagic
11  
12 Query:/php/sock.php?site=nowamagic.net Welcome to NowaMagic

5. 用fsockopen函数打开url,以POST方式获取完整的数据,包括header和body

01 // $url = 'http://www.nowamagic.net';
03 function HTTP_Post($URL,$data,$cookie$referer="")
04 {
05  
06     // parsing the given URL
07     $URL_Info=parse_url($URL);
08  
09     // Building referrer
10     if($referer==""// if not given use this script as referrer
11         $referer="www.nowamagic.net";
12  
13     // making string from $data
14     foreach($data as $key=> $value)
15     $values[]="$key=".urlencode($value);
16     $data_string=implode("&",$values);
17  
18     // Find out which port is needed - if not given use standard (=80)
19     if(!isset($URL_Info["port"]))
20         $URL_Info["port"]=80;
21      
22     $request '';
23     // building POST-request:
24     $request.="POST ".$URL_Info["path"]." HTTP/1.1\n";
25     $request.="Host: ".$URL_Info["host"]."\n";
26     $request.="Referer: $referer\n";
27     $request.="Content-type: application/x-www-form-urlencoded\n";
28     $request.="Content-length: ".strlen($data_string)."\n";
29     $request.="Connection: close\n";
30  
31     $request.="Cookie:   $cookie\n";
32  
33     $request.="\n";
34     $request.=$data_string."\n";
35  
36     $fp fsockopen($URL_Info["host"],$URL_Info["port"]);
37     fputs($fp$request);
38     $result '';
39     while(!feof($fp))
40     {
41         $result .= fgets($fp, 1024);
42     }
43     fclose($fp);
44  
45     return $result;
46 }
47  
48 $data array(
49     'foo'=>'bar',
50     'baz'=>'boom',
51     'site'=>'www.nowamagic.net',
52     'name'=>'nowa magic');
53      
54 $cookie '';
55 $referer 'http://www.nowamagic.net/';
56      
57 echo HTTP_Post($url$data$cookie$referer);

程序输出:

01 HTTP/1.1 200 OK
02 Date: Wed, 19 Feb 2014 06:15:38 GMT
03 Server: Apache/2.2.3 (CentOS)
04 X-Powered-By: PHP/5.3.3
05 Vary: Accept-Encoding
06 Content-Length: 21
07 Connection: close
08 Content-Type: text/html; charset=UTF-8
09  
10 Welcome to NowaMagic

6. 使用curl库,使用curl库之前,可能需要查看一下php.ini是否已经打开了curl扩展。

使用 curl 代码比较简洁,代码也比较规范,容易理解:

01 // $url = 'http://www.nowamagic.net';
03 $ch = curl_init();
04 $timeout = 5;
05 curl_setopt ($ch, CURLOPT_URL, $url);
06 curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
07 curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
08 $file_contents = curl_exec($ch);
09 curl_close($ch);
10   
11 echo $file_contents;

fsockopen 是比较底层的调用,属于网络系统的socket调用,而curl经过的包装支持HTTPS认证,HTTP POST方法, HTTP PUT方法, FTP上传, kerberos认证,HTTP上传, 代理服务器, cookies, 用户名/密码认证, 下载文件断点续传,上载文件断点续传,http代理服务器管道( proxy tunneling), 甚至它还支持IPv6, socks5代理服务器,,通过http代理服务器上传文件到FTP服务器等等,功能十分强大。fsockopen 返回的是没有处理过的数据,包括数据的长度数据内容和数据的结束符。而curl是处理后的内容。

在用户使用时,curl 更加方便,但其参数很多,配置稍微复杂,fsockopen 则有固定的几个参数,简单,但获取结果可能需要再做处理。

那么file_get_contents呢?

有些时候用 file_get_contents() 调用外部文件容易超时报错。curl 效率比 file_get_contents() 和 fsockopen() 高一些,原因是CURL会自动对DNS信息进行缓存。

file_get_contents / curl / fsockopen 在当前所请求环境下选择性操作,没有一概而论。

file_get_contents 需要php.ini里开启allow_url_fopen,请求http时,使用的是http_fopen_wrapper,不会keeplive的话curl是可以的。 file_get_contents()单个执行效率高,返回没有头的信息。 

这个是读取一般文件的时候并没有什么问题,但是在读取远程问题的时候有可能就会出现问题。 如果是要打一个持续连接,多次请求多个页面。那么file_get_contents和fopen就会出问题。 取得的内容也可能会不对。所以做一些类似采集工作的时候,肯定就有问题了。 

fsockopen 较底层,可以设置基于UDP或是TCP协议去交互,配置麻烦,不易操作。 返回完整信息。 

总之,file_get_contents 和 curl 能干的,socket都能干。socket能干的,curl 就不一定能干了 。file_get_contents 更多的时候只是去拉取数据。效率比较高也比较简单。 

只讨论 curl 与file_get_contents 的话,有这么一些结论:

  1. fopen /file_get_contents 每次请求都会重新做DNS查询,并不对DNS信息进行缓存。但是CURL会自动对DNS信息进行缓存。对同一域名下的网页或者图片的请求只需要一次DNS查询。这大大减少了DNS查询的次数。所以CURL的性能比fopen /file_get_contents 好很多。
  2. fopen /file_get_contents在请求HTTP时,使用的是http_fopen_wrapper,不会keeplive。而curl却可以。这样在多次请求多个链接时,curl效率会好一些。
  3. fopen / file_get_contents函数会受到php.ini文件中allow_url_open选项配置的影响。如果该配置关闭了,则该函数也就失效了。而curl不受该配置的影响。
  4. curl可以模拟多种请求,例如:POST数据,表单提交等,用户可以按照自己的需求来定制请求。而fopen / file_get_contents只能使用get方式获取数据。

PS:file_get_contents()函数获取https链接内容的时候,需要php 中mod_ssl的支持(或安装opensll)。

结论就是,curl 效率及稳定都比 file_get_contents() 要好,fsockopen 也很强大,但是比较偏底层。

http://www.nowamagic.net/academy/detail/12220248
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值