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. echo $body; 
  14.  
  15. fclose($fp); 
  16.  
  17. ?> 

 方法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. $context = stream_context_create($opts); 
  22.  
  23. $html = file_get_contents('https://wenda.shukaming.com', false, $context); 
  24.  
  25. echo $html; 
  26.  
  27. ?>  

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

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

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

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

方法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.hantingmc.com/qtweb/news37/455137.html

网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联