2010-09-01 26 views

Trả lời

10

Bạn có thể truy vấn các API của YouTube như thế này:

<?php 

$curlhandle = curl_init(); 
curl_setopt($curlhandle, CURLOPT_URL, "http://gdata.youtube.com/feeds/api/videos?v=2&alt=jsonc&q=computers&max-results=10&orderby=viewCount"); 
curl_setopt($curlhandle, CURLOPT_RETURNTRANSFER, 1); 

$response = curl_exec($curlhandle); 
curl_close($curlhandle); 

$json = json_decode($response); 


foreach ($json->data->items as $result) 
{ 

     echo '<div class="video"><a href="'.$result->player->default.'" target="_blank">'; 
     echo '<img src="'.$result->thumbnail->hqDefault.'">'; 
     echo ' <div class="title"> '.$result->title.'</div><div class="rating">'.$result->likeCount.'</div></a></div>'; 
     //print_r($result); 

} 

>

+1

Phần quan trọng ở đây là thêm 'v = 2' vào hàng đợi thông số ry. Lượt thích và lượt không thích không hiển thị trong phiên bản đầu tiên của API. – johnf

2

Nếu bạn đang sử dụng Java API, sau đó bạn có thể nhận được những cầu thủ như không thích như sau:

YtRaing ytRating = videoEntry.getYtRating(); 
int likes = ytRating.getNumLikes(); 
int dislikes = ytRating.getNumDislikes(); 

videoĐoạn, là biến VideoEntry từ (com.google.gdata.data.youtube.VideoEntry)

3

Nếu bạn đang tự hỏi nơi không thích là, dislikeCount = số xếp hạng - likeCount

0

Likes đếm, Không thích có thể được lấy bằng cách thiết lập phần như thống kê

Dưới đây là đoạn code python:

payload = {'id': search_result["id"]["videoId"], 'part': 'statistics', 'key': DEVELOPER_KEY} 
l = requests.Session().get('https://www.googleapis.com/youtube/v3/videos', params=payload)  
print l.text 

Trả lời sẽ là:

{ 
"kind": "youtube#videoListResponse", 
"etag": "\"m2yskBQFythfE4irbTIeOgYYfBU/0NR0uhQMzlaae_et8wHFZKsdFPA\"", 
"pageInfo": { 
    "totalResults": 1, 
    "resultsPerPage": 1 
}, 
"items": [ 
    { 
    "kind": "youtube#video", 
    "etag": "\"m2yskBQFythfE4irbTIeOgYYfBU/vBL_50n3XI1eQcsdivfxN_g9c2M\"", 
    "id": "hMncTg0iBko", 
    "statistics": { 
    "viewCount": "10281", 
    "likeCount": "61", 
    "dislikeCount": "9", 
    "favoriteCount": "0", 
    "commentCount": "1" 
    } 
    } 
] 
} 
Các vấn đề liên quan