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

php curl 扩展,可以在server端发起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']这两个server參数。

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

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 password:654321

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

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

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 假设要发送帐号和password,须要手动拼接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';
}
?>

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

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

curl 要么 file_get_contents 获得授权页面的方法的必要性的更多相关文章

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

    原文:http://blog.csdn.net/fdipzone/article/details/44475801 红色字体部分是加上自己的注释,整理了一下. 今天因工作需要,需要用 curl / f ...

  2. 解析PHP中的file_get_contents获取远程页面乱码的问题【转】

    在工作中,遇到一个问题.我需要将一个网址(该网址是一个json数据的接口,即 打开该网址,在浏览器中显示的是json数据),我使用file_get_contents($url),数据是乱码的. 通过查 ...

  3. php 采集类snoopy http://www.jb51.net/article/27568.htm | cURL、file_get_contents、snoopy.class.php 优缺点

    Snoopy是一个php类,用来模拟浏览器的功能,可以获取网页内容,发送表单. Snoopy的特点: 1.抓取网页的内容 fetch 2.抓取网页的文本内容 (去除HTML标签) fetchtext ...

  4. PHP cURL实现模拟登录与采集使用方法详解教程

    来源:http://www.zjmainstay.cn/php-curl 本文将通过案例,整合浏览器工具与PHP程序,教你如何让数据 唾手可得 . 对于做过数据采集的人来说,cURL一定不会陌生.虽然 ...

  5. cURL和file_get_contents实现模拟post请求

    以前面试时候,面试官问过我后端有没有跨域问题,但是不敢肯定,现在可以肯定的说没有. 不文用php的cURL和file_get_contents方法分别实现后端跨域.本文场景也是在tp5下实现的. 一, ...

  6. PHP CURL或file_get_contents获取网页标题的代码及两者效率的稳定性问题

    PHP CURL与file_get_contents函数都可以获取远程服务器上的文件保存到本地,但在性能上面两者完全不在同一个级别,下面我先来介绍PHP CURL或file_get_contents函 ...

  7. PHP使用curl替代file_get_contents

    初学php的朋友们,很容易翻一个错误,在写采集程序或者调用api接口总会有线考虑到使用file_get_contents函数来或许内容,程序的访问量不大倒是没什么影响,但是访问量提升了那非常的悲剧了, ...

  8. 远程读取URL 建议用curl代替file_get_contents

    初学php的朋友们,很容易翻一个错误,在写采集程序或者调用api接口总会有线考虑到使用file_get_contents函数来或许内容,程序的访问量不大倒是没什么影响,但是访问量提升了那非常的悲剧了, ...

  9. 接入天猫精灵auth2授权页面https发送ajax请求

    已存在一个应用A,采用的是http交互, 在接入天猫精灵时,要求请求类型是https,所以在应用服务前加了个nginx转发https请求.在绑定授权页面,会发送ajax请求验证用户名和密码,采用htt ...

随机推荐

  1. python开发_tkinter_小球完全弹性碰撞游戏

    python开发_tkinter_小球完全弹性碰撞游戏   完成这个小球的完全弹性碰撞游戏灵感来自于: 下面是我花了一周下班时间所编写的一个小球完全弹性碰撞游戏: 游戏初始化状态: 最下面的游标和修改 ...

  2. win7提示“ipconfig不是内部或外部命令”

    进入windows环境变量设置->系统变量,找到path,添加C:\Windows\SysWOW64,或者c:\windows\system32

  3. sed中求公共前缀

    string1="test toast" string2="test test" printf "%s\n%s\n" "$stri ...

  4. WPF 各种基础动画实现

    C# using System; using System.Collections.Generic; using System.Linq; using System.Text; using Syste ...

  5. 软件測试系统文章(文件夹&amp;链接在此)

    前言 我会在此账号上写一系列关于软件測试的文章,故在此置顶软件測试系列文章的文件夹和链接,以方便大家阅读! 文件夹 软件測试系列之入门篇(一) 软件測试系列之了解篇(二) 软件測试系列之黑白盒(三) ...

  6. android studio下gradle与Git错误解决方法

    Error: Gradle: Execution failed for task ':mytask' > A problem occurred starting process 'command ...

  7. 递归遍历XML所有节点

    package xml; import org.dom4j.Document; import org.dom4j.DocumentHelper; import org.dom4j.DocumentEx ...

  8. [PATCH] UBUNTU: SAUCE: (no-up) apparmor: Sync to apparmor3 - RC1(v3.4.x kernel)

    ubuntu touch v3.4 kernel AppArmor v3 backport patch 地址1:https://github.com/multirom-aries/ubuntu-pho ...

  9. 移植kl档,但调用默认Generic.kl解决的方法

    从飞机到现在移植模型来开发遥控器按键,调查发现,大部分的功能键始终没有任何效果. 跑 dumpsys input 要查看当前的关键kl, 查找KeyLayoutFile它原来是默认Generic.kl ...

  10. HTML5_表单元素

    <!DOCTYPE html> <hmtl> <html  lang="zh-cn"> <head> <meta charse ...