找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 1344|回复: 0
打印 上一主题 下一主题

PHP curl 或 file_get_contents 获取需要授权页面的方法

[复制链接]

2617

主题

2617

帖子

7789

积分

论坛元老

Rank: 8Rank: 8

积分
7789
跳转到指定楼层
楼主
发表于 2018-2-14 05:26:14 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

            今天因工作需要,需要用 curl / file_get_contents 获取需要授权(Authorization)的页面内容,解决后写了这篇文章分享给大家。
PHP curl 扩展,能够在服务器端发起POST/GET请求,访问页面,并能获取页面的返回数据。
例如要获取的页面:http://localhost/server.php
$content));
?>
使用curl获取server.php页面
'fdipzone blog');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($param));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$ret = curl_exec($ch);
$retinfo = curl_getinfo($ch);
curl_close($ch);
if($retinfo['http_code']==200){
$data = json_decode($ret, true);
print_r($data);
}else{
echo 'POST Fail';
}
?>
如果服务没有安装php curl扩展,使用file_get_contents也可以实现发起请求,获取页面返回数据
'fdipzone blog');
$opt = array(
'http' => array(
  'method' => 'POST',
  'header' => 'content-type:application/x-www-form-urlencoded',
  'content' => http_build_query($param)
)
);
$context = stream_context_create($opt);
$ret = file_get_contents($url, false, $context);
if($ret){
$data = json_decode($ret, true);
print_r($data);
}else{
echo 'POST Fail';
}
?>
使用curl 和 file_get_contents 返回的结果都是一样的。
Array
(
[content] => fdipzone blog
)
对于需要授权的页面,例如使用了htpasswd+.htaccess设置目录访问权限的页面,直接用上面的方法会返回401 Unauthorized错误。
这次的例子先不使用htpasswd+.htaccess来控制访问权限,而使用 $_SERVER['PHP_AUTH_USER'] $_SERVER['PHP_AUTH_PW']这两个服务器参数。
http://localhost/server.php 修改为:
$content));
?>
设定帐号:fdipzone 密码:654321
curl中,有一个参数是 CURLOPT_USERPWD,我们可以利用这个参数把帐号密码在请求时发送过去。
curl_setopt($ch, CURLOPT_USERPWD, '帐号:密码');  
curl请求的程序修改为:
'fdipzone blog');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($param));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, 'fdipzone:654321'); // 加入这句
$ret = curl_exec($ch);
$retinfo = curl_getinfo($ch);
curl_close($ch);
if($retinfo['http_code']==200){
$data = json_decode($ret, true);
print_r($data);
}else{
echo 'POST Fail';
}
?>
而file_get_contents 如果要发送帐号和密码,需要手动拼接header
file_get_contents 请求的程序修改为:
'fdipzone blog');
$auth = sprintf('Authorization: Basic %s', base64_encode('fdipzone:654321')); // 加入这句
$opt = array(
'http' => array(
  'method' => 'POST',
  'header' => "content-type:application/x-www-form-urlencoded\r\n".$auth."\r\n", // 把$auth加入到header
  'content' => http_build_query($param)
)
);
$context = stream_context_create($opt);
$ret = file_get_contents($url, false, $context);
if($ret){
$data = json_decode($ret, true);
print_r($data);
}else{
echo 'POST Fail';
}
?>
源码下载地址:点击查看
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持脚本之家!
            
            
您可能感兴趣的文章:
  • php中使用Curl、socket、file_get_contents三种方法POST提交数据
  • 比file_get_contents稳定的curl_get_contents分享
  • 探讨file_get_contents与curl效率及稳定性的分析
  • 深入file_get_contents与curl函数的详解
  • php采用file_get_contents代替使用curl实例
  • php中file_get_contents与curl性能比较分析
  • PHP CURL或file_get_contents获取网页标题的代码及两者效率的稳定性问题
  • php基于curl重写file_get_contents函数实例
            
  • 分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
    收藏收藏
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    用户反馈
    客户端