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 相关文章推荐
第十三节 对象串行化 [13]
Oct 09 PHP
phpMyAdmin 安装教程全攻略
Mar 19 PHP
深入php socket的讲解与实例分析
Jun 13 PHP
解析yahoo邮件用phpmailer发送的实例
Jun 24 PHP
PHP获得数组交集与差集的方法
Jun 10 PHP
php动态绑定变量的用法
Jun 16 PHP
Yii基于数组和对象的Model查询技巧实例详解
Dec 28 PHP
简单的php+mysql聊天室实现方法(附源码)
Jan 05 PHP
php数组函数array_walk用法示例
May 26 PHP
PHP基于接口技术实现简单的多态应用完整实例
Apr 26 PHP
ThinkPHP 模板引擎使用详解
May 07 PHP
php获取目录下所有文件及目录(多种方法)(推荐)
May 14 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
php cookie 作用范围?不要在当前页面使用你的cookie
2009/03/24 PHP
PHP 时间转换Unix时间戳代码
2010/01/22 PHP
PHP删除数组中空值的方法介绍
2014/04/14 PHP
php+jQuery.uploadify实现文件上传教程
2014/12/26 PHP
PHP设计模式之装饰器模式实例详解
2018/02/07 PHP
详解PHP PDO简单教程
2019/05/28 PHP
Yii 框架入口脚本示例分析
2020/05/19 PHP
基于jquery的地址栏射击游戏代码
2011/03/10 Javascript
js确定对象类型方法
2012/03/30 Javascript
jquery让返回的内容显示在特定div里(代码少而精悍)
2014/06/23 Javascript
跟我学习javascript的隐式强制转换
2015/11/16 Javascript
学习JavaScript设计模式(封装)
2015/11/26 Javascript
javascript中call apply 与 bind方法详解
2016/03/10 Javascript
jQuery Dialog 取消右上角删除按钮事件
2016/09/07 Javascript
微信小程序 特效菜单抽屉效果实例代码
2017/01/11 Javascript
jQuery插件FusionCharts绘制的3D饼状图效果实例【附demo源码下载】
2017/03/03 Javascript
实现一个完整的Node.js RESTful API的示例
2017/09/29 Javascript
vue 使用Jade模板写html,stylus写css的方法
2018/02/23 Javascript
vue中eslintrc.js配置最详细介绍
2018/12/21 Javascript
Vue 开发必须知道的36个技巧(小结)
2019/10/09 Javascript
vue 解决addRoutes多次添加路由重复的操作
2020/08/04 Javascript
python中lambda函数 list comprehension 和 zip函数使用指南
2014/09/28 Python
Matplotlib 生成不同大小的subplots实例
2018/05/25 Python
pyqt5 lineEdit设置密码隐藏,删除lineEdit已输入的内容等属性方法
2019/06/24 Python
tensorflow2.0保存和恢复模型3种方法
2020/02/03 Python
tensorflow 实现自定义layer并添加到计算图中
2020/02/04 Python
jupyter notebook清除输出方式
2020/04/10 Python
Django:使用filter的pk进行多值查询操作
2020/07/15 Python
分享PyCharm最新激活码(真永久激活方法)不用每月找安装参数或最新激活码了
2020/12/27 Python
中华魂演讲稿
2014/05/13 职场文书
争先创优演讲稿
2014/09/15 职场文书
2014年小学工作总结
2014/11/26 职场文书
MySQL数据库必备之条件查询语句
2021/10/15 MySQL
Pygame如何使用精灵和碰撞检测
2021/11/17 Python
Hive导入csv文件示例
2022/06/25 数据库
阿里云服务器(windows)手动部署FTP站点详细教程
2022/08/05 Servers