Link to home
Start Free TrialLog in
Avatar of Mlungisi Ndlela
Mlungisi NdlelaFlag for South Africa

asked on

Get posted data from another url in php

Is there anyone know how could you get posted data from another website. I have a site that needs to get some pass through data that is returned by the notify_URL so when I read the engines documentation they send data by means of post so I need to listing and be able to get the variables as well as data passed from that url. How can I achieve that in php?
Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

You can only get that if the first site relays it to you.
Avatar of Mlungisi Ndlela

ASKER

Mmm I'm not sure what you mean. Basically whats happens is that the gateway is a third party system which I pass some data to it of which some data are just passed where the gateway acts as a bridge for that data to another engine which will use that data, so when the gateway is done and the job was successful it calls 2 URLs one is Notify_URL and the other is Return_URL where Notify is called behind the scenes and other jobs are done by an engine on that URL and the user is redirected to the Return_URL. So I want to get these returned data from the gateway to this Notify_URL.

I've just saw this so I'm trying it out and see if its will help me.
If a POST request is made to your Notify_URL, then in PHP you will have access to the data in the $_POST array. You can view the whole POST array by dumping the data:

var_dump($_POST);

If you know the names of the keys (fields) that are sent, then you can access them directly.

$_POST['someKey'];

If you want to output the value, simply echo:

echo $_POST['someKey'];
SOLUTION
Avatar of mohan singh
mohan singh
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Well the problem is that it seems as if there is no data posted by payfast to our notify_url but payfast does call our notify_url but we don't get any data. Here is how I tried to get whatever was posted by payfast to use and write that to the textfile but the text file is always created but its empty
header( 'HTTP/1.0 200 OK' );
flush();
$custuser = $_GET["custom_str1"];


// Posted variables from ITN
$pfData = $_POST;
$myfile = fopen("payfastPost.txt", "w") or die("Unable to open file!");
$txt = $pfData;
fwrite($myfile, $txt);
fclose($myfile);
//update db
switch( $pfData['payment_status'] )
{
 case 'COMPLETE':
    // If complete, update your application, email the buyer and process the transaction as paid     
    $myfile = fopen("payfastComplete.txt", "w") or die("Unable to open file!");
$txt = $pfData;
fwrite($myfile, $txt);
fclose($myfile);
 break;
 case 'FAILED':                    
    // There was an error, update your application
    $myfile = fopen("payfastFailed.txt", "w") or die("Unable to open file!");
$txt = $pfData;
fwrite($myfile, $txt);
fclose($myfile);
 break;
 default:
    // If unknown status, do nothing (safest course of action)
 break;
}

Open in new window

ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Dear Mlungisi Ndlela,
 when you use GET method so please check
that get data is set or not
if it does't work
then you use
curl  method
Thank you chris. I am able to see the data now. So now How can I get each data within? I mean do I have to create a new variable that will output these data in a print_r function? Here is the data returned:

I've copied exactly as it was written on the file by the script.

Array
(
    [m_payment_id] => 
    [pf_payment_id] => 578822
    [payment_status] => COMPLETE
    [item_name] => Product1
    [item_description] => Product1 description
    [amount_gross] => 5.00
    [amount_fee] => -2.28
    [amount_net] => 2.72
    [custom_str1] => test1
    [custom_str2] => xx
    [custom_str3] => 
    [custom_str4] => myworld
    [custom_str5] => 0000000011
    [custom_int1] => 5
    [custom_int2] => 1
    [custom_int3] => 
    [custom_int4] => 
    [custom_int5] => 
    [name_first] => Test
    [name_last] => User 01
    [email_address] => sbtu01@payfast.co.za
    [merchant_id] => 10007327
    [signature] => bd03275f7af84ad5984e77455c839acc
)

Open in new window


my interest or what I want to get is custom_str1, custom_str4, custom_str5, custom_int1.
Managed. Thanks everyone.
Thanks to everyone who offered help. You have help me solve this problem. Thanks.
OK. So that shows you exactly what's in the POST array, and you can acces those values directly using the keys, such as $_POST['custom_str1'], $_POST['custom_str4'] etc.

If you need to output any of those values individually, you would pass them into the fwrite function, so you could do:

fwrite( $myfile, $_POST['custom_str1'] );

That would just write the value to the text file. If you need to format it rather than just echo it's value, then you could use the sprintf function, so you may end up with something like this:

$myfile = fopen("payfastComplete.txt", "w") or die("Unable to open file!");
fwrite( $myfile, sprintf("Custom String 1: %s" . PHP_EOL, $_POST['custom_str1']) );
fwrite( $myfile, sprintf("Custom String 4: %s" . PHP_EOL, $_POST['custom_str4']) );
fwrite( $myfile, sprintf("Custom String 5: %s" . PHP_EOL, $_POST['custom_str5']) );
fwrite( $myfile, sprintf("Custom Int 1: %s" . PHP_EOL, $_POST['custom_int1']) );
fclose($myfile);

Open in new window

That would write the following to your text file:

Custom String 1: test1
Custom String 4: myworld
Custom String 5: 0000000011
Custom Int 1: 5