下面是我到目前为止创建的代码,我的目标是使用youtube Search:list api查找特定游戏的流,然后发布该游戏的流数量,我有一个游戏数据库这是我的下面的功能,我的api链接确实有效,我只是无法从中获取Info.totalResults,任何帮助都会很棒,感谢您提供的任何帮助
function totalgamelist() {
global $wpdb;
$gamelistname = $wpdb->prefix . 'Games';
global $wpdb;
$getgames = $wpdb->get_results( 'SELECT * FROM '.$gamelistname , OBJECT );
if (empty($getgames)) {
//empty array
echo 'This is empty sorry!';
} else {
echo 'We Got Something!';
foreach ( $getgames as $getgame )
{
echo '<div class="gamename">'.$getgame->GameTitle;
$JSON = file_get_contents("https://www.googleapis.com/youtube/v3/search?part=snippet&eventType=live&type=video&videoCategoryId=20®ionCode=US&maxResults=50&q='.$getgame->GameTitle.'&key=[API KEY]");
$json_data = json_decode($JSON, true);
echo $json_data['totalResults'];
echo '</div>';
}
}
}
编辑:
所以在johnh10的帮助下,我能够发现虽然echo johnh10给出的是正确的但是我的能力不是显示结果:)这也是我的服务器阻止访问我的网址要求查看。下面是我用来访问网址的卷曲代码,希望它可以帮助其他人。
$urlgame = 'https://www.googleapis.com/youtube/v3/search?part=snippet&eventType=live&type=video&videoCategoryId=20®ionCode=US&maxResults=1&q='.$getgame->GameTitle.'&key=[API Key]';
$ch = curl_init($urlgame);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
$json_data = json_decode($data, true);
if (!empty($json_data)) {
$streamnumber = $json_data['pageInfo']['totalResults'];
echo ' Streams:'.$streamnumber;
} else {
echo ' Streams: No Streams Found';
}
答案 0 :(得分:0)
echo $json_data['pageInfo']['totalResults'];