PHP - selecting a particular entry with YQL

情到浓时终转凉″ 提交于 2020-01-16 17:35:14

问题


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'];

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.


回答1:


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 ...

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>


来源:https://stackoverflow.com/questions/4417341/php-selecting-a-particular-entry-with-yql

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!