2014-11-06 49 views
3

我在这里是基于XML的API查找。我将大部分的xml网址加载到simple_xml_load_file()简单的XML加载文件并从XML结果中提取信息

将URL粘贴到浏览器中,提供XML输出。

您可以尝试查找链接here

我加载带有引号完全相同的链接进入simplexml_load_file

我卡在哪里是提取部分,我想从XML结果中提取州,承运人,市,县和电话类型。

这是我的代码提取State

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

它由于某种原因失败,我不知道为什么。做的$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 />'; 

?> 

感谢您抽出时间看这个,大加赞赏。

回答

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

Sample Output

+0

感谢@Ghost现在的工作...... +1和正确的答案给出:)大加赞赏。 – Eshwar 2014-11-07 01:44:51

+1

@Eshwar肯定的人我很高兴这有帮助 – Ghost 2014-11-07 01:45:03