Chinaunix首页 | 论坛 | 博客
  • 博客访问: 5405093
  • 博文数量: 763
  • 博客积分: 12108
  • 博客等级: 上将
  • 技术积分: 15717
  • 用 户 组: 普通用户
  • 注册时间: 2007-09-28 21:21
个人简介

业精于勤,荒于嬉

文章分类

全部博文(763)

文章存档

2018年(6)

2017年(15)

2016年(2)

2015年(31)

2014年(14)

2013年(87)

2012年(75)

2011年(94)

2010年(190)

2009年(38)

2008年(183)

2007年(28)

分类: PHP

2013-05-06 15:40:08

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

  1. $url='';  
  2. $html = file_get_contents($url);  
  3. echo $html;  
  4. ?>  


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

  1. $fp = fopen($url'r');  
  2. //返回请求流信息(数组:请求状态,阻塞,返回值是否为空,返回值http头等)  
  1. stream_get_meta_data($fp);  
  1. while(!feof($fp)) {  
  2. $result .= fgets($fp, 1024);  
  3. }  
  4. echo "url body: $result";  
  5. fclose($fp);  
  6. ?>  


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

  1. $data = array ('foo' => 'bar');  
  1. //生成url-encode后的请求字符串,将数组转换为字符串  
  2. $data = http_build_query($data);  
  3. $opts = array (  
  4. "white-space:pre">  'http' => array (  
  5. "white-space:pre">      'method' => 'POST',  
  6. "white-space:pre">      'header'=> "Content-type: application/x-www-form-urlencoded\r\n" .  
  7. "white-space:pre">      "Content-Length: " . strlen($data) . "\r\n",  
  8. "white-space:pre">      'content' => $data  
  9. "white-space:pre">  )  
  10. );  
  1. //生成请求的句柄文件  
  2. $context = stream_context_create($opts);  
  3. $html = file_get_contents('', false, $context);  
  4. echo $html;  
  5. ?>  


方法4:用fsockopen函数打开url,以get方式获取完整的数据,包括header和body,fsockopen需要 PHP.ini 中 allow_url_fopen 选项开启

  1. function get_url ($url,$cookie=false)  
  2. {  
  3. $url = parse_url($url);  
  4. $query = $url[path]."?".$url[query];  
  5. echo "Query:".$query;  
  6. $fp = fsockopen$url[host], $url[port]?$url[port]:80 , $errno$errstr, 30);  
  7. if (!$fp) {  
  8. return false;  
  9. else {  
  10. $request = "GET $query HTTP/1.1\r\n";  
  11. $request .= "Host: $url[host]\r\n";  
  12. $request .= "Connection: Close\r\n";  
  13. if($cookie$request.="Cookie:   $cookie\n";  
  14. $request.="\r\n";  
  15. fwrite($fp,$request);  
  16. while()) {  
  17. $result .= @fgets($fp, 1024);  
  18. }  
  19. fclose($fp);  
  20. return $result;  
  21. }  
  22. }  
  23. //获取url的html部分,去掉header  
  24. function GetUrlHTML($url,$cookie=false)  
  25. {  
  26. $rowdata = get_url($url,$cookie);  
  27. if($rowdata)  
  28. {  
  29. $bodystristr($rowdata,"\r\n\r\n");  
  30. $body=substr($body,4,strlen($body));  
  31. return $body;  
  32. }  
  33.     return false;  
  34. }  
  35. ?>  


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

  1. function HTTP_Post($URL,$data,$cookie$referrer="")  
  2. {  
  3.     // parsing the given URL  
  4. $URL_Info=parse_url($URL);  
  5.     // Building referrer  
  6. if($referrer==""// if not given use this script as referrer  
  7. $referrer="111";  
  8.     // making string from $data  
  9. foreach($data as $key=>$value)  
  10. $values[]="$key=".urlencode($value);  
  11. $data_string=implode("&",$values);  
  12.     // Find out which port is needed - if not given use standard (=80)  
  13. if(!isset($URL_Info["port"]))  
  14. $URL_Info["port"]=80;  
  15.     // building POST-request:  
  16. $request.="POST ".$URL_Info["path"]." HTTP/1.1\n";  
  17. $request.="Host: ".$URL_Info["host"]."\n";  
  18. $request.="Referer: $referer\n";  
  19. $request.="Content-type: application/x-www-form-urlencoded\n";  
  20. $request.="Content-length: ".strlen($data_string)."\n";  
  21. $request.="Connection: close\n";  
  22.     $request.="Cookie:   $cookie\n";  
  23.     $request.="\n";  
  24. $request.=$data_string."\n";  
  25.     $fp = fsockopen($URL_Info["host"],$URL_Info["port"]);  
  26. fputs($fp$request);  
  27. while(!feof($fp)) {  
  28. $result .= fgets($fp, 1024);  
  29. }  
  30. fclose($fp);  
  31.     return $result;  
  32. }  
  33. ?>  


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

  1. $ch = curl_init();  
  2. $timeout = 5;  
  3. curl_setopt ($ch, CURLOPT_URL, '');  
  4. curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);  
  5. curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);  
  6. $file_contents = curl_exec($ch);  
  7. curl_close($ch);  
  8. echo $file_contents;  
  9. ?>  
阅读(1604) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~