[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Low
  • Security: Public
  • Views: 32
  • Last Modified:

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?
0
Mlungisi Ndlela
Asked:
Mlungisi Ndlela
  • 5
  • 3
  • 2
  • +1
2 Solutions
 
Dave BaldwinFixer of ProblemsCommented:
You can only get that if the first site relays it to you.
0
 
Mlungisi NdlelaFounder of MCSIDevelopers also a software developerAuthor Commented:
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.
0
 
Chris StanyonCommented:
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'];
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
mohan singhWeb developerCommented:
if you know the  witch variable/parameter  post to another page then do like this

<?php
if (isset($_POST['variable/parameter'])) {
   $id = $_POST[''variable/parameter'];
echo $id ;
}else{
    echo "ERROR";
}
0
 
Mlungisi NdlelaFounder of MCSIDevelopers also a software developerAuthor Commented:
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

0
 
Chris StanyonCommented:
OK. Firstly, turn on error reporting. You;ll then get an idea of what's broken. Add this to the top of your script:

error_reporting(E_ALL);
ini_set('display_errors', 1);

Turning on the error reporting will inform you that you're trying to write an array to your text file and that will fail. You need someway of converting your array into a string. print_r is a good candidate.

There's absolutely no need to assign POST to pfData and then pfData to txt ! Just use POST.

Take a look at this. You should build in some error checking but this is the general idea:

<?php
$myfile = fopen("payfastPost.txt", "w") or die("Unable to open file!");
fwrite($myfile, print_r($_POST, true));
fclose($myfile);

switch( $_POST['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!");
        fwrite($myfile, print_r($_POST, true));
        fclose($myfile);
        break;
    
    case 'FAILED':                    
        // There was an error, update your application
        $myfile = fopen("payfastFailed.txt", "w") or die("Unable to open file!");
        fwrite($myfile, print_r($_POST, true));
        fclose($myfile);
        break;

    default:
        // If unknown status, do nothing (safest course of action)

endswitch;

Open in new window

0
 
mohan singhWeb developerCommented:
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
0
 
Mlungisi NdlelaFounder of MCSIDevelopers also a software developerAuthor Commented:
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.
0
 
Mlungisi NdlelaFounder of MCSIDevelopers also a software developerAuthor Commented:
Managed. Thanks everyone.
0
 
Mlungisi NdlelaFounder of MCSIDevelopers also a software developerAuthor Commented:
Thanks to everyone who offered help. You have help me solve this problem. Thanks.
0
 
Chris StanyonCommented:
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
0

Join & Write a Comment

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 5
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now