How to forward $_POST with PHP and cURL?

前端 未结 4 1937
长发绾君心
长发绾君心 2021-01-11 13:51

I receive POST request at my PHP script and would like to forward this post call to another script using POST too. How to do this? I can use cURL if it\'s required for this

相关标签:
4条回答
  • 2021-01-11 14:20

    If anyone needs this, here's a fully functional cURL request that re-routes $_POST where you want (based on ZZ coder's reply above)

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, "http://urlOfFileWherePostIsSubmitted.com");
        curl_setopt($ch, CURLOPT_POST, TRUE);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        // ZZ coder's part
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($_POST));
        $response = curl_exec($ch);
        curl_close($ch);
    
    0 讨论(0)
  • 2021-01-11 14:30
    <?php
    
    function executeCurl($arrOptions) {
    
        $mixCH = curl_init();
    
        foreach ($arrOptions as $strCurlOpt => $mixCurlOptValue) {
            curl_setopt($mixCH, $strCurlOpt, $mixCurlOptValue);
        }
    
        $mixResponse = curl_exec($mixCH);
    
        curl_close($mixCH);
    
        return $mixResponse;
    
    }
    
    // if need any http auth
    
    $username = 'http-auth-username';
    $password = 'http-auth-password';
    
    $requestType = 'POST'; // this can be PUT or POST
    
    // this can be $arrPostData = $_POST;
    $arrPostData = array(
        'key1'  => 'value-1-for-k1y-1',
        'key2'  => 'value-2-for-key-2',
        'key3'  => array(
                'key31'   => 'value-for-key-3-1',
                'key32'   => array(
                    'key321' => 'value-for-key321'
                )
        ),
        'key4'  => array(
            'key'   => 'value'
        )
    );
    
    // you can set your post data
    $postData = http_build_query($arrPostData); // raw php array
    
    $postData = json_encode($arrPostData); // Only USE this when request json data
    
    $arrResponse = executeCurl(array(
        CURLOPT_URL => 'http://whatever-your-request-url.com/xyz/yii',
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_HTTPGET => true,
        CURLOPT_VERBOSE => true,
        CURLOPT_AUTOREFERER => true,
        CURLOPT_CUSTOMREQUEST => $requestType,
        CURLOPT_POSTFIELDS  => $postData,
        CURLOPT_HTTPHEADER  => array(
            "X-HTTP-Method-Override: " . $requestType,
            'Content-Type: application/json', // Only USE this when request json data
        ),
        // if required HTTP Authentication use below lines
        CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
        CURLOPT_USERPWD  => $username. ':' . $password
    ));
    
    0 讨论(0)
  • 2021-01-11 14:34

    Perhaps:

    curl_setopt($ch, CURLOPT_POSTFIELDS, $_POST);
    

    http://www.php.net/manual/en/function.curl-setopt.php

    This can either be passed as a urlencoded string like 'para1=val1&para2=val2&...' or as an array with the field name as key and field data as value.
    0 讨论(0)
  • 2021-01-11 14:45

    Do this,

    curl_setopt($handle, CURLOPT_POSTFIELDS, http_build_query($_POST));
    
    0 讨论(0)
提交回复
热议问题