Jump to content

[SOLVED] ob_get_contents() help


xrgt

Recommended Posts

Hi, please help this PHP newbie.

 

How can i strip the following output and access the variables via an array?

 

The buffered output i'm expecting is always in the following format:

 

<html><body>
processingError=
<br>
processingErrorMessage=
<br>
transactionApproved=
<br>
responseCode=
<br>
transactionNo=
<br>
receiptNo=
<br>
merchantTXRef=
<br>
transactionDeclined=
<br>
creditCardExpired=
<br>
insufficientFunds=
</body></html>

 

This is the Code I have to work with:

 

// Get result
  $dilResponse = ob_get_contents();
// Finish with the buffer
  ob_end_clean(); 
// Check for errors
  if(strchr($dilResponse,"<html>")) $errorMessage = $dilResponse;
  else if(curl_error($clientURL)) $errorMessage = "CURL ERROR: " . curl_errno($clientURL) . " " . curl_error($clientURL);
// Close the connection
  curl_close($clientURL);

$responseKeyVals = split("&", $dilResponse);

     foreach ($responseKeyVals as $val) {
          $param = split("=", $val);
          $responseArray[urldecode($param[0])] = urldecode($param[1]);
     }

// Process the results and determine the transactions status
$transactionResponse = $responseArray['responseCode'];

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/40092-solved-ob_get_contents-help/
Share on other sites

 

In Java i'd do it this way - PHP i have no idea, can anyone assist?:

 

resp = new BufferedReader(new InputStreamReader(gateway.openStream()));

        resp.readLine();
    String inputLine2=resp.readLine();
        resp.readLine();
    String inputLine4=resp.readLine();
        resp.readLine();
                String inputLine6=resp.readLine();
        resp.readLine();
                String inputLine8=resp.readLine();
//and so on...

paymentDetail.setResponseCode(inputLine8.substring(13,inputLine8.length()));
paymentDetail.setTransactionNo(inputLine10.substring(14,inputLine10.length()));
paymentDetail.setReceiptNo(inputLine12.substring(10,inputLine12.length()));

Ehhh Java is a foreign language to me :( (trying to learn it!).

 

Anyways I came up with:

<?php
//$assume content is the content of the page
$content2 = str_replace(array("<html>", "<body>", "<br>"), "", $content);
$content_lines = explode("\r\n", $content2);
if(count($content_lines) < 1) {
$content_lines = explode("\n", $content2);
}

$replaces = array(
'processingError=',
'processingErrorMessage',
'transactionApproved',
'responseCode=',
'transactionNo=',
'receiptNo=',
'merchantTXRef=',
'transactionDeclined=',
'creditCardExpired=',
'insufficientFunds='
);

foreach($content_lines as $k = $v) {
$$replaces[$k] = str_replace($replaces[$k], "", $v);
}
?>

 

What this will do is it will take the values from each line and assign them to a variable named the thing at the beginning.

 

For example if recieptNo was 7 then $receiptNo  would be 7.

 

This script isn't very good, and I'm hoping another PHPfreaks member that's more experienced than me with text manipulation can come along and help you out more....

 

If you want me to explain anything about the code just ask.

tested your example with '=>' fixing the syntax, and it does NOT work...

 

anyway, for anyone that is interested, this is what i came up with by modifying the existing code:

 

<?php
$content2 = str_replace(array("<html>", "<body>", "<br>"), "", $dilResponse);

			$responseKeyVals = split("\r\n", $content2);
		    foreach ($responseKeyVals as $val) {
		        $param = split("=", $val);
		        $responseArray[urldecode($param[0])] = urldecode($param[1]);
		    }
?>
<?php echo $responseArray['responseCode']; ?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.