激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - PHP教程 - PHP curl 或 file_get_contents 獲取需要授權頁面的方法

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

2021-05-17 16:51傲雪星楓 PHP教程

本篇文章主要介紹了PHP curl 或 file_get_contents獲取需要授權頁面的方法,具有很好的參考價值。下面跟著小編一起來看下吧

今天因工作需要,需要用 curl / file_get_contents 獲取需要授權(Authorization)的頁面內容,解決后寫了這篇文章分享給大家。

PHP curl 擴展,能夠在服務器端發起POST/GET請求,訪問頁面,并能獲取頁面的返回數據。

例如要獲取的頁面:http://localhost/server.php

?
1
2
3
4
5
<?php
$content = isset($_POST['content'])? $_POST['content'] : '';
header('content-type:application/json');
echo json_encode(array('content'=>$content));
?>

使用curl獲取server.php頁面

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?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也可以實現發起請求,獲取頁面返回數據

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?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 返回的結果都是一樣的。

?
1
2
3
4
Array
(
 [content] => fdipzone blog
)

對于需要授權的頁面,例如使用了htpasswd+.htaccess設置目錄訪問權限的頁面,直接用上面的方法會返回401 Unauthorized錯誤。

這次的例子先不使用htpasswd+.htaccess來控制訪問權限,而使用 $_SERVER['PHP_AUTH_USER'] $_SERVER['PHP_AUTH_PW']這兩個服務器參數。

http://localhost/server.php 修改為:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?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請求的程序修改為:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?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 請求的程序修改為:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<?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';
}
?>

源碼下載地址:點擊查看

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

原文鏈接:http://blog.csdn.net/fdipzone/article/details/44475801

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 一级外国毛片 | 男人的天堂色偷偷 | 国产精品久久久久无码av | 天天操天天操天天操天天操天天操天天操 | 日日摸夜夜添夜夜添牛牛 | 亚洲影视在线 | 久久999精品 | cosplay裸体福利写真 | 性明星video另类hd | 免看黄大片aa | h视频在线播放 | av在线等 | 成人短视频在线观看免费 | 色中色激情影院 | xxxxxx免费| 成人网在线观看 | 国产精品一区自拍 | 国产一区二区三区手机在线 | 91一区二区在线观看 | 越南一级黄色片 | 国产精品a一 | 一级毛片免费电影 | 国产一区亚洲 | 成人毛片网站 | 视频一区二区在线观看 | 午夜视频大全 | 亚洲一级毛片 | 九九综合视频 | 国产精品久久久久网站 | 国产老师做www爽爽爽视频 | 精品成人免费一区二区在线播放 | 毛毛片在线看 | 色综合网在线观看 | 中文字幕精品一二三四五六七八 | 91精品国产91 | 精品一区二区久久久久久久网精 | 国产超碰人人爽人人做人人爱 | 圆产精品久久久久久久久久久 | 国产美女视频黄a视频免费 日韩黄色在线播放 | 久久99精品久久久久久国产越南 | 成人三级电影网 |