Having difficulty with processing SOAP response in PHP using simplexml

I'm using cURL to send a SOAP request and receive the following response:

<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
<env:Header xmlns:wsa="http://www.w3.org/2005/08/addressing">
<wsa:To>http://www.w3.org/2005/08/addressing/anonymous</wsa:To>
  <wsa:Action>http://www.csapi.org/schema/parlayx/common/v3_1/TerminalLocationPort/getLocationForGroupResponse</wsa:Action>
</env:Header>
<env:Body>
  <ns2:getLocationForGroupResponse xmlns:ns2="http://www.csapi.org/schema/parlayx/terminal_location/v3_1/local">
     <ns2:result>
        <address>234983</address>
        <reportStatus>Retrieved</reportStatus>
        <currentLocation>
           <latitude>12.5665</latitude>
           <longitude>43.7708</longitude>
           <timestamp>2012-01-03T17:06:16.805+01:30</timestamp>
        </currentLocation>
     </ns2:result>
     <ns2:result>
        <address>423903</address>
        <reportStatus>Retrieved</reportStatus>
        <currentLocation>
           <latitude>12.2165</latitude>
           <longitude>43.6518</longitude>
           <timestamp>2012-01-03T17:06:16.824+01:30</timestamp>
        </currentLocation>
     </ns2:result>
  </ns2:getLocationForGroupResponse>
</env:Body>
</env:Envelope>

My attempt at decoding this with SimpleXML is resulting in an error. This link seems relevant, but I can't figure out how to apply it in my case.

$err = curl_error($soap_do);
$result = curl_exec($soap_do);
$xml = simplexml_load_string($result);
$ns = $xml->getNamespaces(true);
$soap = $xml->children($ns['env']);
$getaddressresponse = $soap->body->children($ns['ns2']);
foreach ($getaddressresponse->children() as $item) {
    echo (string) $item->address . '<br />';
}

I'm encountering trouble with this code. Any suggestions?

UPDATE: If the server returns the following error message, how can I handle it?

<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
   <env:Header xmlns:wsa="http://www.w3.org/2005/08/addressing">
      <wsa:To>http://www.w3.org/2005/08/addressing/anonymous</wsa:To>
      <wsa:Action>http://www.w3.org/2005/08/addressing/fault</wsa:Action>
   </env:Header>
   <env:Body>
      <env:Fault>
         <faultcode>env:Server</faultcode>
         <faultstring>Service Exception</faultstring>
         <detail>
            <ns1:ServiceException xmlns:ns1="http://www.csapi.org/schema/parlayx/common/v3_1" xmlns:ns2="http://www.csapi.org/schema/parlayx/terminal_location/v3_1/local">
               <messageId>SVC004</messageId>
               <text>Trip not Found for this MSISDN</text>
            </ns1:ServiceException>
         </detail>
      </env:Fault>
   </env:Body>
</env:Envelope>

Answer №1

During my testing, I discovered that variable and property names are case sensitive. In addition to this, I found some other important details. The code snippet below demonstrates a working example:

$soap = $xml->children($ns['env']);
$getaddressresponse = $soap->Body->children($ns['ns2']);
foreach ($getaddressresponse->getLocationForGroupResponse->children($ns['ns2']) as $item)
{
    $item = $item->children();
    echo $item->address . '<br />';
}

In response to the recent update:

$fault = $soap->Body->children($ns['env']);
if (isset($fault->Fault))
{
    // Handle error
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Navigating the complexities of the MySql relational database system

I have been attempting to utilize PHP to showcase all the outcomes from the MySql NOTES table that correspond with specific NAME in QUOTES table. The goal is to exhibit all the COMMENTS listed under each particular NAME. QUOTES table columns: ID / NAME / ...

Assigning background colors based on the data stored in a specific field of a MySQL database

I have successfully implemented a Ticketing System using PHP and MySQL database. The view tickets page displays each ticket from the database along with its priority level, which can be Low, Normal or High. Currently, the priority value pulled from the d ...

PHP, jQuery, and MySQL combine to create a powerful autocomplete feature for your

I've implemented the source code from into my project, but I'm facing an issue where I can't retrieve any results when typing in the autocomplete textbox. Could someone point out where I might be making a mistake? This is the code I am us ...

Php Error 404 occurs upon attempting to redirect from the main landing page

I have been encountering an issue while trying to add links to different pages of my website. Whenever I click on the link, it displays a "404 Page Not Found" error message. Objective: My main goal is to create links from my home page, index.php, to other ...

Setting up PhpStorm, Xdebug, and Laravel Configuration

My current development setup consists of PhpStorm 2018.2.1 running on Windows 10. Within this environment, I have successfully configured Homestead Vagrant box for Laravel. The Laravel application is functioning as expected. Recently, I decided to include ...

PHP code to check the width and height of an image using the `getimages

Currently, I am working on validating the size of an image pixel in my project. After using the getimagesize function and storing it to a variable, I can then access the width and height by referencing the 3rd index of the array. My main challenge is fig ...

Is it possible to use JavaScript to clear a field that was generated by Yii CRUD?

Issue with Yii's GRUD field generated and Firefox HTML: <?= $form->field($model, 'productId')->textInput(['maxlength' => true]) ?> Comparison of PHP generated code to Firefox HTML: <input id="taolistforcreate-p ...

PHP Dividing the outcome

Looking for PHP assistance, specifically with modifying hexadecimal values. I currently have a value of 612B in hex decimal format and need to switch the positions of 61 & 2B to become 2B61. $skillsearch1 = odbc_exec($conn1,"select cast(cast(rever ...

Combining multiple values into a single column across various rows using PHP

I am facing an issue with handling three variables: $Title, $Pubdate, and $Link. Each of these variables contains multiple values as follows: $Title = aa, bb, cc, dd, ee, ff $Pubdate = aa, bb, cc, dd, ee, ff $Link = aa, bb, cc, dd, ee, ff In order to dis ...

Trouble with the getimagesize() command! An issue with the getimagesize() command loop!

While using the getimagesize() command in both a For loop and a Foreach loop, I noticed that they are giving me the same incorrect result. Despite my efforts, I haven't been able to fix this issue. Can someone please explain why this is happening and ...

Methods for passing JavaScript variables to PHP

I have encountered this problem on Stack Overflow before, but I couldn't find a solution that worked for me. I am using Codeigniter and have a form where users can rate a product. What I need to achieve is to insert the user's rating into the dat ...

Using PHP to send a JSON request via cURL

I am attempting to make a cURL request in PHP. The request I am trying to send is as follows: $ curl -H 'Content-Type: application/json' -d '{"username":"a", "password":"b","msisdn":"447000000001","webhook":"http://example.com/"}' http ...

Obtain the longitude and latitude coordinates by calculating the distance and angle

I need to generate geographical combinations within a grid that covers my city. The grid measures 500m x 500m. I want to input starting longitude/latitude coordinates, distance (500m), degree (90 or 270), and receive the next longitude/latitude combination ...

What are the best ways to enhance performance for ajax requests using Jquery?

Currently, I am in the process of developing a mobile application that utilizes jquery mobile, jquery, and a PHP backend. My issue arises when dealing with certain pages as there are numerous ajax requests being sent and received simultaneously, resulting ...

Executing a Particular Function in PHP with Arguments

As a newcomer to PHP and JavaScript, I'm attempting to invoke a specific function in a PHP file from JavaScript. Here is my code: <script> function load($dID) { $.ajax({ url: "myPHP.php", ...

Display various post types in a WordPress loop

I am designing a website on Wordpress where I want to showcase various "tiles" containing content from different sections of the site on the homepage. These tiles are custom post types such as "services", "consultants", and "blog posts". While I understan ...

Sending data from PHP to jQuery.ajax using JSON to receive multiple values

Hi there! I've been attempting to retrieve multiple results from a database using PHP and Ajax, but unfortunately, the JSON method doesn't seem to be working for me. $.ajax({ type: "POST", data: "qid=162", url: "activity_ajax.php", dataT ...

Can someone please guide me on determining the orientation of a PDF file through PHP or a Linux script?

I am looking for a way to programmatically analyze an uploaded PDF file and identify whether the pages are in portrait or landscape orientation. Is there a method using PHP or Linux commands that can help me accomplish this task? ...

Retrieve records where the ID is not found in another table limited by a specific date range

Is it possible to use the 'where not in' and 'between date intervals' clauses separately instead of combining them into one query? $this->db->select('*'); $this->db->from('admission'); $this->db->w ...

What is the best way to implement a left join using Laravel's join function?

I am struggling with implementing leftjoin and join in Laravel. I need help rewriting the following sample code using Laravel methods. LEFT JOIN (advertsolution_f SF JOIN function_d FD ON SF.fd_id = FD.fd_id JOIN function_m FM ON FD. ...