PHP curl 或 file_get_contents 獲取需要授權(quán)頁(yè)面的方法
今天因工作需要,需要用 curl / file_get_contents 獲取需要授權(quán)(Authorization)的頁(yè)面內(nèi)容,解決后寫(xiě)了這篇文章分享給大家。
PHP curl 擴(kuò)展,能夠在服務(wù)器端發(fā)起POST/GET請(qǐng)求,訪問(wèn)頁(yè)面,并能獲取頁(yè)面的返回?cái)?shù)據(jù)。
例如要獲取的頁(yè)面: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頁(yè)面
<?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';
}
?>
如果服務(wù)沒(méi)有安裝php curl擴(kuò)展,使用file_get_contents也可以實(shí)現(xiàn)發(fā)起請(qǐng)求,獲取頁(yè)面返回?cái)?shù)據(jù)
<?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 返回的結(jié)果都是一樣的。
Array ( [content] => fdipzone blog )
對(duì)于需要授權(quán)的頁(yè)面,例如使用了htpasswd+.htaccess設(shè)置目錄訪問(wèn)權(quán)限的頁(yè)面,直接用上面的方法會(huì)返回401 Unauthorized錯(cuò)誤。
這次的例子先不使用htpasswd+.htaccess來(lái)控制訪問(wèn)權(quán)限,而使用 $_SERVER['PHP_AUTH_USER'] 和 $_SERVER['PHP_AUTH_PW']這兩個(gè)服務(wù)器參數(shù)。
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));
?>
設(shè)定帳號(hào):fdipzone 密碼:654321
curl中,有一個(gè)參數(shù)是 CURLOPT_USERPWD,我們可以利用這個(gè)參數(shù)把帳號(hào)密碼在請(qǐng)求時(shí)發(fā)送過(guò)去。
curl_setopt($ch, CURLOPT_USERPWD, '帳號(hào):密碼');
curl請(qǐng)求的程序修改為:
<?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 如果要發(fā)送帳號(hào)和密碼,需要手動(dòng)拼接header
file_get_contents 請(qǐng)求的程序修改為:
<?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';
}
?>
源碼下載地址:點(diǎn)擊查看
以上就是本文的全部?jī)?nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,同時(shí)也希望多多支持腳本之家!
- PHP中file_get_contents函數(shù)抓取https地址出錯(cuò)的解決方法(兩種方法)
- PHP實(shí)現(xiàn)模擬http請(qǐng)求的方法分析
- PHP模擬http請(qǐng)求的方法詳解
- php發(fā)送http請(qǐng)求的常用方法分析
- php實(shí)現(xiàn)的http請(qǐng)求封裝示例
- PHP使用socket發(fā)送HTTP請(qǐng)求的方法
- php 利用socket發(fā)送HTTP請(qǐng)求(GET,POST)
- PHP實(shí)現(xiàn)取得HTTP請(qǐng)求的原文
- PHP獲取http請(qǐng)求的頭信息實(shí)現(xiàn)步驟
- PHP使用file_get_contents發(fā)送http請(qǐng)求功能簡(jiǎn)單示例
相關(guān)文章
PHP file_get_contents 函數(shù)超時(shí)的幾種解決方法
在使用file_get_contents函數(shù)的時(shí)候,經(jīng)常會(huì)出現(xiàn)超時(shí)的情況,在這里要通過(guò)查看一下錯(cuò)誤提示,看看是哪種錯(cuò)誤,比較常見(jiàn)的是讀取超時(shí),這種情況大家可以通過(guò)一些方法來(lái)盡量的避免或者解決。2009-07-07
PHP實(shí)現(xiàn)文件分片上傳的實(shí)例代碼
在本篇文章里小編給大家整理的是一篇關(guān)于PHP實(shí)現(xiàn)文件分片上傳的實(shí)例代碼內(nèi)容,有興趣的朋友們參考學(xué)習(xí)下。2020-01-01
php使用preg_match()函數(shù)驗(yàn)證ip地址的方法
這篇文章主要介紹了php使用preg_match()函數(shù)驗(yàn)證ip地址的方法,涉及php針對(duì)數(shù)字及字符串的正則匹配操作相關(guān)技巧,需要的朋友可以參考下2017-01-01
處理(php-cgi.exe?-?FastCGI?進(jìn)程超過(guò)了配置的請(qǐng)求超時(shí)時(shí)限)的問(wèn)題
本篇文章是對(duì)解決(php-cgi.exe?-?FastCGI?進(jìn)程超過(guò)了配置的請(qǐng)求超時(shí)時(shí)限)的問(wèn)題進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-07-07

