gongzhonghao.png
扫码关注我们
最新赞助活动温馨提示:自愿赞助服务器费用,学生和没有工作的整站资源免费下载!
头像

php中请求url有哪些方法

来源:http://erdangjiade.com/topic/1511.html 你好,世界。 2017-09-25 22:42浏览(35)

PHP中打开URL地址的几种方法总结,这里的函数主要用于小偷采集等函数。

1: 用file_get_contents

以get方式获取内容

<?php 
$url='www.baidu.com/'; 
$html = file_get_contents($url); 
//print_r($http_response_header); 
ec($html); 
printhr(); 
printarr($http_response_header); 
 
printhr(); 
?>

示例代码2: 用fopen打开url,

以get方式获取内容

<? 
$fp = fopen($url, 'r'); 
 
printarr(stream_get_meta_data($fp)); 
printhr(); 
while(!feof($fp)) { 
 
$result .= fgets($fp, 1024); 
} 
echo "url body: $result"; 
 
printhr(); 
fclose($fp); 
?>

示例代码3:用file_get_contents函数,以post方式获取url

<?php 
$data = array ('foo' => 
'bar'); 
$data = http_build_query($data); 
$opts = array ( 
'http' 
=> array ( 
'method' => 'POST', 
'header'=> "Content-type: 
application/x-www-form-urlencoded" . 
"Content-Length: " . strlen($data) . 
"", 
'content' => $data 
), 
); 
$context = 
stream_context_create($opts); 
$html = 
file_get_contents('localhost/e/admin/test.html', false, $context); 
 
echo $html; 
?>

示例代码4:用fsockopen函数打开url,以get方式获取完整的数据,包括header和body

<? 
function get_url 
($url,$cookie=false) { 
$url = parse_url($url); 
$query = 
$url[path]."?".$url[query]; 
ec("Query:".$query); 
$fp = fsockopen( 
$url[host], $url[port]?$url[port]:80 , $errno, $errstr, 30); 
if (!$fp) { 
 
return false; 
} else { 
$request = "GET $query HTTP/1.1"; 
 
$request .= "Host: $url[host]"; 
$request .= "Connection: Close"; 
 
if($cookie) $request.="Cookie: $cookie
"; 
$request.=""; 
 
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,""); 
$body=substr($body,4,strlen($body)); 
return $body; 
} 
return false; 
} 
?>

最近开发中遇到一个问题,程序第4行会请求一个url,通过查找相关的资料发现有多种方法,本文给大家介绍了关于php中请求url的五种方法,分别是用fopen()函数、file()函数、file_get_contents()函数、curl() 请求远程url数据和exec() 执行命令行命令

本文主要给大家介绍了关于php中请求url的五种方法,分享出来供大家参考学习,下面话不多说,来一起看看详细的介绍:

五种方法:

  • 前三种都是php基本的文件操作函数

  • curl()是php扩展需要开启,linux下需要安装

  • exec()执行的是linux命令行下的命令wget下载远程文件

其中wget命令在本地虚机测试请求www.baidu.com时,没有成功,在远程服务器上却可以,考虑时DNS解析的问题,于是直接请求IP成功下载了index.html的文件。

这里只提供了方法,其中的优缺点需要详细了解每一个方法的功能和缺陷。

一、fopen()函数

$file = fopen("www.jb51.net", "r") or die("打开远程文件失败!");
while (!feof($file)) {
 $line = fgets($file, 1024);
 //使用正则匹配标题标记
 if (preg_match("/<title>(.*)</title>/i", $line, $out)) { 
 $title = $out[1]; //将标题标记中的标题字符取出
 break; //退出循环,结束远程文件读取
 }
}
fclose($file);

二、file()函数


$lines = file("www.jb51.net/article/48866.htm");
readfile(www.jb51.net/article/48866.htm);

三、file_get_contents()函数


$content = file_get_contents(www.jb51.net/article/48866.htm);

四、curl() 请求远程url数据


$url = "www.baidu.com";
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$contents = curl_exec($ch);
curl_close($ch);

五、exec() 执行命令行命令

//exec("wget 220.181.111.188");
shell_exec("wget 220.181.111.188");

以上就是php中请求url有哪些方法的详细内容,更多请关注二当家的素材网其它相关文章!

评论0
头像

友情提示:垃圾评论一律封号 加我微信:826096331拉你进VIP群学习群

1 2