今天因工做須要,須要用 curl / file_get_contents 獲取須要受權(Authorization)的頁面內容,解決後寫了這篇文章分享給你們。php
php curl 擴展,可以在服務器端發起POST/GET請求,訪問頁面,並能獲取頁面的返回數據。
apache
例如要獲取的頁面:http://localhost/server.php
json
[php] view plain copy 服務器
<?php app
$content = isset($_POST['content'])? $_POST['content'] : ''; curl
header('content-type:application/json'); ui
echo json_encode(array('content'=>$content)); 加密
?> url
使用curl獲取server.php頁面
spa
[php] view plain copy
<?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] view plain copy
<?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 返回的結果都是同樣的。
[php] view plain copy
Array
(
[content] => fdipzone blog
)
對於須要受權的頁面,例如使用了htpasswd+.htaccess設置目錄訪問權限的頁面,直接用上面的方法會返回401 Unauthorized錯誤。
此次的例子先不使用htpasswd+.htaccess來控制訪問權限,而使用 $_SERVER['PHP_AUTH_USER'] 和 $_SERVER['PHP_AUTH_PW']這兩個服務器參數。
想了解htpasswd+.htaccess的朋友,能夠訪問我以前寫的文章 《使用apache htpasswd生成加密的密碼文件,並使用.htaccess控制目錄訪問》
http://localhost/server.php 修改成:
[php] view plain copy
<?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,咱們能夠利用這個參數把賬號密碼在請求時發送過去。
[php] view plain copy
curl_setopt($ch, CURLOPT_USERPWD, '賬號:密碼');
curl請求的程序修改成:
[php] view plain copy
<?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] view plain copy
<?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';
}
?>