博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
php实现请求分流
阅读量:7009 次
发布时间:2019-06-28

本文共 2095 字,大约阅读时间需要 6 分钟。

一个请求,同时分发到多个服务器,

正常的是: A ============>  B

现在想实现的是:  

                                    --------------> C

        A   ======>  B   ---------------> D

          ---------------> E

如果是 GET请求,就处理一下 URL请求即可,但 POST 请求,还需要处理数据,

处理数据:

    如果是键值对方式的,使用 $_REQUEST 获取整个键值对;

$post_data = $_REQUEST;   //则会获取 整个请求中的键值对,返回结果为数组;

             如果是以流的方式的,则使用:

$post_data = file_get_contents("php://input");

 

获取完数据后,就用代码来进行转发,需要使用 curl:

** 如果需要根据进来的 url进行判断,可以使用 $_SERVER['REQUEST_URI']; // 获取的是 url中,domain后的部分,如: https://www.google.com/abc.php  ==>  /abc.php

array( 'method' => 'POST', 'header' => 'Content-type:application/x-www-form-urlencoded', 'content' => $postdata, 'timeout' => 15 * 60 ) ); $context = stream_context_create($options); $result = file_get_contents($url, false, $context); return $result; } /** * 发送post请求,而且data是流的形式 */ function send_post_content($url, $postdata) { $options = array( 'http' => array( 'method' => 'POST', 'header' => 'Content-type:application/x-www-form-urlencoded', 'content' => $postdata, 'timeout' => 15 * 60 ) ); $context = stream_context_create($options); $result = file_get_contents($url, false, $context); return $result; } // 转发键值对请求 if (isset($_REQUEST) && !empty($_REQUEST)) { $url1 = "http://test1.php"; $url2 = "http://test2.php"; $request = $_REQUEST; echo send_post($url1, $request); /*echo*/ send_post($url2, $request); } else { // 转发流请求 $url3 = "http://test3.php"; $url4 = "http://test4.php"; $request = file_get_contents("php://input"); // $_REQUEST; echo send_post_content($url1, $request); /*echo*/ send_post_content($url2, $request); }

 

转载地址:http://qqttl.baihongyu.com/

你可能感兴趣的文章
c关键字控制语句运算符小结
查看>>
初学Oracle的笔记(1)——基础内容(实时更新中..)
查看>>
(转)Ogre 安装 配置 问题
查看>>
【转】清理浮动的全家
查看>>
ZJOI2006 物流运输trans
查看>>
WinForm编程数据视图之DataGridView浅析(续)
查看>>
vim中实现括号和引号自动补全
查看>>
linux下的块设备驱动(一)
查看>>
Git-Flow
查看>>
mysql 5.7 安装手册(for linux)
查看>>
7.1-7.31推荐文章汇总
查看>>
ubuntu16.4中安装samba服务
查看>>
IT Operations(IT 运营),运维的更价值化认识
查看>>
eclipse启动时虚拟机初始内存配置
查看>>
在Linux用户空间做内核空间做的事情
查看>>
Chapter 1 First Sight——29
查看>>
SSH的jar包下载地址
查看>>
Solve VS2010 Error "Exceptions has been thrown by the target of an invocation"
查看>>
点击发送短信剩余秒数
查看>>
nodejs01
查看>>