PayPal sandbox ipn status never verified

Hi All,
I’m testing a PayPal ipn listener and it seems it NEVER returns VERIFIED status… Here’s the script :


//read the post from PayPal system and add 'cmd'
$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.1\\r\
";
$header .= "Content-Type: application/x-www-form-urlencoded\\r\
";
$header .= "Host:www.sandbox.paypal.com\\r\
";
$header .= "Connection: close\\r\
";
$header .= "Content-Length: " . strlen($req) . "\\r\
\\r\
";
//$fp = fsockopen ('ssl://www.paypal.com', 443, $errno, $errstr, 30);
$fp = fsockopen ('ssl://www.sandbox.paypal.com', 443, $errno, $errstr, 30);
//

//error connecting to paypal
if (!$fp) {
    //
}

//successful connection
if ($fp) {
    fputs ($fp, $header . $req);

    //while (!feof($fp)) {
    $res = stream_get_contents($fp, 1024);
        //$res = fgets ($fp, 1024);
        $res = trim($res); //NEW & IMPORTANT

        if (strcmp($res, "VERIFIED") == 0) {
            // if status is COMPLETED insert order into database
            if ($_POST['payment_status'] == "Completed") {
                $subject = 'Instant Payment Notification - COMPLETED';
                 $to = 'my_email_address@gmail.com';    //  your email
                 $body =  "An instant payment notification was successfully recieved\
";
                 $body .= "from ".$_POST['payer_email']." on ".date('m/d/Y');
                 $body .= " at ".date('g:i A')."\
\
Details:\
";

                 foreach ($_POST as $key => $value) { $body .= "\
".$key." : ".$value; }
                 mail($to, $subject, $body);
            }
        }

        else {
            //insert into DB in a table for bad payments for you to process later
            $subject = 'Instant Payment Notification - ELSE status';
             $to = 'my_email_address@gmail.com';    //  your email
             $body =  "Else clause \
";
             $body .= "from ".$_POST['payer_email']." on ".date('m/d/Y');
             $body .= " at ".date('g:i A')."\
\
Details:\
";

             foreach ($_POST as $key => $value) { $body .= "\
".$key." : ".$value; }
             mail($to, $subject, $body);
        }
    //}

    fclose($fp);
}


I’ve tried the code with while (!feof($fp)) and $res = fgets ($fp, 1024)
I never get the VERIFIED email and I’m even not sure if it is a coding issue… That’s why I’m asking here : maybe some of you had this problem before and can help me. Thanks in advance.

Try putting the fputs right after $fp = fsockopen line, then look to see what is in the file.