PHP头条
热点:

PHP发送HTTP请求的6种方法,知道4种算你牛!,6种4种


 PHP发送HTTP请求的6种方法,知道4种算你牛

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

  1. <?php 
  2.  
  3. $url='https://wenda.shukaiming.com/'; 
  4.  
  5. echo file_get_contents($url); 
  6.  
  7. ?>  

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

  1. <?php 
  2.  
  3. //r标识read,即标识只读 
  4.  
  5. $fp = fopen($url, 'r'); 
  6.  
  7. stream_get_meta_data($fp); 
  8.  
  9. while(!feof($fp)) { 
  10.  
  11. $body.= fgets($fp, 1024); 
  12.  
  13.  
  14. echo $body; 
  15.  
  16. fclose($fp); 
  17.  
  18. ?> 

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

  1. <?php 
  2.  
  3. $data = array (‘foo' => ‘bar'); 
  4.  
  5. $data = http_build_query($data); 
  6.  
  7. $opts = array ( 
  8.  
  9. 'http' => array ( 
  10.  
  11. 'method' => 'POST', 
  12.  
  13. 'header'=> 'Content-type: application/x-www-form-urlencodedrn' . 
  14.  
  15. 'Content-Length: ' . strlen($data) . '\r\n', 
  16.  
  17. 'content' => $data 
  18.  
  19.  
  20. ); 
  21.  
  22. $context = stream_context_create($opts); 
  23.  
  24. $html = file_get_contents('https://wenda.shukaming.com', false, $context); 
  25.  
  26. echo $html; 
  27.  
  28. ?>  

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

  1. <?php 
  2.  
  3. function get_url ($url,$cookie=false) 
  4.  
  5.  
  6. $url = parse_url($url); 
  7.  
  8. $query = $url[path].”?”.$url[query]; 
  9.  
  10. echo $query; 
  11.  
  12. $fp = fsockopen( $url[host], $url[port]?$url[port]:80 , $errno, $errstr, 30); 
  13.  
  14. if (!$fp) { 
  15.  
  16. return false; 
  17.  
  18. } else { 
  19.  
  20. $request = "GET $query HTTP/1.1\r\n"; 
  21.  
  22. $request .= "Host: $url[host]\r\n"; 
  23.  
  24. $request .= "Connection: Close\r\n"; 
  25.  
  26. if($cookie) $request.="Cookie:  $cookien"; 
  27.  
  28. $request.="\r\n"; 
  29.  
  30. fwrite($fp,$request); 
  31.  
  32. while(!@feof($fp)) { 
  33.  
  34. $result .= @fgets($fp, 1024); 
  35.  
  36.  
  37. fclose($fp); 
  38.  
  39. return $result; 
  40.  
  41.  
  42.  
  43. //获取url的html部分,去掉header 
  44.  
  45. function GetUrlHTML($url,$cookie=false) 
  46.  
  47.  
  48. $rowdata = get_url($url,$cookie); 
  49.  
  50. if($rowdata) 
  51.  
  52.  
  53. $body= stristr($rowdata,”\r\n\r\n”); 
  54.  
  55. $body=substr($body,4,strlen($body)); 
  56.  
  57. return $body; 
  58.  
  59.  
  60. return false; 
  61.  
  62.  
  63. ?>  

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

  1. <?php 
  2.  
  3. function HTTP_Post($URL,$data,$cookie, $referrer="") 
  4.  
  5.  
  6. // parsing the given URL 
  7.  
  8. $URL_Info=parse_url($URL); 
  9.  
  10. // Building referrer 
  11.  
  12. if($referrer=="") // if not given use this script as referrer 
  13.  
  14. $referrer="111"; 
  15.  
  16. // making string from $data 
  17.  
  18. foreach($data as $key=>$value) 
  19.  
  20. $values[]="$key=".urlencode($value); 
  21.  
  22. $data_string=implode("&",$values); 
  23.  
  24. // Find out which port is needed – if not given use standard (=80) 
  25.  
  26. if(!isset($URL_Info["port"])) 
  27.  
  28. $URL_Info["port"]=80; 
  29.  
  30. // building POST-request: 
  31.  
  32. $request.="POST ".$URL_Info["path"]." HTTP/1.1\n"; 
  33.  
  34. $request.="Host: ".$URL_Info["host"]."\n"; 
  35.  
  36. $request.="Referer: $referer\n"; 
  37.  
  38. $request.="Content-type: application/x-www-form-urlencodedn"; 
  39.  
  40. $request.="Content-length: ".strlen($data_string)."\n"; 
  41.  
  42. $request.="Connection: closen"; 
  43.  
  44. $request.="Cookie:  $cookien"; 
  45.  
  46. $request.="\n"; 
  47.  
  48. $request.=$data_string."\n"; 
  49.  
  50. $fp = fsockopen($URL_Info["host"],$URL_Info["port"]); 
  51.  
  52. fputs($fp, $request); 
  53.  
  54. while(!feof($fp)) { 
  55.  
  56. $result .= fgets($fp, 1024); 
  57.  
  58.  
  59. fclose($fp); 
  60.  
  61. return $result; 
  62.  
  63.  
  64. ?>  

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

  1. <?php 
  2.  
  3. $ch = curl_init(); 
  4.  
  5. $timeout = 5; 
  6.  
  7. curl_setopt ($ch, CURLOPT_URL, 'http://wenda.shukaiming.com'); 
  8.  
  9. curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); 
  10.  
  11. curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout); 
  12.  
  13. $file_contents = curl_exec($ch); 
  14.  
  15. curl_close($ch); 
  16.  
  17. echo $file_contents; 
  18.  
  19. ?>   

www.phpzy.comtrue/php/22391.htmlTechArticlePHP发送HTTP请求的6种方法,知道4种算你牛!,6种4种 方法1: 用 file_get_contents 以get方式获取内容: ?php $url='https://wenda.shukaiming.com/'; echofile_get_contents($url); ? 方法2: 用fopen打开url, 以get方式...

相关文章

PHP之友评论

今天推荐