I want to verify IPN response from paypal, but for some reason I cannot read the response.
This is the code I'm trying to use:
$fp = fsockopen ('ssl://www.sandbox.paypal.com', 443, $errno, $errstr, 30);
if(!$fp)die();
fputs($fp, $header . $postback);
$res=stream_get_contents($fp); #this triggers 500 error
It does open a connection, does fputs
, but at the last line it hangs up and after some considerable amount of waiting returns 500 error
.
Any ideas what's wrong with it?
note: even if I comment the fputs
line the exactly same thing happens.
Headers:
$postback='cmd=_notify-validate&'.http_build_query($_POST);
// build the header string to post back to PayPal system to validate
$header = "POST /cgi-bin/webscr HTTP/1.1\r\n";
$header .= "Host: www.sandbox.paypal.com\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: " . strlen($postback) . "\r\n\r\n";
Edit
I tried to use curl, but it always gets me an empty response:
$ch = curl_init( 'https://www.sandbox.paypal.com/cgi-bin/webscr' );
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1) ;
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postback);
$strCurlResult = curl_exec($ch);
curl_close( $ch );
echo $strCurlResult;
The above code outputs nothing at all.