PHP Implementation Request shunt

Source: Internet
Author: User

A request, distributed to multiple servers at the same time,

The normal is: A ============> B

What you want to achieve now is:

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

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

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

If it's a GET request, just process the URL request, but the POST request also needs to process the data,

Working with Data:

If it is a key-value pair, use $_request to get the entire key-value pair;

$post _data $_request;   // a key-value pair is obtained from the entire request, and the result is an array;

If it is in a stream, then use:

$post _data file_get_contents ("Php://input");

Once the data has been obtained, the code is used to forward it, and curl is required:

* * $_server[' Request_uri ' can be used if it is necessary to judge by the incoming URL; Gets the part of the URL, after domain, such as: https://www.google.com/abc.php ==>/abc.php

<?PHP/** * Send a POST request, and data is a key-value pair form*/    functionSend_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; }    /** * Send a POST request, and data is in the form of a stream*/    functionSend_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; }    //forwarding a key-value pair request    if(isset($_request) &&!Empty($_request)) {        $url 1= "http://test1.php"; $url 2= "http://test2.php"; $request=$_request; EchoSend_post ($url 1,$request); /*Echo*/Send_post ($url 2,$request); }     Else {        //forward Stream Request        $url 3= "http://test3.php"; $url 4= "http://test4.php"; $request=file_get_contents("Php://input");//$_request;        EchoSend_post_content ($url 1,$request); /*Echo*/Send_post_content ($url 2,$request); }

PHP Implementation Request shunt

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.