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

开发 后端
本文总结了如何使用PHP代码发送HTTP请求的6种方法,如何你知道4种方法算你牛呦!快来看看吧!

 [[215975]]

方法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. ?>   
责任编辑:庞桂玉 来源: PHP技术大全
相关推荐

2015-10-27 11:06:51

PHPGETPOST

2015-08-06 13:33:22

PHPGETPOST

2023-10-13 10:45:18

HTTP数据

2024-03-18 10:15:00

HTTPNode.jsAPI

2020-06-23 09:52:31

运营效率首席信息官IT预算

2020-05-15 10:09:17

优化创新数字化转型CIO

2013-01-07 10:44:00

JavaScriptjQueryJS

2019-10-08 10:28:36

Python程序员镜音双子

2020-11-16 15:51:54

Kubernetes

2015-05-04 14:50:48

PHPPHP生成随机密码

2018-05-29 11:20:18

数据中心方法省钱

2011-09-19 14:30:27

2017-02-08 12:00:45

PHP性能对比

2017-04-28 15:07:10

网络瓶颈问题

2010-12-01 09:04:59

PHP开发

2009-11-23 15:57:51

PHP伪静态

2013-10-21 14:26:04

2020-12-01 09:00:00

数据中心IT技术

2009-04-13 09:09:53

WebServices返回数据横向

2020-07-24 00:34:54

工业物联网IIOT物联网
点赞
收藏

51CTO技术栈公众号