php请求远程url内容方法
php请求远程url内容有两个方法fopen/file_get_contents和curl。
1,fopen/file_get_contents与curl的差异
(1)fopen /file_get_contents 每次请求都会重新做DNS查询,并不对DNS信息进行缓存。但是CURL会自动对DNS信息进行缓存。对同一域名下的网页或者图片的请求只需要一次DNS查询。这大大减少了DNS查询的次数。所以CURL的性能比fopen /file_get_contents 好很多。
(2)fopen /file_get_contents在请求HTTP时,使用的是http_fopen_wrapper,不会keeplive。而curl却可以。这样在多次请求多个链接时,curl效率会好一些。
(3)curl可以模拟多种请求,例如:POST数据,表单提交等,用户可以按照自己的需求来定制请求。而fopen / file_get_contents只能使用get方式获取数据。
2,如果远程服务器关闭,file_get_contents处理方法,可以参考这篇文章,http://www.cnblogs.com/scofi/articles/3607529.html
公司里有经常有这样的业务,需要调用第三方公司提供的HTTP接口,在把接口提供的信息显示到网页上,代码是这样写的: file_get_contents("http://example.com/") 。
$opts = array(
'http'=>array(
'method'=>"GET",
'timeout'=>10,
)
);
$context = stream_context_create($opts);
$html =file_get_contents('http://www.example.com', false, $context);
echo $html;
代码中的timeout就是file_get_contents读取url的超时时间。
上篇说到我们说到设置file_get_contents超时时间用到了 stream_context_create方法,那么这个方法到底是什么呢?
<?php
$data = array("name" => 'test_name',"content" => 'test_con');
$data = http_build_query($data);
$opts = array(
'http'=>array(
'method'=>"POST",
'header'=>"Content-type: application/x-www-form-urlencoded\r\n".
"Content-length:".strlen($data)."\r\n" .
"Cookie: foo=bar\r\n" .
"\r\n",
'content' => $data,
)
);
$cxContext = stream_context_create($opts);
$sFile = file_get_contents("http://127.0.0.1/reponse.php", false, $cxContext);
echo $sFile;
?>
reponse.php被请求的页面:
<?php
var_dump($_POST);
var_dump($_COOKIE);
?>
运行之后的结果为:
<?php
$url = 'http://www.example.com';
//初始化一个 cURL 对象
$ch = curl_init();
//设置你需要抓取的URL
curl_setopt($ch, CURLOPT_URL, $url);
// 设置cURL 参数,要求结果保存到字符串中还是输出到屏幕上。
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//是否获得跳转后的页面
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$data = curl_exec($ch);
curl_close($ch);
echo $data;
?>
(2)使用curl。post获取数据
<?php
function curl_post($url, $arr_data){
$post_data = http_build_query($url_data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFLELDS, $post_data);
$data = curl_exec($ch);
curl_close($ch);
echo $data;
}
$arr_post = array(
'name'=>'test_name',
'age' => 1
);
curl_post("http://www.explame.com/", $arr_post);
?>
(3)使用代理抓取页面,什么要使用代理进行抓取呢?以google为例吧,如果去抓google的数据,短时间内抓的很频繁的话,你就抓取不到了。google对你的ip地址做限制这个时候,你可以换代理重新抓。
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://google.com");
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//是否通过http代理来传输
curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, TRUE);
curl_setopt($ch, CURLOPT_PROXY, 125.21.23.6:8080);
//url_setopt($ch, CURLOPT_PROXYUSERPWD, 'user:password');如果要密码的话,加上这个
$result=curl_exec($ch);
curl_close($ch);
?>
(4)继续保持本站session调用,在实现用户同步登录的情况下需要共享session,如果要继续保持本站的session,那么要把sessionid放到http请求中。
<?php
$session_str = session_name().'='.session_id().'; path=/; domain=.explame.com';
session_write_close(); //将数据写入文件并且结束session
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_COOKIE, $session_str);
$ret = curl_exec($ch);
curl_close($ch);
?>
php请求远程url内容方法的更多相关文章
- php使用curl简单抓取远程url的方法
这篇文章主要介绍了php使用curl简单抓取远程url的方法,涉及php操作curl的技巧,具有一定参考借鉴价值,需要的朋友可以参考下 本文实例讲述了php使用curl抓取远程url的方法.分 ...
- file_get_contents抓取远程URL内容
/** * POST URL * @param $url * @param null $post * @return false / string */ public static function ...
- 获取一个请求的URL内容
using System.Net; 1. // 创建一个请求的URL. WebRequest request = WebRequest.Create("http://www ...
- post请求远程url 报错“基础连接已经关闭...Authentication.AuthenticationException...远程证书无效”解决方案
当我们有时用代码编写post请求url远程地址会报“基础连接已经关闭: 未能为 SSL/TLS 安全通道建立信任关系. ---> System.Security.Authentication.A ...
- nodejs通过request请求远程url的文件并下载到本地
需要循环去下载远程文件,然后自己写了一个demo,可以直接运行,如下: //文件下载 var fs = require("fs"); var path = require(&quo ...
- HttpServletRequest 获取URL的方法及区别
HttpServletRequest 获取请求的URL的方法有: 1.request.getRequestURL() 返回的是完整的url,包括Http协议,端口号,servlet名字和映射路径,但它 ...
- EasyUI queryParams属性 在请求远程数据同时给action方法传参
http://www.cnblogs.com/iack/p/3530500.html?utm_source=tuicool EasyUI queryParams属性 在请求远程数据同时给action方 ...
- paip.uapi 获取网络url内容html 的方法java php ahk c++ python总结.
paip.uapi 获取网络url内容html 的方法java php ahk c++ python总结. 各种语言总结比较,脚本php.python果然是方便.简短,实用. uapi : get_w ...
- TCP服务端开发为例--web开发不同url请求走不同control方法
拿java的web开发为例子,相信有很多小伙伴是做j2EE开发的,htpp请求,json数据传输都是工作中经常用的,查询请求,添加请求,修改请求前端配个url,例如https://localhost/ ...
随机推荐
- 登录PL/SQL无法登录,提示错误:ORA-01017: invalid username/password; logon denied 错误
在使用在登录PL/SQL(使用scott用户)无法登录,提示错误:ORA-01017: invalid username/password; logon denied 错误(程序中的用户和密码无法登录 ...
- Redis 集群二
[Redis 集群二] 集群的客户端 Redis 集群现阶段的一个问题是客户端实现很少. 以下是一些我知道的实现: redis-rb-cluster 是我(@antirez)编写的 Ruby 实现, ...
- python's object model
[python's object model] 1.object.__init__(self[, ...]) 如果subclass没有实现__init__,那么python类在实例化的时 ...
- jquery实现全选,取消,反选的功能&实现左侧菜单
1.全选,取消,反选的例子 <!DOCTYPE html> <html lang="en"> <head> <meta charset=& ...
- Golang学习系列:(一)介绍和安装
Golang学习系列:(一)介绍和安装 Java程序员带你来到Go的世界,让我们开始探索吧! Go是一种新的语言,一种并发的,带有垃圾回收的.快速编译的语言,它具有一下特点: 他可以在一台计算机上用几 ...
- 部署MVC项目ManagedPipelineHandler报错
"处理程序ExtensionlessUrlHandler-Integrated-4.0在其模块列表中有一个错误模块ManagedPipelineHandler": 解决方法:以管理 ...
- js的prototype(2)
1 原型法设计模式 在.Net中可以使用clone()来实现原型法 原型法的主要思想是,现在有1个类A,我想要创建一个类B,这个类是以A为原型的,并且能进行扩展.我们称B的原型为A. 2 javasc ...
- Maven项目下java.lang.ClassNotFoundException常规解决办法
网上很多要修改.class .project的比较麻烦有时候还不一定管用.下面的方法适合于项目用已经引用了jar,但是运行时却ClassNotFound,请用如下方法试试: 严重: Error con ...
- chrome会话cookie显示过期时间为1969-12-31T23:59:59.000Z
cookie不设置过期时间的话,为浏览器会话cookie,关闭浏览器自动删除cookie 但是在chrome浏览器下,cookie过期时间显示为“1969-12-31T23:59:59.000Z” 在 ...
- Git 客户端基本配置
Welcome to Git (version -preview20140611) Run 'git help git' to display the help index. Run 'git hel ...