2014-11-06 48 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