Hi,
I am modifying a ipn script for my use and want to fully understand what it is doing. It seems to me that it is trying to do a cUrl and if that fails it uses a fsockopen function.
Here are the two functions that I am talking about:
The main bit of the program starts with:
Then it starts getting the variables:
I guess that these variables are picked up using either of the two
functions - hence my questions about why they are being used.
These are the question that I have about it:
1) This script seems to "prefer" the Curl function - Is it
better method? why ?
2) Since the script will be running on my server, so long as I have curl
installed - Why should it not work ?
3) Following on from 2 - do I need a back up function ?
4) Should I keep both functions or scrap one of them, in order to concentrate on one of them ? ( and which one ? )
Thanks for any comments and help with this :)
I am modifying a ipn script for my use and want to fully understand what it is doing. It seems to me that it is trying to do a cUrl and if that fails it uses a fsockopen function.
Here are the two functions that I am talking about:
Code:
function doTheCurl () {
$req = 'cmd=_notify-validate';
foreach ($_POST as $key => $value) {
$value = urlencode(stripslashes($value));
$req .= "&$key=$value";
}
$ch = curl_init();
// check to see if this is sandbox or not
if ($_POST["test_ipn"] == 1){
curl_setopt($ch, CURLOPT_URL, "https://www.sandbox.paypal.com/cgi-bin/webscr");
}
else
{
curl_setopt($ch, CURLOPT_URL, "https://www.paypal.com/cgi-bin/webscr");
}
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $req);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
$paypal_response = curl_exec ($ch);
curl_close($ch);
return $paypal_response;
} // end function
function doTheHttp () {
$req = 'cmd=_notify-validate';
foreach ($_POST as $key => $value){
$value = urlencode(stripslashes($value));
$req .= "&$key=$value";
}
// post back to PayPal system to validate
$header .= "POST /cgi-bin/webscr HTTP/1.0\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: " . strlen($req) . "\r\n\r\n";
// check to see if this is sandbox or not.
if ($_POST["test_ipn"] == 1) {
$paypal_response = fsockopen ('www.sandbox.paypal.com', 80, $errno, $errstr, 30);
}
else {
$paypal_response = fsockopen ('www.paypal.com', 80, $errno, $errstr, 30);
}
if (!$paypal_response) {
return "ERROR";
}
else
{
fputs ($paypal_response, $header . $req);
while (!feof($paypal_response)) {
$res = fgets ($paypal_response, 1024);
if (strcmp ($res, "VERIFIED") == 0) {
return "VERIFIED";
}
else if (strcmp ($res, "INVALID") == 0) {
return "INVALID";
}
}
fclose ($paypal_response);
}
return "ERROR";
} // end function
$paypal_respons e = doTheCurl();
if (!$paypal_respo nse) {
$paypal_respons e = doTheHttp();
}
else
{
echo "We have an Error";
}
if (!$paypal_respo nse) {
$paypal_respons e = doTheHttp();
}
else
{
echo "We have an Error";
}
Code:
//get variables $receiver_email = $_POST['receiver_email']; $first_name = $_POST['first_name']; etc etc
functions - hence my questions about why they are being used.
These are the question that I have about it:
1) This script seems to "prefer" the Curl function - Is it
better method? why ?
2) Since the script will be running on my server, so long as I have curl
installed - Why should it not work ?
3) Following on from 2 - do I need a back up function ?
4) Should I keep both functions or scrap one of them, in order to concentrate on one of them ? ( and which one ? )
Thanks for any comments and help with this :)
Comment