12

I receive a POST request at my PHP script and would like to forward this POST call to another script using POST too. How can I do this?

I can use cURL if it's required for this action.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Tom Smykowski
  • 25,487
  • 54
  • 159
  • 236

4 Answers4

13

Perhaps:

curl_setopt($ch, CURLOPT_POSTFIELDS, $_POST);

From curl_setopt:

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.
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Natalie Adams
  • 1,841
  • 22
  • 27
  • Have you tried this before? With the version of CURL I use, this will send the fields in "multipart/form-data", not a regular post. – ZZ Coder Apr 28 '10 at 00:41
  • I have never tried it before, but the PHP documentation does do a good job of documenting the functionality. – Natalie Adams Apr 29 '10 at 19:19
  • 2
    This is the answer most of the times. However, if you have deep variables passed in the post content (e.g. "...&var1[var2]=val&...") it won't work (`var1` will be passed as an empty array). ZZCoder's answer below (using `http_build_query()`) is the (complete) right answer. – zeh Oct 11 '12 at 15:35
13

Do this,

curl_setopt($handle, CURLOPT_POSTFIELDS, http_build_query($_POST));
ZZ Coder
  • 74,484
  • 29
  • 137
  • 169
  • This did the trick! Thank you. http_build_query() is necessary, otherwise it won't work. – acme Oct 25 '10 at 15:37
1

Here's a fully functional cURL request that re-routes $_POST where you want (based on ZZ coder's reply)

    $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);
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Robert Sinclair
  • 4,550
  • 2
  • 44
  • 46
0
<?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 authentication

    $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 requesting 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 HTTP authentication is required , use the below lines
        CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
        CURLOPT_USERPWD  => $username. ':' . $password
    ));
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131