简单的XML加载文件,并从XML结果中提取信息


Simple XML load file and extracting information from the XML result

这里有一个基于XML的API查找。我正在将大部分是xml的URL加载到simple_xml_load_file()中。

将URL粘贴到浏览器中,会得到XML输出。

你可以试试这里的查找链接,

我正在将完全相同的带有引号的链接加载到simplexml_load_file中。

我遇到的问题是提取部分,我想从XML结果中提取State、Carrier、City、County和Phone类型。

这是我提取State 的代码

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

由于某种原因它失败了,我不知道为什么。执行$simpleXMLecho不会给出任何输出。

因此,我在加载XMLURL或提取方面都失败了,现在加载XML就清楚了。

所以我粘贴了整个代码让你看一看,

<?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 />';
?>

谢谢你花时间研究这件事,非常感谢。

您指出了错误的对象:

$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/>
";

样本输出