今天因工作需要,需要用 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)); ?>
<?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 $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'; } ?>
Array ( [content] => fdipzone blog )
这次的例子先不使用htpasswd+.htaccess来控制访问权限,而使用 $_SERVER['PHP_AUTH_USER'] 和 $_SERVER['PHP_AUTH_PW']这两个服务器参数。
想了解htpasswd+.htaccess的朋友,可以访问我之前写的文章 《使用apache htpasswd生成加密的密码文件,并使用.htaccess控制目录访问》
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)); ?>
curl中,有一个参数是 CURLOPT_USERPWD,我们可以利用这个参数把帐号密码在请求时发送过去。
curl_setopt($ch, CURLOPT_USERPWD, '帐号:密码');
<?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 请求的程序修改为:
<?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-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'; } ?>