我在过去的几个小时里遇到了一个问题,但还没有找到解决方案。我正在尝试使用laravel中的php-youtube-api参考链接获得youtube频道的名称。但我的输出只有一个通道。但我想要一个频道相关名称的列表,像这样。当我搜索任何名称为"soccerit"的频道时,返回
stdClass Object
(
[kind] => youtube#channel
[etag] => "kjEFmP90GvrCl8BObMQtGoRfgaQ/I7mfLJg2NMc3uuc6rLgQDgQ-_3g"
[id] => UClENohulIkpi8JV18UJIPeg
[snippet] => stdClass Object
(
[title] => soccer
[description] =>
[publishedAt] => 2005-11-07T19:54:17.000Z
[thumbnails] => stdClass Object
(
[default] => stdClass Object
(
[url] => https://yt3.ggpht.com/-NcE9VxoZBwQ/AAAAAAAAAAI/AAAAAAAAAAA/F923oDS5GIc/s88-c-k-no/photo.jpg
)
[medium] => stdClass Object
(
[url] => https://yt3.ggpht.com/-NcE9VxoZBwQ/AAAAAAAAAAI/AAAAAAAAAAA/F923oDS5GIc/s240-c-k-no/photo.jpg
)
[high] => stdClass Object
(
[url] => https://yt3.ggpht.com/-NcE9VxoZBwQ/AAAAAAAAAAI/AAAAAAAAAAA/F923oDS5GIc/s240-c-k-no/photo.jpg
)
)
)
[contentDetails] => stdClass Object
(
[relatedPlaylists] => stdClass Object
(
[uploads] => UUlENohulIkpi8JV18UJIPeg
)
[googlePlusUserId] => 107844494883714037142
)
[statistics] => stdClass Object
(
[viewCount] => 763486
[commentCount] => 134
[subscriberCount] => 1478
[hiddenSubscriberCount] =>
[videoCount] => 32
)
)
提前谢谢。
我的代码是:
YoutubeController.php
class YoutubeController extends BaseController {
public function index()
{
$youtube = new MadcodaYoutube(array('key' => ##Yuxw99Ka7szK4'));
$channel = $youtube->getChannelByName($data['q']);
print_r($channel);
}
}
Youtube.php
public function getChannelByName($username, $optionalParams = false)
{
$API_URL = $this->getApi('channels.list');
$params = array(
'forUsername' => $username,
'part' => 'id,snippet,contentDetails,statistics,invideoPromotion'
);
if($optionalParams){
$params = array_merge($params, $optionalParams);
}
$apiData = $this->api_get($API_URL, $params);
return $this->decodeSingle($apiData);
}
public function api_get($url, $params)
{
//set the youtube key
$params['key'] = $this->youtube_key;
//boilerplates for CURL
// $url = "https://gdata.youtube.com/feeds/api/channels";
$a = $url . (strpos($url, '?') === false ? '?' : '') . http_build_query($params);
$tuCurl = curl_init();
curl_setopt($tuCurl, CURLOPT_URL, $a);
if (strpos($url, 'https') === false) {
curl_setopt($tuCurl, CURLOPT_PORT, 80);
} else {
curl_setopt($tuCurl, CURLOPT_PORT, 443);
}
curl_setopt($tuCurl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($tuCurl, CURLOPT_SSL_VERIFYPEER, 0);
$tuData = curl_exec($tuCurl);
if (curl_errno($tuCurl)) {
throw new Exception('Curl Error : ' . curl_error($tuCurl));
}
return $tuData;
}
您似乎使用了错误的方法。方法getChannelByName
返回具有确切名称的通道。如果你想搜索频道,你应该使用:
// Search playlists, channels and videos, Return an array of PHP objects
$channels = $youtube->search($data['q']);
或者如果你只想共同搜索视频,你应该选择:
// Search only Videos, Return an array of PHP objects
$channels = $youtube->searchVideos($data['q']);