您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

PHP下打开URL地址的几种方法小结

2025/5/14 7:04:14发布22次查看
php中打开url地址的几种方法总结,这里的函数主要用于小偷采集等函数。
1: 用file_get_contents 以get方式获取内容
代码如下:
示例代码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
代码如下:
'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('http://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\n;
$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;
}
?>
示例代码5:用fsockopen函数打开url,以post方式获取完整的数据,包括header和body
代码如下:
function http_post($url,$data,$cookie, $referrer=) {
// parsing the given url
$url_info=parse_url($url);
// building referrer
if($referrer==) // if not given use this script as referrer
$referrer=111;
// making string from $data
foreach($data as $key=>$value)
$values[]=$key=.urlencode($value);
$data_string=implode(&,$values);
// find out which port is needed - if not given use standard (=80)
if(!isset($url_info[port]))
$url_info[port]=80;
// building post-request:
$request.=post .$url_info[path]. http/1.1\n;
$request.=host: .$url_info[host].\n;
$request.=referer: $referer\n;
$request.=content-type: application/x-www-form-urlencoded\n;
$request.=content-length: .strlen($data_string).\n;
$request.=connection: close\n;
$request.=cookie: $cookie\n;
$request.=\n;
$request.=$data_string.\n;
$fp = fsockopen($url_info[host],$url_info[port]);
fputs($fp, $request);
while(!feof($fp)) {
$result .= fgets($fp, 1024);
}
fclose($fp);
return $result;
}
printhr();
?>
示例代码6:使用curl库,使用curl库之前,你可能需要查看一下php.ini,查看是否已经打开了curl扩展
代码如下:
$ch = curl_init();
$timeout = 5;
curl_setopt ($ch, curlopt_url, 'http://www.baidu.com/');
curl_setopt ($ch, curlopt_returntransfer, 1);
curl_setopt ($ch, curlopt_connecttimeout, $timeout);
$file_contents = curl_exec($ch);
curl_close($ch);
echo $file_contents;
?>
关于curl库:
curl官方网站http://curl.haxx.se/
curl 是使用url语法的传送文件工具,支持ftp、ftps、http htpps scp sftp tftp telnet dict file和ldap。curl 支持ssl证书、http post、http put 、ftp 上传,kerberos、基于htt格式的上传、代理、cookie、用户+口令证明、文件传送恢复、http代理通道和大量其他有用的技巧
代码如下:
function printarr(array $arr)
{
echo
row field count: .count($arr).
;
foreach($arr as $key=>$value)
{
echo $key=$value
;
}
}
?>
7.
有些主机服务商把php的allow_url_fopen选项是关闭了,就是没法直接使用file_get_contents来获取远程web页面的内容。那就是可以使用另外一个函数curl。
下面是file_get_contents和curl两个函数同样功能的不同写法
file_get_contents函数的使用示例:
代码如下:
$file_contents = file_get_contents('http://www.ccvita.com/');
echo $file_contents;
?>
换成curl函数的使用示例:
代码如下:
$ch = curl_init();
$timeout = 5;
curl_setopt ($ch, curlopt_url, 'http://www.ccvita.com');
curl_setopt ($ch, curlopt_returntransfer, 1);
curl_setopt ($ch, curlopt_connecttimeout, $timeout);
$file_contents = curl_exec($ch);
curl_close($ch);
echo $file_contents;
?>
利用function_exists函数来判断php是否支持一个函数可以轻松写出下面函数
代码如下:
function vita_get_url_content($url) {
if(function_exists('file_get_contents')) {
$file_contents = file_get_contents($url);
} else {
$ch = curl_init();
$timeout = 5;
curl_setopt ($ch, curlopt_url, $url);
curl_setopt ($ch, curlopt_returntransfer, 1);
curl_setopt ($ch, curlopt_connecttimeout, $timeout);
$file_contents = curl_exec($ch);
curl_close($ch);
}
return $file_contents;
}
?>
其实上面的这个函数还有待商榷,如果你的主机服务商把file_get_contents和curl都关闭了,上面的函数就会出现错误。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product