gpt4 book ai didi

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

转载 作者:数据小太阳 更新时间:2023-10-29 02:40:25 25 4
gpt4 key购买 nike

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

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

您可以尝试查找链接 here ,

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

我卡在提取部分的地方,我想从 XML 结果中提取州、运营商、城市、县和电话类型。

这是我提取State

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

不知为何失败了。执行 $simpleXMLecho 不会给出任何输出。

所以我在加载 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

关于php - 简单的 XML 加载文件并从 XML 结果中提取信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26791675/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com