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


Posted in PHP onMay 16, 2010

1: 用file_get_contents 以get方式获取内容

<?php 
$url='http://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('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 "<br> Row field count: ".count($arr)."<br>"; 
foreach($arr as $key=>$value) 
{ 
echo "$key=$value <br>"; 
} 
} 
?>

7.
有些主机服务商把php的allow_url_fopen选项是关闭了,就是没法直接使用file_get_contents来获取远程web页面的内容。那就是可以使用另外一个函数curl。
下面是file_get_contents和curl两个函数同样功能的不同写法
file_get_contents函数的使用示例:
< ?php 
$file_contents = file_get_contents('http://www.ccvita.com/'); 
echo $file_contents; 
?>

换成curl函数的使用示例:
< ?php 
$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是否支持一个函数可以轻松写出下面函数
< ?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都关闭了,上面的函数就会出现错误。
PHP 相关文章推荐
PHP实现多条件查询实例代码
Jul 17 PHP
PHP的变量总结 新手推荐
Apr 18 PHP
php小技巧 把数组的键和值交换形成了新的数组,查找值取得键
Jun 02 PHP
ThinkPHP验证码使用简明教程
Mar 05 PHP
php实现对象克隆的方法
Jun 20 PHP
php生成数字字母的验证码图片
Jul 14 PHP
PHP中substr_count()函数获取子字符串出现次数的方法
Jan 07 PHP
详解yii2实现分库分表的方案与思路
Feb 03 PHP
浅谈php中fopen不能创建中文文件名文件的问题
Feb 06 PHP
PHP基于SMTP协议实现邮件发送实例代码
Apr 27 PHP
PHP精确到毫秒秒杀倒计时实例详解
Mar 14 PHP
PhpStorm 如何优雅的调试Hyperf的方法步骤
Nov 24 PHP
让PHP支持断点续传的源码
May 16 #PHP
php 获取一个月第一天与最后一天的代码
May 16 #PHP
PHP 缓存实现代码及详细注释
May 16 #PHP
PHP 防恶意刷新实现代码
May 16 #PHP
PHP 全角转半角实现代码
May 16 #PHP
php5.3 废弃函数小结
May 16 #PHP
memcached 和 mysql 主从环境下php开发代码详解
May 16 #PHP
You might like
基于qmail的完整WEBMAIL解决方案安装详解
2006/10/09 PHP
解析php中两种缩放图片的函数,为图片添加水印
2013/06/14 PHP
PHP 冒泡排序 二分查找 顺序查找 二维数组排序算法函数的详解
2013/06/25 PHP
CI框架验证码CAPTCHA辅助函数用法实例
2014/11/05 PHP
完美解决phpexcel导出到xls文件出现乱码的问题
2016/10/29 PHP
PHP abstract 抽象类定义与用法示例
2018/05/29 PHP
根据分辨率不同,调用不同的css文件
2006/08/25 Javascript
jQuery中与toggleClass等价的程序段 以及未来学习的方向
2010/03/18 Javascript
JavaScript使用IEEE 标准进行二进制浮点运算产生莫名错误的解决方法
2011/05/28 Javascript
页面回到顶部的三种实现(锚标记,js)
2012/10/01 Javascript
JavaScript中的Math.atan2()方法使用详解
2015/06/15 Javascript
Angularjs CURD 详解及实例代码
2016/09/14 Javascript
NodeJs读取JSON文件格式化时的注意事项
2016/09/25 NodeJs
使用UrlConnection实现后台模拟http请求的简单实例
2017/01/04 Javascript
解决vue打包项目后刷新404的问题
2018/03/06 Javascript
JS监听滚动和id自动定位滚动
2018/12/18 Javascript
[05:05]DOTA2亚洲邀请赛 战队出场仪式
2015/02/07 DOTA
PyChar学习教程之自定义文件与代码模板详解
2017/07/17 Python
python3爬虫获取html内容及各属性值的方法
2018/12/17 Python
Python OpenCV 使用滑动条来调整函数参数的方法
2019/07/08 Python
Django实现跨域请求过程详解
2019/07/25 Python
python爬虫 execjs安装配置及使用
2019/07/30 Python
python 命名规范知识点汇总
2020/02/14 Python
深入理解Tensorflow中的masking和padding
2020/02/24 Python
Python获取对象属性的几种方式小结
2020/03/12 Python
PyTorch安装与基本使用详解
2020/08/31 Python
阿拉伯世界最大的电子卖场:Souq埃及
2016/08/01 全球购物
PHP面试题及答案一
2012/06/18 面试题
致跳远运动员广播稿
2014/02/11 职场文书
餐厅总厨求职信
2014/03/04 职场文书
教师个人查摆剖析材料
2014/10/14 职场文书
2014年工人工作总结
2014/11/25 职场文书
北京天坛导游词
2015/02/12 职场文书
钱学森电影观后感
2015/06/04 职场文书
幼儿园小班开学寄语(2016秋季)
2015/12/03 职场文书
2016小学优秀教师先进事迹材料
2016/02/26 职场文书