我这里有一个基于xml的api查找。我正在将大部分是xml的url加载到simple_xml_load_file()
中。
将url粘贴到浏览器中,给出xml输出。
您可以尝试查找链接here,
我正在将带有引号的完全相同的链接加载到simplexml_load_file
。
我遇到的问题是提取部分,我想从xml结果中提取state、carrier、city、country和phone类型。
这是我提取State
的代码。
$state = $simpleXML->searchService->searchResult->dataset->phoneInfo->rateCenter['state'];
我不知道为什么会失败。执行
echo
的$simpleXML
操作不会产生任何输出。所以我要么加载xml url要么解压失败,这在加载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/>
";
Sample Output