zoukankan      html  css  js  c++  java
  • php实现请求分流

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

    正常的是: 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);
        }
  • 相关阅读:
    pair
    非整除集合
    集合 set
    实现字通配符*
    vector
    矩阵及其初等变换
    求数组中连续子数组(最少有一个元素)的最大和。
    最长上升序列(Lis)
    st表求区间最大值
    [Noip2015] 信息传递
  • 原文地址:https://www.cnblogs.com/tommy-huang/p/9159203.html
Copyright © 2011-2022 走看看