当前位置:Gxlcms > PHP教程 > PHP curl 或 file_get_contents 获取需要授权页面的方法

PHP curl 或 file_get_contents 获取需要授权页面的方法

时间:2021-07-01 10:21:17 帮助过:31人阅读

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

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

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

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

使用curl获取server.php页面

  1. <?php
  2. $url = 'http://localhost/server.php';
  3. $param = array('content'=>'fdipzone blog');
  4. $ch = curl_init();
  5. curl_setopt($ch, CURLOPT_URL, $url);
  6. curl_setopt($ch, CURLOPT_POST, true);
  7. curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($param));
  8. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  9. $ret = curl_exec($ch);
  10. $retinfo = curl_getinfo($ch);
  11. curl_close($ch);
  12. if($retinfo['http_code']==200){
  13. $data = json_decode($ret, true);
  14. print_r($data);
  15. }else{
  16. echo 'POST Fail';
  17. }
  18. ?>

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

  1. <?php
  2. $url = 'http://localhost/server.php';
  3. $param = array('content'=>'fdipzone blog');
  4. $opt = array(
  5. 'http' => array(
  6. 'method' => 'POST',
  7. 'header' => 'content-type:application/x-www-form-urlencoded',
  8. 'content' => http_build_query($param)
  9. )
  10. );
  11. $context = stream_context_create($opt);
  12. $ret = file_get_contents($url, false, $context);
  13. if($ret){
  14. $data = json_decode($ret, true);
  15. print_r($data);
  16. }else{
  17. echo 'POST Fail';
  18. }
  19. ?>

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

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

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

这次的例子先不使用htpasswd+.htaccess来控制访问权限,而使用 $_SERVER['PHP_AUTH_USER'] $_SERVER['PHP_AUTH_PW']这两个服务器参数。

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

  1. <?php
  2. if(!isset($_SERVER['PHP_AUTH_USER']))
  3. {
  4. header('WWW-Authenticate: Basic realm="localhost"');
  5. header("HTTP/1.0 401 Unauthorized");
  6. exit;
  7. }else{
  8. if (($_SERVER['PHP_AUTH_USER']!= "fdipzone" || $_SERVER['PHP_AUTH_PW']!="654321")) {
  9. header('WWW-Authenticate: Basic realm="localhost"');
  10. header("HTTP/1.0 401 Unauthorized");
  11. exit;
  12. }
  13. }
  14. $content = isset($_POST['content'])? $_POST['content'] : '';
  15. header('content-type:application/json');
  16. echo json_encode(array('content'=>$content));
  17. ?>

设定帐号:fdipzone 密码:654321

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

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

curl请求的程序修改为:

  1. <?php
  2. $url = 'http://localhost/server.php';
  3. $param = array('content'=>'fdipzone blog');
  4. $ch = curl_init();
  5. curl_setopt($ch, CURLOPT_URL, $url);
  6. curl_setopt($ch, CURLOPT_POST, true);
  7. curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($param));
  8. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  9. curl_setopt($ch, CURLOPT_USERPWD, 'fdipzone:654321'); // 加入这句
  10. $ret = curl_exec($ch);
  11. $retinfo = curl_getinfo($ch);
  12. curl_close($ch);
  13. if($retinfo['http_code']==200){
  14. $data = json_decode($ret, true);
  15. print_r($data);
  16. }else{
  17. echo 'POST Fail';
  18. }
  19. ?>

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

file_get_contents 请求的程序修改为:

  1. <?php
  2. $url = 'http://localhost/server.php';
  3. $param = array('content'=>'fdipzone blog');
  4. $auth = sprintf('Authorization: Basic %s', base64_encode('fdipzone:654321')); // 加入这句
  5. $opt = array(
  6. 'http' => array(
  7. 'method' => 'POST',
  8. 'header' => "content-type:application/x-www-form-urlencoded\r\n".$auth."\r\n", // 把$auth加入到header
  9. 'content' => http_build_query($param)
  10. )
  11. );
  12. $context = stream_context_create($opt);
  13. $ret = file_get_contents($url, false, $context);
  14. if($ret){
  15. $data = json_decode($ret, true);
  16. print_r($data);
  17. }else{
  18. echo 'POST Fail';
  19. }
  20. ?>

源码下载地址:点击查看

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持脚本之家!

人气教程排行