Post Data and Receive Response from an external script

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
gcreddy
Forum Newbie
Posts: 10
Joined: Tue Sep 08, 2009 8:26 am

Post Data and Receive Response from an external script

Post by gcreddy »

Hello,

I am a novice coder trying to implement a payment processing portal. I have an account with a payment processing website.

I am gathering data (eg: card name, card number, expiration date etc.) and posting it to their CGI script with the test variables provided by them and I am getting a (parsed) response on their CGI script. However, I need to collect this reponse, come back to some page on my domain, write it into a databasse and generate some kind of a thank you message.

Can somebody please tell me how I can write a script (I mean...what functions / resources are used) to implement this type of functionality?

Thanks for reading my post.

Regards,
G.C.Reddy
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: Post Data and Receive Response from an external script

Post by s.dot »

Hi, You will need to post the variables like this...

Code: Select all

 
//generate the request you are POSTing to the payment processor
$req = '';
foreach ($_POST AS $k => $v)
{
    $req .= '&' . $k . '=' . urlencode($v);
}
 
header("POST /script.cgi HTTP/1.0");
header("Host: http://www.example.com");
header("Content-Type: application/x-www-form-urlencoded");
header("Content-Length: " . strlen($req));
 
//open a file stream
if ($fileStream = fsockopen('www.example.com', 80, $errno, $errstr, 30))
{
    //write the $_POST data to the file stream
    fwrite($fileStream, $headers . $req);
 
    //loop, attempting to read content sent back from payment processor
        $contents = '';
    while (!feof($fileStream))
    {
        //get the contents, 1kb at a time
        $contents .= fgets($fileStream, 1024);
     }
 
     //you now have what the payment processor sent back to you
     //so do something with it, like write it to the database
     echo $contents;
}
 
Edit: I did not check the code for errors but I do something similar for PayPal IPN requests.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: Post Data and Receive Response from an external script

Post by Darhazer »

The headers, send by header(), are send to the client, and not to the CGI script. You have to include them in the beginning of the $req, with 2 end-of-line characters to separate them from the body.
Using curl however is the easiest way to accomplish this.
Post Reply