今天,需要工作,需要使用 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. Windows phone 8 学习笔记(1) 触控输入

    原文:Windows phone 8 学习笔记(1) 触控输入 Windows phone 8 的应用 与一般的Pc应用在输入方式上最大的不同就是:Windows phone 8主要依靠触控操作.因此 ...

  2. vim netrw

    我们现在试一下vim文件功能,当你使用vim尝试打开目录时,vim会自动调用netrw.vim插件打开该目录(从操作系统的视角来看,目录其实是一种特殊的文件).例如,我们在vim中执行命令”:e -/ ...

  3. Android该HTTP下载

    今天学习了Android开发中比較难的一个环节,就是断点续传下载,非常多人看到这个标题就感觉头大.的确,假设没有良好的逻辑思维,这块的确非常难搞明确.以下我就将自己学到的知识和一些见解写下供那些在这个 ...

  4. Windows远程桌面连接Ubuntu 14.04 (转)

    由于xrdp.gnome和unity之间的兼容性问题,在Ubuntu 14.04版本中仍然无法使用xrdp登陆gnome或unity的远程桌面,现象是登录后只有黑白点为背景,无图标也无法操作.与13. ...

  5. Linux命令对应的全称解释(转)

    Filesystem Hierarchy Standard(FHS) /:根目录,所有的目录.文件.设备都在/之下,/就是Linux文件系统的组织者,也是最上级的领导者.  /bin:bin 就是二进 ...

  6. android如何使用自己定义JNI接口,以及NDK环境建设和使用的工具。

    一.建筑环境: 1.需要的工具: A.cygwin    (在windows下使用LINUX系统)                                                   ...

  7. eclipse 于 Tomcat于 热部署 project

    eclipse在 Tomcat中热部署project 1.在eclipse中国安装一个tomcat插件:SysdeoEclipse Tomcat Launcher plugin(http://www. ...

  8. 【从翻译mos文章】Weblogic AdminServer 启动fail,报错&quot;unable to get file lock, will retry&quot;

    Weblogic AdminServer 启动fail,报错"unable to get file lock, will retry" 参考原始: Weblogic AdminSe ...

  9. WCF扩展之实现ZeroMQ绑定和protocolBuffer消息编码(三)实现ReplyChannel(2016-03-15 12:35)

    这是这个系列的第三篇,其他的文章请点击下列目录 WCF扩展之实现ZeroMQ绑定和protocolBuffer消息编码(一)概要设计 WCF扩展之实现ZeroMQ绑定和protocolBuffer消息 ...

  10. hdu 1251 统计拼图

    二手tire木: Basic应用程序 谈到很具体的 点击打开链接 #include<cstdio> #include<cstring> #include<iostream ...