方法一:用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; 
 } 
?>
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。