Search code examples
phpcurlphp-5.3

PHP: curl postfield parameters are not showing in return of target url


I have used curl in php and set parameter in "CURLOPT_POSTFIELDS". And I use return $_REQUEST/$_POST on the target url for checking my passed parameter are posted correctly. But I am not able to check the posted parameter in target page.

Example of target url:- http://www.eg.com/target

curl_setopt($ipnexec, CURLOPT_URL, "http://www.eg.com/target");

CODE

$clientId = "AXLAatA9ucEkGt2C9y5SuNRd24Ys4NPod8VJmNNFq5otso1RQRIn";
        $secret = "EGOojWJihcU8wnGTVQivKOsD_ylB5mMdaWmbn_1UWGlqbaugSCOZ";
        $post = array(
                        "key" =>  $clientId,
                        "secret" => $secret   
                    );

        $ipnexec = curl_init();

        curl_setopt($ipnexec, CURLOPT_URL, "http://www.eg.com/target"); 
        curl_setopt($ipnexec, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ipnexec, CURLOPT_POST, true);
        curl_setopt($ipnexec, CURLOPT_POSTFIELDS, json_encode($post));
        curl_setopt($ipnexec, CURLOPT_RETURNTRANSFER, true);

        $ipnresult = curl_exec($ipnexec);
        $result = json_decode($ipnresult);

Any helps !!

Regards Patrick


Solution

  • If you do have access to the endpoint at http://www.eg.com/target, you need to change the stream used. $_REQUEST is for form encoded data only. To access raw json, you need to use

    $data = file_get_contents('php://input');

    And then if you want to "overwrite $_POST, use

    $_POST = json_decode($data, true);