久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

PHP curl 或 file_get_contents 獲取需要授權頁面的方法

本篇文章主要介紹了PHP curl 或 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-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'; 
} 
?> 

源碼下載地址:點擊查看

以上就是本文的全部內容,希望本文的內容對大家的學習或者工作能帶來一定的幫助,同時也希望多多支持!

【網站聲明】本站除付費源碼經過測試外,其他素材未做測試,不保證完整性,網站上部分源碼僅限學習交流,請勿用于商業用途。如損害你的權益請聯系客服QQ:2655101040 給予處理,謝謝支持。

相關文檔推薦

下面小編就為大家帶來一篇php file_get_contents取文件中數組元素的方法。小編覺得挺不錯的,現在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
主站蜘蛛池模板: 99精品欧美一区二区三区 | 国产精品夜间视频香蕉 | 一级毛片视频 | 久久久精品日本 | 精品一区二区免费视频 | 国产网站在线 | 国产成人av电影 | 天堂在线中文 | 亚洲欧美激情精品一区二区 | 国产高清视频在线观看 | 九七午夜剧场福利写真 | 91欧美精品成人综合在线观看 | 国产黄视频在线播放 | 亚洲一区在线免费观看 | 欧美一区二区三区日韩 | 亚洲人人 | 欧美亚洲一级 | 老司机午夜性大片 | 欧美久久电影 | 超碰在线人 | 伊人免费在线观看 | 国精日本亚洲欧州国产中文久久 | 涩色视频在线观看 | 亚洲精品在线免费观看视频 | 人操人免费视频 | 久久不卡 | 国产乱码精品一区二区三区五月婷 | 日韩高清中文字幕 | 国产精品视频偷伦精品视频 | 一区二区三区视频在线 | 日韩一及片| 视频1区| 中文在线视频观看 | 久久国产婷婷国产香蕉 | 久久99精品久久久久久国产越南 | 亚洲精品久久久一区二区三区 | 在线观看www | 操久久| 国产1区| 黄色毛片一级 | 三区四区在线观看 |