问题
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