cancel
Showing results for 
Search instead for 
Did you mean: 

How do I parse the result message from a CIM call in PHP?

Hi all,

 

I'm new to the Auth.Net API and am having difficulties trying to understand how to parse out the result values from errors in my test calls to the CIM functions.

 

I've pored through the documentation, but there isn't really anywhere I can see that says "call this function" or use these variables to find the results from a failed call to a function.  Where can I find some instructions on handling and parsing errors?  This is as far as I have gotten so far but none of my echo statements seem to show any error information.

	$response = $request->createCustomerProfile($customerProfile);
	if ($response->isOk()) {
		$G['profile_id'] = $response->getCustomerProfileId();
	} else {
		$G['err']['something'] = "some kind of error occurred";
		echo "Error!";
		echo "<p>Response=".$response->getResultCode();
		echo "<p>Response=".$response->validationDirectResponseList;
//		echo $response; // Throws an error since it is an object not a string
		exit;
	}
4classjuggler
Member
4 REPLIES 4

I was able to finally figure this out, despite the very poor documentation for the APIs and no responses to my post for help.  I figured this out by using the PHP function get_object_vars to show me just what is inside the response code object (since I couldn't find any reference to how to address values in the documentation).  I am posting this in case it helps any other new developers using PHP.

echo get_object_vars($response);

This showed me the dump of the values in the $response object upon a call to create a new CIM profile, which looked like:

Array
(
    [xml] => SimpleXMLElement Object
        (
            [messages] => SimpleXMLElement Object
                (
                    [resultCode] => Error
                    [message] => SimpleXMLElement Object
                        (
                            [code] => E00003
                            [text] => The element 'billTo' in namespace 'AnetApi/xml/v1/schema/AnetApiSchema.xsd' has invalid child element 'astName' in namespace 'AnetApi/xml/v1/schema/AnetApiSchema.xsd'. List of possible elements expected: 'phoneNumber, faxNumber, email' in namespace 'AnetApi/xml/v1/schema/AnetApiSchema.xsd'.
                        )

                )

        )

    [response] => ErrorE00003The element 'billTo' in namespace 'AnetApi/xml/v1/schema/AnetApiSchema.xsd' has invalid child element 'astName' in namespace 'AnetApi/xml/v1/schema/AnetApiSchema.xsd'. List of possible elements expected: 'phoneNumber, faxNumber, email' in namespace 'AnetApi/xml/v1/schema/AnetApiSchema.xsd'.
    [xpath_xml] => SimpleXMLElement Object
        (
            [messages] => SimpleXMLElement Object
                (
                    [resultCode] => Error
                    [message] => SimpleXMLElement Object
                        (
                            [code] => E00003
                            [text] => The element 'billTo' in namespace 'AnetApi/xml/v1/schema/AnetApiSchema.xsd' has invalid child element 'astName' in namespace 'AnetApi/xml/v1/schema/AnetApiSchema.xsd'. List of possible elements expected: 'phoneNumber, faxNumber, email' in namespace 'AnetApi/xml/v1/schema/AnetApiSchema.xsd'.
                        )

                )

        )

)

From that dump above I was then able to determine how to get to the values in the response I really needed:

	$response = $request->createCustomerProfile($customerProfile);
	if ($response->isOk()) {
		$G['profile_id'] = $response->getCustomerProfileId();
	} else {
		echo "<pre>";
		print_r(get_object_vars($response));
		echo "</pre>";
		$G['err']['something'] = "some kind of error occurred";
		echo "Error!";
		echo "<p>Response0=".$response->xml->messages->resultCode;
		echo "<p>Response1=".$response->xml->messages->message;
		echo "<p>Response2=".$response->response;
		echo "<p>Response3=".$response->xml->messages->message->code;
		echo "<p>Response4=".$response->xml->messages->message->text;
//		echo $response; // Throws an error since it is an object not a string
		exit;
	}

The key is knowing the structure to the result object hierarchy.  Now I can see the $response -> xml -> messages -> code (error code) and text (for the description of the error).

 

I hope this helps others.

 

 

4classjuggler
Member

Hello @4classjuggler

 

Thank you for posting your solution to help other developers.  In answer to your question, we're currently working on adding sample code for each of our API methods and adding it to GitHub so you can easily see how it should work.  This should be complete in a few weeks.

 

Richard

Has the sample code been posted somewhere?

Hello @Jim88763563

 

Our sample code is available on Github: http://github.com/AuthorizeNet

 

Richard