我得到以下XML字符串作为来自BING的响应(注意:COMPOSITE结果),我尝试并尝试扫描条目(inline-> feed->条目)到for循环但是失败..代码我过去常常扫描结果
$xml = new SimpleXMLElement($rs);
$i=0;
if ( $xml->entry->link ) {
$feeds = $xml->entry->link->children('m', TRUE)->inline->entry;
foreach ( $feeds as $results) {
$i++;
echo $data=(string)$results->content;
$result = $data->children('m', TRUE)->properties->children('d', TRUE);
echo "ss".$clickurl = $result->Url;
$url = urldecode($clickurl);
$search[$i]['url'] = str_replace("&", "&", $url);// for the vali
$search[$i]['abstract'] = (string)$result->Description;
$search[$i]['title'] = (string)$result->Title;
$search[$i]['rank'] = $i;
} //foreach
}
return $search;
* 你能告诉我我在这里缺少什么吗?我不知道如何使用simpleXML对象访问<m:inline> <feed>
数据集,而微软支持团队的回答是PHP不是他们的语言所以他们无法帮助我,请我使用forums / stackoverflow。 *
<feed xmlns:base="https://api.datamarket.azure.com/Data.ashx/Bing/SearchWeb/Composite" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns="http://www.w3.org/2005/Atom">
<title type="text">keyword</title>
<subtitle type="text">Bing Search API</subtitle>
<id>https://api.datamarket.azure.com/Data.ashx/Bing/SearchWeb/Composite?Query='keyword'</id>
<rights type="text" />
<updated>2012-08-07T18:29:09Z</updated>
<entry>
<id>https://api.datamarket.azure.com/Data.ashx/Bing/SearchWeb/Composite?Query='keyword'&$skip=0&$top=1</id>
<title type="text">ExpandableSearchResult</title>
<updated>2012-08-07T18:29:09Z</updated>
<link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Web" type="application/atom+xml;type=feed" title="Web" href="ExpandableSearchResultSet(guid'3947df4e-b3b3-4be7-b25b-77852c8d312a')/Web">
<m:inline>
<feed xmlns:base="https://api.datamarket.azure.com/Data.ashx/Bing/SearchWeb/ExpandableSearchResultSet(guid'3947df4e-b3b3-4be7-b25b-77852c8d312a')/Web" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns="http://www.w3.org/2005/Atom">
<title type="text">Web</title>
<subtitle type="text">Bing Search API</subtitle>
<id>https://api.datamarket.azure.com/Data.ashx/Bing/SearchWeb/ExpandableSearchResultSet(guid'3947df4e-b3b3-4be7-b25b-77852c8d312a')/Web</id>
<rights type="text"></rights>
<updated>2012-08-07T18:29:09Z</updated>
<link rel="next" href="https://api.datamarket.azure.com/Data.ashx/Bing/SearchWeb/Web?Query='keyword'&$skip=3&$top=50" />
<entry>
<id>https://api.datamarket.azure.com/Data.ashx/Bing/SearchWeb/ExpandableSearchResultSet(guid'3947df4e-b3b3-4be7-b25b-77852c8d312a')/Web?$skip=1&$top=1</id>
<title type="text">WebResult</title>
<updated>2012-08-07T18:29:09Z</updated>
<content type="application/xml">
<m:properties>
<d:ID m:type="Edm.Guid">a6c62628-0012-42a2-b65c-513c82f523d1</d:ID>
<d:Title m:type="Edm.String">title ...</d:Title>
<d:Description m:type="Edm.String">title: ...</d:Description>
<d:DisplayUrl m:type="Edm.String">sss.newikis.com/dd.html</d:DisplayUrl>
<d:Url m:type="Edm.String">http://ss.newikis.com/ss.html</d:Url>
</m:properties>
</content>
</entry>
</feed>
</m:inline>
</link>
<content type="application/xml">
<m:properties>
<d:ID m:type="Edm.Guid">3947df4e-b3b3-4be7-b25b-77852c8d312a</d:ID>
<d:WebTotal m:type="Edm.Int64">3</d:WebTotal>
<d:WebOffset m:type="Edm.Int64">0</d:WebOffset>
<d:AlteredQuery m:type="Edm.String"></d:AlteredQuery>
<d:AlterationOverrideQuery m:type="Edm.String"></d:AlterationOverrideQuery>
</m:properties>
</content>
</entry>
</feed>
有谁可以帮助我使用上述代码使用PHP来解决此问题?
答案 0 :(得分:1)
你非常关闭,只缺少一些小的(但是至关重要的)积分。
您只需更改原始代码的三行即可。
获取<entry>
元素。
$feeds = $xml->entry->link->children('m', TRUE)->inline->entry;
变为
$feeds = $xml->entry->link->children('m', TRUE)->inline->children('', TRUE)->feed->entry;
从<d:*>
获取<m:properties>
个元素。
$result = $data->children('m', TRUE)->properties->children('d', TRUE);
变为
$result = $results->content->children('m', TRUE)->properties->children('d', TRUE);
删除以下行。
echo $data=(string)$results->content;
通过这些更改,您的脚本将提供您想要的结果数组。
以下示例是对原始内容的一个小修改,它以稍微整洁的方式从Feed中构建$search
数组。
*在我看来:读者可能不同意。很难,这是我的答案。
$xml = new SimpleXMLElement($rs);
$i = 0;
$search = array();
$entries = $xml->entry->link->children('m', TRUE)->inline->children('', TRUE)->feed->entry;
foreach ($entries as $entry) {
$i++;
$properties = $entry->content->children('m', TRUE)->properties->children('d', TRUE);
$url = (string) $properties->Url;
$url = str_replace("&", "&", urldecode($url));
$search[$i] = array(
'url' => $url,
'abstract' => (string) $properties->Description,
'title' => (string) $properties->Title,
'rank' => $i,
);
}
$entries
行使用children('', TRUE)->feed
来访问<feed>
内的<m:inline>
元素1}}。$search[$i]
;这只是个人偏好,因为我觉得它看起来更整洁。echo
和分配线。答案 1 :(得分:0)
不确定这是否是您问题的答案,但这些内容看起来很可疑:
echo $data=(string)$results->content;
$data
现在包含一个字符串,因为您将$results->content
的值转换为字符串,然后再将其分配给$data
。但是代码中的下一行是:
$result = $data->children('m', TRUE)->properties->children('d', TRUE);
在这里,$data
显然应该是一个对象而不是一个字符串。
如果您需要回音,请将该行分开:
$data = $results->content;
echo $data; // casting to string is done automatically