Need PHP help with parsing an XML cURL response

I'm fairly novice with PHP, but I can usually figure things out. But I'm stuck on working around this legacy code that we use on our sales form.
 I have to shoehorn in another Card processing merchant that does things a little differently with their API.

This is processing the response from a cURL API where I receive a SOAP XML response. I'm having some issues with the actual response as well, so I have it sampled in what appears to be a proper xml format, then attempting to parse out the message objects.

The process:
1. I have the response XML converting into a "SimpleXMLElement" with the simplexml_load_string
2. Then the XML is "converted to an array", I think, with the $array = (array)$xml;
3. Then, the code should be able to look into the array values and process a Success or Failed based on the Status Value
4. Then the $data is json encoded and is passed to the JavaScript file for further processing of the notification and filling in the Auth code field of the sales form.

The Problem(s):
1. You'll see in the PHP code that I have 2 testing sections to process the response. The first uses the $array and the second skips the array and just uses the $xml. One is commented out for testing the other, of course.
2. When using the $xml section to get the values, I get real close to what I need until it get json encoded, then the output just says Object Object in the JS Alert. It appears that the code is finding the proper value of the key, but it's not handled the same way for the json encoding in order to get a good alert.
3. When using the array process, I don't get anything but a full failure to extract the values, so it rolls to the end else statement "$data = array( 'message' => $xml->transaction->description, 'html' => 'Failed, Not seeing Success or TransactionID', 'date' => $curdate->format('m/d/Y h:i:s a') );"

What I really need help with is:
What is the proper way to use the array process to grab that Status value? is it $array->status == "Success" or do I need to go deeper?
I'm hoping that then I will get the proper json encoding to pass on.

Thank you for your time!

Here is what the SimpleXMLElement produces:
SimpleXMLElement Object
(
    [transaction] => SimpleXMLElement Object
        (
            [status] => Success
            [reasoncode] => N/A
            [transactionid] => J7DXXXX8U0
            [description] => The credit card has been accepted.
        )

)

Open in new window


Here is what the array produces:
Array
(
    [transaction] => SimpleXMLElement Object
        (
            [status] => Success
            [reasoncode] => N/A
            [transactionid] => J7DXXXX8U0
            [description] => The credit card has been accepted.
        )

)

Open in new window


Code sample PHP of SOAP XML Response handler:
// Success response sample
    /* $response = "<?xml version='1.0' encoding='utf-8'?>
                    <string xmlns='http://wsclientapi.everyware.com/'>
                        <transaction>
                            <status>Success</status>
                            <reasoncode>N/A</reasoncode>
                            <transactionid>J7DXXXX8U0</transactionid>
                            <description>The credit card has been accepted.</description>
                       </transaction>
                    </string>"; */

//Failed response sample
$response = "<?xml version='1.0' encoding='utf-8'?>
                    <string xmlns='http://wsclientapi.everyware.com/'>
                        <transaction>
                            <status>Failed</status>
                            <reasoncode>409</reasoncode>
                            <transactionid>N/A</transactionid>
                            <description>The transaction has been declined.</description>
                        </transaction>
                    </string>";

    $xml = simplexml_load_string($response); 

    //Used to print out the xml for testing
    ?><pre><?php print_r($xml); ?></pre><?php
 

                $array = (array)$xml;
                //Used to print out the array for testing
                ?><pre><?php print_r($array); ?></pre><?php


                  if ($xml === false) {
                    $data = array('message' => $response, 'html' => $response);
       
                } else {
                    // $data = array('message' => $array[0], 'html' => $array[0]);
                    if ($array->status == "Success") {
                        // Transaction approved! Do your logic here.

                        $curdate = new DateTime('NOW', new DateTimeZone('America/Los_Angeles'));
                        if( isset($array->transactionid) ){
                            $data = array( 'message' => 'Success', 'html' => 'Success', 'transid' => $array->transactionid, 'auth' => $array->transactionid, 'date' => $curdate->format('m/d/Y h:i:s a') );
                        } else{
                            $data = array( 'message' => 'Success', 'html' => 'Success', 'transid' => $array->transactionid, 'date' => $curdate->format('m/d/Y h:i:s a') );
                        }

                    } else {
                        $curdate = new DateTime('NOW', new DateTimeZone('America/Los_Angeles'));
                        if( isset($response->transactionid) ){
                            $data = array( 'message' => $array->description, 'html' => 'Failed', 'date' => $curdate->format('m/d/Y h:i:s a'), 'transid' => $array->transactionid );
                        } else{
                            $data = array( 'message' => $array->description, 'html' => 'Failed, Not seeing Success or TransactionID', 'date' => $curdate->format('m/d/Y h:i:s a') );
                        }
                    }
                }

                /*    if ($xml === false) {
                        $data = array('message' => $response, 'html' => $response);
                        file_put_contents("whereDataFalse.txt", $data);
                    } else {
                        // $data = array('message' => $array[0], 'html' => $array[0]);
                        if ($xml->transaction->status == "Success") {
                            // Transaction approved! Do your logic here.

                            $curdate = new DateTime('NOW', new DateTimeZone('America/Los_Angeles'));
                            if( isset($xml->transaction->transactionid) ){
                                $data = array( 'message' => 'Success', 'html' => 'Success', 'transid' => $xml->transaction->transactionid, 'auth' => $xml->transaction->transactionid, 'date' => $curdate->format('m/d/Y h:i:s a') );
                            } else{
                                $data = array( 'message' => 'Success', 'html' => 'Success', 'transid' => $xml->transaction->transactionid, 'date' => $curdate->format('m/d/Y h:i:s a') );
                            }

                        } else {
                            $curdate = new DateTime('NOW', new DateTimeZone('America/Los_Angeles'));
                            if( isset($xml->transaction->transactionid) ){
                                $data = array( 'message' => $xml->transaction->description, 'html' => 'Failed', 'date' => $curdate->format('m/d/Y h:i:s a'), 'transid' => $xml->transaction->transactionid );
                            } else{
                                $data = array( 'message' => $xml->transaction->description, 'html' => 'Failed, Not seeing Success or TransactionID', 'date' => $curdate->format('m/d/Y h:i:s a') );
                            }
                        }
                    } */
                echo json_encode($data);
   
                die();
            }

Open in new window


JS Code sample of alert handler:
var response = jQuery.parseJSON(data);
          // var response = data;
          console.log(response);
        if( response.message == 'Success' && response.html != 'Failed' && response.html != '' ) {
          
          jQuery('#lead_item_full_trans').hide();
          
          if( response.transid ){
            jQuery('#sales_form input#trans_id').val(response.transid);
          }
          if( response.date ){
            jQuery('#sales_form input#trans_date').val(response.date);
          }
          if( response.auth ){
            jQuery('#sales_form input[name=v_auth]').val(response.auth);
          }

          jQuery('#sales_form input#trans_type').val('Charge');
          alert('Card has been successfully charged.');
        } else {
          if( response.transid ){
            jQuery('#sales_form input#trans_id').val(response.transid);
          }
          if( response.message ){
            jQuery('#sales_form input#trans_note').val(response.message);
          }
          if( response.date ){
            jQuery('#sales_form input#trans_date').val(response.date);
          }
          jQuery('#sales_form input#trans_type').val('Error');
          alert(response.message);
        }
      });

Open in new window

Chris InmanOne-man IT Show!Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Chris StanyonWebDevCommented:
Hey Chris,

Couple of things to points out. When you convert to an array:

$array = (array)$xml;

You will end up with an associative array containing a key of 'transaction':

$array = array('transaction' => SimpleXmlElement);

The value of that key will be a SimpleXmlElement, so you would need to access the values like so:

$array['transaction']->status;

To me this feels a little odd to me as you're mixing arrays and objects, so I would just stick to the $xml;

$xml->transaction->status

The reason, you're getting the object / object issue is because each 'node' of the transaction element (status / description etc) is itself an SimpleXmlElement , so when you do this:

$data = array('message' => $xml->transaction->description)

you're actually setting message to an instance of a SimpleXmlElement, and not the value of it, so your JS can't understand it. What you need to do is cast that element to a string. Same goes for your Transaction ID.

$data = array('message' => (string)$xml->transaction->description, 'transid' => (string)$xml->transaction->transactionid);

Now the data will be strings and not objects and you'll be able to json_encode it in a way that your JS will understand.

FYI - when you're debugging like this, it's often easier to use var_dump rather than print_r.

var_dump($data);

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Chris InmanOne-man IT Show!Author Commented:
Thank you for the awesome response. I will attempt the changes over the weekend and let you know how it turns out.
Thank you, thank you!
Chris InmanOne-man IT Show!Author Commented:
Chris,
Phenomenal response as I actually was able to understand it as you explained and walked through it, even at my novice level.
Getting rid of the array conversion altogether was great and converting the xml data to string worked like a charm for the json.

With some other minor tweaks, I have this bad boy running pretty good now.
I still have a minor issue with the vendors end, but that is on them to fix.

Many Thanks to you Sir!

~Chris
Chris StanyonWebDevCommented:
No worries Chris,

Pleased you got it working, but more importantly, I'm pleased you know why you got it working.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
arrays

From novice to tech pro — start learning today.