[nycphp-talk] Posting data to PayPal from a script using sockets
William E. Fisher
wefisher at open-world.com
Wed Jun 9 15:35:41 EDT 2004
Here is a question from someone (me) who is in way over his head.
I am in search of some help in posting form variables to a third party server from a script. The third party server is PayPal (sandbox) and the post would be the initial variables that are needed to initiate an IPN transaction.
I am trying sockets to connect to the paypal server. I had some success but not yet a complete transaction.
The following code is able to open a socket on the sandbox server and write the post header and data to the server. And I am able to get and read a response from the the sandbox server.
However, I am not able to complete the transaction.
Despite being logged into developer central, I receive the message "To access the PayPal Sandbox, please log in to PayPal Developer Central. " rather than the payment details page. No activity is recorded in the seller's account, and my ipn script at the notify_url is not hit.
Is someone familiar enough with either using sockets or using paypal that he/she might be able to provide some direction or a solution?
Any help will be much, much, much appreciated.
Thank you.
Bill
Here is the code:
//First I define the variables as key/value pairs in an
//array called $PayPal_Vars
//Next we need to provide the parameters for the
//fsockopen() and fputs() functions
foreach ($PayPal_Vars as $key => $value)
{
$value = stripslashes($value);
$value = urlencode($value);
if (!$req)
{
$req .= "$key=$value";
}
else
{
$req .= "&";
$req .= "$key=$value";
}
$req .= "\r\n\r\n";
}
$header .= "POST /cgi-bin/webscr HTTP/1.0\r\n";
$header .= "Host: www.sandbox.paypal.com\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: " . strlen($req) . "\r\n\r\n";
//Finally we need to script the call to the PayPal server
//and the posting of the data
if ($PayPal_Vars[test_ipn] == 1)
{
$fp = fsockopen ("www.sandbox.paypal.com", 80, $errno, $errstr, 30);
}
else
{
$fp = fsockopen ("www.paypal.com", 80, $errno, $errstr, 30);
}
if (!$fp)
{
return "ERROR";
}
else
{
$fout = fputs ($fp, $header . $req);
echo "<br>fout = $fout";
$tmp_headers = "";
while ($str = trim(fgets($fp, 4096)))
{
$tmp_headers .= $str."\n";
}
$tmp_body = "";
while ($str = trim(fgets($fp, 4096)))
{
$tmp_body .= $str."\n";
}
fclose($fp);
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.nyphp.org/pipermail/talk/attachments/20040609/33a631d4/attachment.html>
More information about the talk
mailing list