#方法一:用fopen()開啟url
<?php $fp = fopen($url, ‘r'); stream_get_meta_data($fp); while(!feof($fp)) { $result .= fgets($fp, 1024); } echo “url body: $result”; fclose($fp); ?>
方法二:用file_get_contents()
#<?php $url='https://www.adminn.cn/'; $html = file_get_contents($url); echo $html; ?>
方法三:用fsockopen()函數開啟url,fsockopen( )函數需要PHP.ini 中allow_url_fopen 選項開啟。
<?php function get_url ($url,$cookie=false) { $url = parse_url($url); $query = $url[path].”?”.$url[query]; echo “Query:”.$query; $fp = fsockopen( $url[host], $url[port]?$url[port]:80 , $errno, $errstr, 30); if (!$fp) { return false; } else { $request = “GET $query HTTP/1.1rn”; $request .= “Host: $url[host]rn”; $request .= “Connection: Closern”; if($cookie) $request.=”Cookie: $cookien”; $request.=”rn”; fwrite($fp,$request); while(!@feof($fp)) { $result .= @fgets($fp, 1024); } fclose($fp); return $result; } } //获取url的html部分,去掉header function GetUrlHTML($url,$cookie=false) { $rowdata = get_url($url,$cookie); if($rowdata) { $body= stristr($rowdata,”rnrn”); $body=substr($body,4,strlen($body)); return $body; } return false; } ?>
推薦:《2021年PHP面試題大匯總(收藏)》《php影片教學》
以上是如何利用PHP發送GET請求的詳細內容。更多資訊請關注PHP中文網其他相關文章!