How to correctly parse XML, from API response, using PHP API

Talk with others about developing applications for Switchvox

Moderators: bmdhacks, dpodolsky, tristand, jwitt, joshuas

How to correctly parse XML, from API response, using PHP API

Postby undo111 » Fri Jun 08, 2012 12:18 am

Hello,

I am using the PHP Client Library for Switchvox Extend. I correctly send the requests and Switchvox correctly executes the request.

But the response is not parsed as an XML document.

Below you can see the request issued:

Code: Select all
<?php

require_once("SwitchvoxRequest.php");

$request = new SwitchvoxRequest("10.10.10.10", "admin", "admin");
$requestParams = array( 'dial_first' => array ('801'), 'dial_second' => array ('00XXXXXXXXXXXX'), 'dial_as_account_id' => array ('1101') );
$response = $request->send("switchvox.call", $requestParams);

print_r($response);

?>


And the response it is parsed as below:

Code: Select all
ArraySwitchvoxResponse Object ( [apiStatus:SwitchvoxResponse:private] => success [apiErrors:SwitchvoxResponse:private] => Array ( ) [apiResult:SwitchvoxResponse:private] => Array ( [call_info] => Array ( [dialed_first] => 801 [dialed_second] => 00XXXXXXXXXXXX [variables] => ) ) [rawXML:SwitchvoxResponse:private] => )


While it should be parsed like this:

Code: Select all
<response method="switchvox.call">
   <result>
      <call_info dialed_first="801" dialed_second="00XXXXXXXXXXXX" >
      </call_info>
   </result>
</response>


Is there any way to correctly parse the response using the PHP API library?

Thank you for your time.

Best regards,
Andi
undo111
Newsterisk
 
Posts: 17
Joined: Tue Sep 20, 2011 10:31 am

Re: How to correctly parse XML, from API response, using PHP API

Postby david55 » Fri Jun 08, 2012 3:57 am

Your "correct" result is not parsed at all! I'm not familiar with the package or this particular API, but your "incorrect" response looks like a plausible valid parse of the response.
david55
Moves Like Spencer
 
Posts: 7718
Joined: Fri Sep 26, 2008 5:03 am

Re: How to correctly parse XML, from API response, using PHP API

Postby undo111 » Fri Jun 08, 2012 4:37 am

hi david55,

thank you for your reply

I was looking for a way to parse XML formatted response from PHP Client Library, without writing any extra code!

so let say, that the PHP Client Library, is not fully functional if we do not get an XML response back, to process!
undo111
Newsterisk
 
Posts: 17
Joined: Tue Sep 20, 2011 10:31 am

Re: How to correctly parse XML, from API response, using PHP API

Postby dpodolsky » Fri Jun 08, 2012 9:30 am

There are plenty of PHP libraries that parse XML. A quick google search came up with a bunch of options. After you get the response from the PHP library then parse the XML and you are all set.

Hope this helps.
dpodolsky
Oldsterisk
 
Posts: 325
Joined: Thu Apr 23, 2009 2:35 pm

Re: How to correctly parse XML, from API response, using PHP API

Postby david55 » Fri Jun 08, 2012 1:35 pm

undo111 wrote:so let say, that the PHP Client Library, is not fully functional if we do not get an XML response back, to process!


Your problem is that is is over-functional for your design constraints. It is getting back the XML, and then parsing it. You want it to stop before parsing it. You will either find there is a lower layer that does not do the parsing, or that getting the unparsed XML is so trivial that it simply issues a read.
david55
Moves Like Spencer
 
Posts: 7718
Joined: Fri Sep 26, 2008 5:03 am

Re: How to correctly parse XML, from API response, using PHP API

Postby KKiernan » Tue Jun 12, 2012 7:04 am

This is definitely a valid response from the Switchvox. You can see it is returning an "ArraySwitchvoxResponse Object" - so access it's properties as such. You should be able to use something like:

Code: Select all
echo $response->apiResult:SwitchvoxResponse:private->call_info->dialed_first;
KKiernan
Newsterisk
 
Posts: 7
Joined: Fri May 04, 2012 7:39 am

Re: How to correctly parse XML, from API response, using PHP API

Postby undo111 » Thu Jun 14, 2012 9:33 am

Hello,

thank you for your reply!

this does not work:

Code: Select all
echo $response->apiResult:SwitchvoxResponse:private->call_info->dialed_first;


Best regards,
Andi
undo111
Newsterisk
 
Posts: 17
Joined: Tue Sep 20, 2011 10:31 am

Re: How to correctly parse XML, from API response, using PHP API

Postby david55 » Thu Jun 14, 2012 10:02 am

1) That seems to be the wrong syntax for accessing array elements in PHP. If you are going to use a PHP class, you really need to be familiar with basic PHP syntax.

2) It looks to me as though the response is in the form of an encapsulated object, which means you will need to use the appropriate methods to get information from it. Again, if you are going to use an object oriented API, you need to understand things like encapsulation.

I presume the printout of the object is the result of using a debugging command which formats private, as well as public, data.

I don't use Switchvox and I cannot find the included php file, to see what methods it defines.
david55
Moves Like Spencer
 
Posts: 7718
Joined: Fri Sep 26, 2008 5:03 am

Re: How to correctly parse XML, from API response, using PHP API

Postby undo111 » Thu Jun 14, 2012 12:13 pm

Hi david55,

Please find the PHP API at this URL:

http://developers.digium.com/switchvox/?pageView=phpLibrary

You can download this file, and extract the PHP files needed (everything needed is on this package):

http://developers.digium.com/switchvox/client_libraries/SwitchvoxPHPLibrary.tar.gz

If you find time, please take a look at the PHP class that constructs "$response" variable, and try to find a solution to extract the data from that object variable!

Thank you for your time.

Best regards,
Andi
undo111
Newsterisk
 
Posts: 17
Joined: Tue Sep 20, 2011 10:31 am

Re: How to correctly parse XML, from API response, using PHP API

Postby david55 » Thu Jun 14, 2012 4:14 pm

You will need to call the getResult method of the response object. That will return an array. You want the element of that array with the index 'call_info'. This is itself an array, so, to get the dialed_first value, you need to further index it with 'dialed_first'.

I haven't written PHP for some time, and have never written object oriented PHP, so I won't attempt to write the actual code.

To do literally what you originally asked for, you would call the getRawXMLResponse method, but that, I am sure, is not how it is normally expected to be used, given that it has gone to the trouble of parsing the XML for you.
david55
Moves Like Spencer
 
Posts: 7718
Joined: Fri Sep 26, 2008 5:03 am

Re: How to correctly parse XML, from API response, using PHP API

Postby undo111 » Fri Jun 15, 2012 6:35 am

Hello everybody,

The solution is as follows:

The PHP file to be used to call the Switchvox methods, should look like this:

Code: Select all
<?php

require_once("SwitchvoxRequest.php");

$request = new SwitchvoxRequest("10.10.10.10", "admin", "admin");
$requestParams = array( 'dial_first' => array ('801'), 'dial_second' => array ('00XXXXXXXXXXXX'), 'dial_as_account_id' => array ('1101') );
$response = $request->send("switchvox.call", $requestParams);

$in_response_XML = $response->getRawXMLResponse();

header('Content-type: text/xml');
echo $in_response_XML;

?>


The last three lines of code, return in your browser the XML response from server!

Code: Select all
$in_response_XML = $response->getRawXMLResponse();

header('Content-type: text/xml');
echo $in_response_XML;


Best regards,
Andi
undo111
Newsterisk
 
Posts: 17
Joined: Tue Sep 20, 2011 10:31 am

Re: How to correctly parse XML, from API response, using PHP API

Postby david55 » Fri Jun 15, 2012 6:37 am

But you shouldn't be using the API if you are going to work directly with the XML.
david55
Moves Like Spencer
 
Posts: 7718
Joined: Fri Sep 26, 2008 5:03 am

Re: How to correctly parse XML, from API response, using PHP API

Postby undo111 » Fri Jun 15, 2012 6:50 am

Hi david55,

I will use the PHP API as a "middle man" to Switchvox!

Thank you for your help.

Best regards,
Andi
undo111
Newsterisk
 
Posts: 17
Joined: Tue Sep 20, 2011 10:31 am


Return to Switchvox Developers

Who is online

Users browsing this forum: Google Feedfetcher and 1 guest