前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >curl 要么 file_get_contents 获得授权页面的方法的必要性

curl 要么 file_get_contents 获得授权页面的方法的必要性

作者头像
全栈程序员站长
发布2022-07-06 10:55:52
1.1K0
发布2022-07-06 10:55:52
举报
文章被收录于专栏:全栈程序员必看

大家好,又见面了,我是全栈君。

今天,需要工作,需要使用 curl / file_get_contents 获得授权的必要性(Authorization)的页面内容。解决后写了这篇文章分享给大家。

php curl 扩展,可以在server端发起POST/GET请求,訪问页面,并能获取页面的返回数据。

比如要获取的页面:http://localhost/server.php

代码语言:javascript
复制
<?php
$content = isset($_POST['content'])? $_POST['content'] : '';header('content-type:application/json');echo json_encode(array('content'=>$content));?>

使用curl获取server.php页面

代码语言:javascript
复制
<?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也能够实现发起请求。获取页面返回数据

代码语言:javascript
复制
<?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 返回的结果都是一样的。

代码语言:javascript
复制
Array
(
    [content] => fdipzone blog
)

对于须要授权的页面,比如使用了htpasswd+.htaccess设置文件夹訪问权限的页面,直接用上面的方法会返回401 Unauthorized错误。

这次的样例先不使用htpasswd+.htaccess来控制訪问权限,而使用 _SERVER[‘PHP_AUTH_USER’] 和 _SERVER[‘PHP_AUTH_PW’]这两个server參数。

想了解htpasswd+.htaccess的朋友。能够訪问我之前写的文章 《使用apache htpasswd生成加密的password文件,并使用.htaccess控制文件夹訪问》

http://localhost/server.php 改动为:

代码语言:javascript
复制
<?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 password:654321

curl中。有一个參数是 CURLOPT_USERPWD,我们能够利用这个參数把帐号password在请求时发送过去。

代码语言:javascript
复制
curl_setopt($ch, CURLOPT_USERPWD, '帐号:password');

curl请求的程序改动为:

代码语言:javascript
复制
<?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 假设要发送帐号和password,须要手动拼接header

file_get_contents 请求的程序改动为:

代码语言:javascript
复制
<?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';
}
?>

源代码下载地址:点击查看

版权声明:本文博客原创文章,博客,未经同意,不得转载。

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/117190.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年1月9,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
命令行工具
腾讯云命令行工具 TCCLI 是管理腾讯云资源的统一工具。使用腾讯云命令行工具,您可以快速调用腾讯云 API 来管理您的腾讯云资源。此外,您还可以基于腾讯云的命令行工具来做自动化和脚本处理,以更多样的方式进行组合和重用。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档