方法1: 用file_get_contents 以get方式获取内容
1 <?php 2 $url='http://www.domain.com/'; 3 $html = file_get_contents($url); 4 echo $html5 ?>
方法2: 用fopen打开url,以get方式获取内容
$fp = fopen($url,'r'3 //返回请求流信息(数组:请求状态,阻塞,返回值是否为空,返回值http头等)
1 stream_get_meta_data($fp);
while(!feof($fp)) { $result .= fgets($fp,1024} echo "url body: $result"5 fclose(6 ?> 7
方法3:用file_get_contents函数,以post方式获取url
1 生成url-encode后的请求字符串,将数组转换为字符串 2 http_build_query($data 3 $opts = array ( 4 <span style="white-space:pre"> </span>'http' => 5 <span style="white-space:pre"> </span>'method' => 'POST', 6 <span style="white-space:pre"> </span>'header'=> "Content-type: application/x-www-form-urlencoded\r\n" . 7 <span style="white-space:pre"> </span>"Content-Length: " . strlen($data) . "\r\n",1)"> 8 <span style="white-space:pre"> </span>'content' => $data 9 <span style="white-space:pre"> </span>) 10 );
生成请求的句柄文件 $context = stream_context_create($optsfile_get_contents('http://localhost/e/admin/test.html',1)">false,$context5 ?>
方法4:用fsockopen函数打开url,以get方式获取完整的数据,包括header和body,fsockopen需要 PHP.ini 中 allow_url_fopen 选项开启
1 <? 2 function get_url ($cookie=false 3 { 4 $url = parse_url( 5 $query = $url[path]."?".[query]; 6 echo "Query:".$query 7 fsockopen( $url[host],1)">$url[port]?$url[port]:80,1)">$errno,1)">$errstr,30 8 if (!) { 9 return 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: $cookie\n"15 $request.="\r\n"16 fwrite($request17 while()) { 18 $result .= @19 20 21 return $result22 23 24 获取url的html部分,去掉header 25 function GetUrlHTML(26 27 $rowdata = get_url($cookie28 $rowdata29 30 $body= stristr($rowdata,"\r\n\r\n"31 $body=substr($body,4,1)">$body)); 32 33 34 35 36 ?>
方法5:用fsockopen函数打开url,以POST方式获取完整的数据,包括header和body
方法6:使用curl库,使用curl库之前,可能需要查看一下php.ini是否已经打开了curl扩展
$ch = curl_init(); $timeout = 5 4 curl_setopt ($ch,CURLOPT_URL,'http://www.domain.com/' 5 curl_setopt ( 6 curl_setopt ($timeout$file_contents = curl_exec($ch 8 curl_close($file_contents10 ?>
原文地址:https://www.cnblogs.com/yuxiuyan
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。