RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
PHP发送HTTP请求的6种方法,知道4种算你牛!

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

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

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

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

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

 
 
 
  1.  
  2. $data = array (‘foo' => ‘bar'); 
  3.  
  4. $data = http_build_query($data); 
  5.  
  6. $opts = array ( 
  7.  
  8. 'http' => array ( 
  9.  
  10. 'method' => 'POST', 
  11.  
  12. 'header'=> 'Content-type: application/x-www-form-urlencodedrn' . 
  13.  
  14. 'Content-Length: ' . strlen($data) . '\r\n', 
  15.  
  16. 'content' => $data 
  17.  
  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.  
  2. function get_url ($url,$cookie=false) 
  3.  
  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.  
  38. fclose($fp); 
  39.  
  40. return $result; 
  41.  
  42. } 
  43.  
  44. } 
  45.  
  46. //获取url的html部分,去掉header 
  47.  
  48. function GetUrlHTML($url,$cookie=false) 
  49.  
  50. { 
  51.  
  52. $rowdata = get_url($url,$cookie); 
  53.  
  54. if($rowdata) 
  55.  
  56. { 
  57.  
  58. $body= stristr($rowdata,”\r\n\r\n”); 
  59.  
  60. $body=substr($body,4,strlen($body)); 
  61.  
  62. return $body; 
  63.  
  64. } 
  65.  
  66. return false; 
  67.  
  68. } 
  69.  
  70. ?>  

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

 
 
 
  1.  
  2. function HTTP_Post($URL,$data,$cookie, $referrer="") 
  3.  
  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.  
  60. fclose($fp); 
  61.  
  62. return $result; 
  63.  
  64. } 
  65.  
  66. ?>  

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

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

当前文章:PHP发送HTTP请求的6种方法,知道4种算你牛!
文章转载:http://www.aqpgk.com/article/dhphiij.html