国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > PHP > 正文

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

2024-05-04 23:57:07
字體:
來源:轉載
供稿:網友

今天因工作需要,需要用 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'; } ?> 

源碼下載地址:點擊查看

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


注:相關教程知識閱讀請移步到PHP教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 凉城县| 丰台区| 嵩明县| 美姑县| 上高县| 寿阳县| 松原市| 子长县| 定州市| 广州市| 轮台县| 岳池县| 乐至县| 吉水县| 宜春市| 永昌县| 浪卡子县| 杂多县| 岑溪市| 三原县| 隆化县| 会同县| 吐鲁番市| 抚州市| 微山县| 类乌齐县| 安化县| 柳江县| 阳东县| 防城港市| 武鸣县| 玉林市| 湄潭县| 凤山县| 双鸭山市| 凌海市| 保康县| 博湖县| 思茅市| 顺义区| 顺义区|