Simple XML load file and extract information from XML result

What I have is an XML based API. I am loading a url, which is mostly xml, in simple_xml_load_file()

.

Pasting the url into the browser yields XML output.

You can try the search link here .

I download the same quoted link in simplexml_load_file

.

Where I am stuck is the extract part, I would like to extract the text State, Carrier, City, County and Phone from the XML result.

This is my code to extract State

$state = $simpleXML->searchService->searchResult->dataset->phoneInfo->rateCenter['state'];

      

For some reason, I don't know why. Execution echo

$simpleXML

gives no output.

So I can't either load the XML url or retrieve it, which became apparent with the XML load.

So, I am pasting all the code so you can see

<?php

$phoneNumber = 5128435436;
$context  = stream_context_create(array('http' => array('header' => 'Accept: application/xml')));

$simpleXML = 'http://api.peoplesearchxml.com/SearchServicePublic.asmx/SearchXML?sSearchRequest=<search><searchType>PartnerPeopleSearchByPhoneACW</searchType><    searchCriteria><phone>'.$phoneNumber.'</phone></searchCriteria><identification><websiteKey>7</websiteKey><partnerID>XYZCalledYou.com</partnerID><partnerPassword>eshwarrocks</    partnerPassword><ipAddress>127.0.0.1</ipAddress></identification><formatting><maxResults>5</maxResults></formatting></search>';    

$xml = file_get_contents($simpleXML, false, $context);
$xml = simplexml_load_string($xml);

$state = $simpleXML->searchService->searchResult->dataset->phoneInfo->rateCenter['state'];
$carrier = $simpleXML->searchResult->dataset->phoneSearch['company'];
$city = $simpleXML->searchResult->dataset->phoneSearch['city'];
$county = $simpleXML->searchResult->dataset->phoneSearch['county'];
$phoneType = $simpleXML->searchResult->dataset->phoneSearch['lineType'];

echo $simpleXML. '<br><br><br><br><br>';
echo 'Phone Number: '.$phoneNumber.'<br />';
echo 'State: '.$state.'<br />';
echo 'Carrier: '.$carrier.'<br />';
echo 'City: '.$city.'<br />';
echo 'County: '.$county.'<br />';
echo 'Phone Type: '.$phoneType.'<br />';

?>

      

Thanks for taking the time looking at this, greatly appreciated.

+3


source to share


1 answer


You are pointing to the wrong objects:

$phoneNumber = 5128435436;
$context  = stream_context_create(array('http' => array('header' => 'Accept: application/xml')));

$simpleXML = 'http://api.peoplesearchxml.com/SearchServicePublic.asmx/SearchXML?sSearchRequest=<search><searchType>PartnerPeopleSearchByPhoneACW</searchType><searchCriteria><phone>'.$phoneNumber.'</phone></searchCriteria><identification><websiteKey>7</websiteKey><partnerID>XYZCalledYou.com</partnerID><partnerPassword>eshwarrocks</partnerPassword><ipAddress>127.0.0.1</ipAddress></identification><formatting><maxResults>5</maxResults></formatting></search>';

$xml = file_get_contents($simpleXML, false, $context);
$xml = simplexml_load_string($xml);

$dataset = $xml->searchResult->dataset[0];

$state = (string) $dataset->phoneInfo->rateCenter->attributes()->state;
$carrier = (string) $dataset->phoneInfo->operatingCompany->attributes()->name;
$city = (string) $dataset->phoneInfo->operatingCompany->attributes()->city;
$country = (string) $dataset->phoneInfo->rateCenter->attributes()->country;
$phoneType = (string) $dataset->phoneInfo->attributes()->lineType;

echo "
    <strong>State:</strong>         $state <br/>
    <strong>Carrier:</strong>       $carrier <br/>
    <strong>City:</strong>          $city <br/>
    <strong>Country:</strong>       $country <br/>
    <strong>Phone Type:</strong>    $phoneType <br/>
";

      



Sample output

+1


source







All Articles