Page 1 of 1

How to print "raw" $_POST data

Posted: Fri Jul 31, 2009 2:14 pm
by steve9876
Is there a way to capture the "raw" $_POST value, exactly as it is received? print_r "cleans up" the data, so it's not what I want. Also, this has to be assigned to a string, which I'll then write to a file.

I'm working on a PayPal "IPN Listener," which requires that the $_POST values be sent back to PayPal "exactly" as they were received. I'm getting "INVALID" responses from PayPal, so I want to see if what I'm sending them is subtly different from what I'm receiving.

Re: How to print "raw" $_POST data

Posted: Fri Jul 31, 2009 2:25 pm
by joeynovak
I don't know if there is a way to actually get the RAW post data or not, but here is an excerpt from a script I found (It's GPL code), available here: http://www.micahcarrick.com/04-19-2005/ ... class.html

It's a little old, so it may not work, but I am 99% sure if you follow what he does, where he does the foreach on the return data, it should work.

Hope this helps,

Joey

Code: Select all

  function validate_ipn() {
 
      // parse the paypal URL
      $url_parsed=parse_url($this->paypal_url);        
 
      // generate the post string from the _POST vars aswell as load the
      // _POST vars into an arry so we can play with them from the calling
      // script.
      $post_string = '';    
      foreach ($_POST as $field=>$value) { 
         $this->ipn_data["$field"] = $value;
         $post_string .= $field.'='.urlencode(stripslashes($value)).'&'; 
      }
      $post_string.="cmd=_notify-validate"; // append ipn command
 
      // open the connection to paypal
      $fp = fsockopen($url_parsed[host],"80",$err_num,$err_str,30); 
      if(!$fp) {
          
         // could not open the connection.  If loggin is on, the error message
         // will be in the log.
         $this->last_error = "fsockopen error no. $errnum: $errstr";
         $this->log_ipn_results(false);       
         return false;
         
      } else { 
 
         // Post the data back to paypal
         fputs($fp, "POST $url_parsed[path] HTTP/1.1\r\n"); 
         fputs($fp, "Host: $url_parsed[host]\r\n"); 
         fputs($fp, "Content-type: application/x-www-form-urlencoded\r\n"); 
         fputs($fp, "Content-length: ".strlen($post_string)."\r\n"); 
         fputs($fp, "Connection: close\r\n\r\n"); 
         fputs($fp, $post_string . "\r\n\r\n"); 
 
         // loop through the response from the server and append to variable
         while(!feof($fp)) { 
            $this->ipn_response .= fgets($fp, 1024); 
         } 
 
         fclose($fp); // close connection
 
      }
      
      if (eregi("VERIFIED",$this->ipn_response)) {
  
         // Valid IPN transaction.
         $this->log_ipn_results(true);
         return true;       
         
      } else {
  
         // Invalid IPN transaction.  Check the log for details.
         $this->last_error = 'IPN Validation Failed.';
         $this->log_ipn_results(false);   
         return false;
         
      }
      
   }

Re: How to print "raw" $_POST data

Posted: Fri Jul 31, 2009 2:29 pm
by requinix
$_POST will have the right data.

My company's (working) code is

Code: Select all

    $postback = 'cmd=_notify-validate';
    foreach($_POST as $key => $value)
        $postback .= "&$key=" . urlencode(stripslashes($value));
 
 
 
    // Now, let's send that data back to PayPal over a secure line
    $ch = curl_init();
 
    if($ch){
    curl_setopt($ch, CURLOPT_URL, "https://www.paypal.com/cgi-bin/webscr");
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postback);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    ob_start();
    curl_exec($ch);
    $res = trim(ob_get_contents());
    ob_clean();}
It then scans $res for the word "VERIFIED".

The bit up with $postback should be identical to

Code: Select all

$_POST["cmd"] = "_notify-validate";
$postback = http_build_query($_POST);

(To get raw POSTed data you can file_get_contents("php://stdin").)