当前位置:首页 > PHP教程 > PHP高级教程

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

今天因工作需要,需要用 curl / file_get_contents 获取需要授权(authorization)的页面内容,解决后写了这篇文章分享给大家。

php curl 扩展,能够在服务器端发起post/get请求,访问页面,并能获取页面的返回数据。

例如要获取的页面:http://localhost/server.php

<?php $content = isset($_post['content'])? $_post['content'] : ''; header('content-type:application/json'); echo json_encode(array('content'=>$content)); ?>

使用curl获取server.php页面

<?php $url = 'http://localhost/server.php'; $param = array('content'=>'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也可以实现发起请求,获取页面返回数据

<?php $url = 'http://localhost/server.php'; $param = array('content'=>'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 修改为:

<?php if(!isset($_server['php_auth_user'])) { header('www-authenticate: basic realm="localhost"'); header("http/1.0 401 unauthorized"); exit; }else{ if (($_server['php_auth_user']!= "fdipzone" || $_server['php_auth_pw']!="654321")) { header('www-authenticate: basic realm="localhost"'); header("http/1.0 401 unauthorized"); exit; } } $content = isset($_post['content'])? $_post['content'] : ''; header('content-type:application/json'); echo json_encode(array('content'=>$content)); ?>

设定帐号:fdipzone 密码:654321

curl中,有一个参数是 curlopt_userpwd,我们可以利用这个参数把帐号密码在请求时发送过去。

curl_setopt($ch, curlopt_userpwd, '帐号:密码'); 

curl请求的程序修改为:

<?php $url = 'http://localhost/server.php'; $param = array('content'=>'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 请求的程序修改为:

<?php $url = 'http://localhost/server.php'; $param = array('content'=>'fdipzone blog'); $auth = sprintf('authorization: basic %s', base64_encode('fdipzone:654321')); // 加入这句 $opt = array( 'http' => array( 'method' => 'post', 'header' => "content-type:application/x-www-form-urlencodedrn".$auth."rn", // 把$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'; } ?>

源码下载地址:点击查看

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持硕编程!



【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!

相关教程推荐

其他课程推荐