xrgt Posted February 26, 2007 Share Posted February 26, 2007 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 More sharing options...
xrgt Posted February 26, 2007 Author Share Posted February 26, 2007 anybody? Link to comment https://forums.phpfreaks.com/topic/40092-solved-ob_get_contents-help/#findComment-194788 Share on other sites More sharing options...
xrgt Posted February 27, 2007 Author Share Posted February 27, 2007 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())); Link to comment https://forums.phpfreaks.com/topic/40092-solved-ob_get_contents-help/#findComment-194924 Share on other sites More sharing options...
corbin Posted February 27, 2007 Share Posted February 27, 2007 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. Link to comment https://forums.phpfreaks.com/topic/40092-solved-ob_get_contents-help/#findComment-194947 Share on other sites More sharing options...
xrgt Posted February 27, 2007 Author Share Posted February 27, 2007 thanks for the reply... i tried the example you gave me, but it has a syntax error?: Parse error: syntax error, unexpected '=', expecting ')' this occurs on/around "foreach($content_lines as $k = $v) {" Link to comment https://forums.phpfreaks.com/topic/40092-solved-ob_get_contents-help/#findComment-194966 Share on other sites More sharing options...
xrgt Posted February 28, 2007 Author Share Posted February 28, 2007 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']; ?> Link to comment https://forums.phpfreaks.com/topic/40092-solved-ob_get_contents-help/#findComment-195621 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.