分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > IT知识

php实现请求分流

发布时间:2023-09-06 01:59责任编辑:白小东关键词:暂无标签

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

正常的是: 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

<?php ???/** ????* 发送post请求,而且data是键值对形式 ????*/ ???function send_post($url, $post_data) { ???????$postdata = http_build_query($post_data); ???????$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; ???} ???/** ????* 发送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); ???}

php实现请求分流

原文地址:https://www.cnblogs.com/tommy-huang/p/9159203.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved