本文介绍了PHP-使用YQL选择特定条目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在使用来获取youtube用户的订阅者人数。

so I'm using YQL to get a youtube user's subscriber count.

要执行此操作,我正在使用如下网址进行查询:

to do this I'm making a query using a URL like this:

$query = 'https://query.yahooapis.com/v1/public/yql?q='.urlencode("SELECT * FROM xml WHERE url='http://gdata.youtube.com/feeds/api/users/{$id}'").'&format=json&callback=';

然后我解码响应并找到我想要的数组项:

then I decode the response and find the array entry I'm looking for like this:

  $response = json_decode($response, true);
  $subscriber_count = $response['query']['results']['entry']['statistics']['subscriberCount'];

工作正常,除了我不喜欢这样做的方式:)
是我可以直接从上面的 $ query URL获取 subscriberCount 值的方法吗?我不需要整个XML,只需要一个条目。

it works fine, except I don't like the way I'm doing this :)I mean, is there a way I can get the subscriberCount value directly from the $query URL above? I don't need the entire XML, just that one entry.

推荐答案

您可以做的是限制数据量YQL通过执行如下操作将YQL返回到您真正感兴趣的数据:

What you could do is to limit the amount of data that is returned by YQL to the data you are really interested in by doing sth like this:

SELECT statistics.subscriberCount FROM xml WHERE ...

仍然会在您感兴趣的数字周围保留一些结构化XML / JSON元素但至少要少一些(见下文)。不确定这是否是您想要的吗?

Still you would have some structural XML/JSON elements around the number that you are interested in but at least it is less (see below). Not sure if that is what you wanted?

<query xmlns:yahoo="http://www.yahooapis.com/v1/base.rng"
    yahoo:count="1" yahoo:created="2011-01-06T23:21:32Z" yahoo:lang="en-US">
    <results>
        <entry xmlns="http://www.w3.org/2005/Atom">
            <yt:statistics
                xmlns:yt="http://gdata.youtube.com/schemas/2007" subscriberCount="7"/>
        </entry>
    </results>
</query>

这篇关于PHP-使用YQL选择特定条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 00:42