當前位置:  首頁  >  PHP教程  >  PHP 進階  >  網絡編程

PHP發送HTTP請求的6種方法,知道4種算你牛

PHP發送HTTP請求的6種方法,知道4種算你牛方法1:用file_get_contents以get方式獲取內容:

方法1: 用 file_get_contents 以get方式獲取內容:

  1. php

  2. $url='https://wenda.shukaiming.com/';

  3. echo file_get_contents($url);

  4. ?>

方法2: 用fopen打開url, 以get方式獲取內容:

  1. php

  2. //r標識read,即標識只讀

  3. $fp = fopen($url, 'r');

  4. stream_get_meta_data($fp);

  5. while(!feof($fp)) {

  6. $body.= fgets($fp, 1024);

  7. }

  8. echo $body;

  9. fclose($fp);

  10. ?>

方法3:用 file_get_contents函數,以post方式獲取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'=> 'Contenttype: application/xwwwformurlencodedrn' .

  8. 'ContentLength: ' . strlen($data) . '\r\n',

  9. 'content' => $data

  10. )

  11. );


  12. $context = stream_context_create($opts);

  13. $html = file_get_contents('https://wenda.shukaming.com', false, $context);


  14. echo $html;

  15. ?>

方法4:用fsockopen函數打開url,以get方式獲取完整的數據,包括header和body,fsockopen需要 PHP.ini 中 allow_url_fopen 選項開啟

  1. php

  2. function get_url ($url,$cookie=false)

  3. {

  4. $url = parse_url($url);

  5. $query = $url[path].”?”.$url[query];

  6. echo $query;

  7. $fp = fsockopen( $url[host], $url[port]?$url[port]:80 , $errno, $errstr, 30);

  8. if (!$fp) {

  9. return false;

  10. } else {

  11. $request = "GET $query HTTP/1.1\r\n";

  12. $request .= "Host: $url[host]\r\n";

  13. $request .= "Connection: Close\r\n";

  14. if($cookie) $request.="Cookie:  $cookien";

  15. $request.="\r\n";

  16. fwrite($fp,$request);

  17. while(!@feof($fp)) {

  18. $result .= @fgets($fp, 1024);

  19. }

  20. fclose($fp);

  21. return $result;

  22. }

  23. }

  24. //獲取url的html部分,去掉header

  25. function GetUrlHTML($url,$cookie=false)

  26. {

  27. $rowdata = get_url($url,$cookie);

  28. if($rowdata)

  29. {

  30. $body= stristr($rowdata,”\r\n\r\n”);

  31. $body=substr($body,4,strlen($body));

  32. return $body;

  33. }


  34. return false;

  35. }

  36. ?>

方法5:用fsockopen函數打開url,以POST方式獲取完整的數據,包括header和body

  1. php

  2. function HTTP_Post($URL,$data,$cookie, $referrer="")

  3. {

  4. // parsing the given URL

  5. $URL_Info=parse_url($URL);


  6. // Building referrer

  7. if($referrer=="") // if not given use this script as referrer

  8. $referrer="111";


  9. // making string from $data

  10. foreach($data as $key=>$value)

  11. $values[]="$key=".urlencode($value);

  12. $data_string=implode("&",$values);


  13. // Find out which port is needed – if not given use standard (=80)

  14. if(!isset($URL_Info["port"]))

  15. $URL_Info["port"]=80;


  16. // building POSTrequest:

  17. $request.="POST ".$URL_Info["path"]." HTTP/1.1\n";

  18. $request.="Host: ".$URL_Info["host"]."\n";

  19. $request.="Referer: $referer\n";

  20. $request.="Contenttype: application/xwwwformurlencodedn";

  21. $request.="Contentlength: ".strlen($data_string)."\n";

  22. $request.="Connection: closen";


  23. $request.="Cookie:  $cookien";


  24. $request.="\n";

  25. $request.=$data_string."\n";


  26. $fp = fsockopen($URL_Info["host"],$URL_Info["port"]);

  27. fputs($fp, $request);

  28. while(!feof($fp)) {

  29. $result .= fgets($fp, 1024);

  30. }

  31. fclose($fp);


  32. return $result;

  33. }


  34. ?>

方法6:使用curl庫,使用curl庫之前,可能需要查看一下php.ini是否已經打開了curl擴展

  1. php

  2. $ch = curl_init();

  3. $timeout = 5;

  4. curl_setopt ($ch, CURLOPT_URL, 'http://wenda.shukaiming.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. ?>

吐了個 "CAO" !
掃碼關注 PHP1 官方微信號
PHP1.CN | 中國最專業的PHP中文社區 | PHP資訊 | PHP教程 | 數據庫技術 | 服務器技術 | 前端開發技術 | PHP框架 | 開發工具 | PHP問答
Copyright ? 1998 - 2020 PHP1.CN. All Rights Reserved PHP1.CN 第一PHP社區 版權所有
     
28玩法