PHP头条
热点:

PHP获取远程URL的实例讲解


今天我们向大家重点讲解的是PHP获取远程URL的具体方法,我们通过六种例子,具体总结了它获取内容的方法,让大家对这几种方法有一个深刻的了解。

  • PHP5指针的类别介绍
  • PHP5面向对象的一些问题反映
  • PHP 5.3闭包语法的具体讲解
  • PHP 5.0构造函数的实例讲解
  • PHP程序员最容易出现的错误总结
示例代码1: 用file_get_contents 以get方式实现PHP获取远程URL

  1. <?php 
  2. $url='http://www.baidu.com/';  
  3. $html=file_get_contents($url);  
  4. //print_r($http_response_header);  
  5. ec($html);  
  6. printhr();  
  7. printarr($http_response_header);  
  8. printhr();  
  9. ?> 

示例代码2: 用fopen打开url, 以get方式获取内容

  1. <? 
  2. $fp=fopen($url,'r');  
  3. printarr(stream_get_meta_data($fp));  
  4. printhr();  
  5. while(!feof($fp)){  
  6. $result.=fgets($fp,1024);  
  7. }  
  8. echo"url body: $result";  
  9. printhr();  
  10. fclose($fp);  
  11. ?> 

示例代码3:用file_get_contents函数,以post方式进行PHP获取远程URL

  1. <?php 
  2. $data=array('foo'=>'bar');  
  3. $data=http_build_query($data);  
  4. $opts=array(  
  5. 'http'=>array(  
  6. 'method'=>'POST',  
  7. 'header'=>"Content-type: application/x-www-form-urlencoded\r\n".  
  8. "Content-Length: ".strlen($data)."\r\n",  
  9. 'content'=>$data  
  10. ),  
  11. );  
  12. $context=stream_context_create($opts);  
  13. $html=file_get_contents('http://localhost/e/admin/test.html',false,$context);  
  14. echo$html;  
  15. ?> 

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

  1. <? 
  2. functionget_url($url,$cookie=false){  
  3. $url=parse_url($url);  
  4. $query=$url[path]."?".$url[query];  
  5. ec("Query:".$query);  
  6. $fp=fsockopen($url[host],$url[port]?$url[port]:80,$errno,$errstr,30);  
  7. if(!$fp){  
  8. returnfalse;  
  9. }else{  
  10. $request="GET$queryHTTP/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(!@feof($fp)){  
  17. $result.=@fgets($fp,1024);  
  18. }  
  19. fclose($fp);  
  20. return$result;  
  21. }  
  22. }  
  23. //获取url的html部分,去掉header  
  24. functionGetUrlHTML($url,$cookie=false){  
  25. $rowdata=get_url($url,$cookie);  
  26. if($rowdata)  
  27. {  
  28. $body=stristr($rowdata,"\r\n\r\n");  
  29. $body=substr($body,4,strlen($body));  
  30. return$body;  
  31. }  
  32. returnfalse;  
  33. }  
  34. ?> 

PHP获取远程URL示例代码5:用fsockopen函数打开url,以POST方式获取完整的数据,包括header和body

  1. <? 
  2. functionHTTP_Post($URL,$data,$cookie,$referrer=""){  
  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($dataas$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. printhr();  
  34. ?> 

PHP获取远程URL示例代码6:使用curl库,使用curl库之前,你可能需要查看一下php.ini,查看是否已经打开了curl扩展

  1. <? 
  2. $ch = curl_init();  
  3. $timeout = 5;  
  4. curl_setopt ($ch, CURLOPT_URL, 'http://www.baidu.com/');  
  5. curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);  
  6. curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);  
  7. $file_contents = curl_exec($ch);  
  8. curl_close($ch);  
  9. echo $file_contents;  
  10. ?> 

文章来源:http://hi.baidu.com/lqkailin/blog/item/f2dc2d1a212e670d34fa416f.html

www.phpzy.comtrue/php/15867.htmlTechArticlePHP获取远程URL的实例讲解 今天我们向大家重点讲解的是PHP获取远程URL的具体方法,我们通过六种例子,具体总结了它获取内容的方法,让大家对这几种方法有一个深刻的了解。 PHP5指针...

相关文章

    暂无相关文章
相关频道:

PHP之友评论

今天推荐