我这里有一个基于XML的API查找。我将大部分xml的URL加载到simple_xml_load_file()
。
将URL粘贴到浏览器中,提供XML输出。
您可以尝试查找链接here,
我正在使用引号将完全相同的链接加载到simplexml_load_file
。
我遇到的是提取部分,我想从XML结果中提取State,Carrier,City,County和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 />';
?>
感谢您花时间研究这一点,非常感谢。
答案 0 :(得分:1)
你指出了错误的对象:
$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/>
";