2017-08-17 109 views
1

下面是我迄今爲止創建的代碼,我的目標是使用YouTube搜索:列表api爲特定遊戲查找流,然後發佈多少我有一個數據庫的遊戲標題,這是我的功能如下,我的API鏈接確實工作,只是無法從它得到Info.totalResults,任何幫助將是偉大的,謝謝你的任何幫助您可以提供如何從YouTube上獲取頁面Info.totalResults api搜索:列表

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&regionCode=US&maxResults=50&q='.$getgame->GameTitle.'&key=[API KEY]"); 
$json_data = json_decode($JSON, true); 
echo $json_data['totalResults']; 

    echo '</div>'; 
} 
    } 
} 

編輯:

所以從johnh10的幫助下,我才得以發現,那不是我的顯示結果的能力雖然回聲johnh10給出的是正確的:)這也是我的服務器阻止訪問我要查看的網址。下面是我用來訪問url的curl代碼,希望它可以幫助其他人。

$urlgame = 'https://www.googleapis.com/youtube/v3/search?part=snippet&eventType=live&type=video&videoCategoryId=20&regionCode=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
echo $json_data['pageInfo']['totalResults']; 
+0

你好,感謝你抽出時間來幫幫忙,我添加了字符串,仍然可以得到什麼只是顯示空白 –