首页 > PHP教程 > php开发知识文章

PHP 使用curl或file_get_contents 获取需要授权(Authorization)页面的方法

本篇文章主要介绍了PHP 使用curl或file_get_contents 获取需要授权(Authorization)页面的方法

获取不需要授权的页面

PHP的curl 扩展,能够在服务器端发起POST/GET请求,访问页面,并能获取页面的返回数据。

$url = 'http://www.yuqingqi.com';
$param = array('type' => 1);
$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);
$re = curl_getinfo($ch);
curl_close($ch);
if ($re['http_code'] == 200) {
print_r($ret);
} else {
echo '0';
}

如果服务没有安装php curl扩展,使用file_get_contents发起请求,获取页面返回数据

$url = 'http://www.yuqingqi.com';
$param = array('type' => '1');
$opt = array(
'http' => array(
'method' => 'POST',
'header' => 'content-type:application/x-www-form-urlencoded',
'content' => http_build_query($param)
)
);
$context = stream_context_create($opt);
$re = file_get_contents($url, false, $context);
if ($re) {
print_r($re);
} else {
echo '0';
}

使用curl 和 file_get_contents 返回的结果都是一样的。

 

获取需要授权的页面

对于需要授权的页面,如使用了htpasswd+.htaccess设置目录访问权限的页面,直接用上面的方法会返回401 Unauthorized错误。

下面是打开一个需要授权页面,会提示要输入账号密码

PHP 使用curl或file_get_contents 获取需要授权(Authorization)页面的方法

下面介绍使用 $_SERVER['PHP_AUTH_USER'] $_SERVER['PHP_AUTH_PW']这两个服务器参数。

http://localhost/server.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'] != "php" || $_SERVER['PHP_AUTH_PW'] != "12")) {
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));

帐号:php密码:12

curl中,有一个参数是 CURLOPT_USERPWD,利用这个参数把帐号密码在请求时发送过去。

curl_setopt($ch, CURLOPT_USERPWD, '帐号:密码'); 

curl请求为:

$url = 'http://localhost/server.php';
$param = array('content' => 'hello php');
$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, 'php:12'); // 加入这句
$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 '0';
}

file_get_contents 如果要发送帐号和密码,需要手动拼接header

file_get_contents 请求为:

$url = 'http://localhost/server.php';
$param = array('content' => 'aa');
$auth = sprintf('Authorization: Basic %s', base64_encode('php:12')); // 加入这句
$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);
$re = file_get_contents($url, false, $context);
if ($re) {
$data = json_decode($re, true);
print_r($data);
} else {
echo '0';
}

以上就是本文PHP 使用curl或file_get_contents 获取需要授权(Authorization)页面的方法的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持本站!

关闭
感谢您的支持,我会继续努力!
扫码打赏,建议金额1-10元


提醒:打赏金额将直接进入对方账号,无法退款,请您谨慎操作。