无法访问Json(twitch API)中的数组内的Json

时间:2019-09-24 17:18:10

标签: php json

试图从twitch的API响应中获取特定值(例如embed_url),但这只是行不通。

我以剪辑https://www.twitch.tv/loltyler1/clip/SteamyWildCocoaEagleEye为例。当我使用print_r($json)时会得到

Array ( 
    [data] => Array ( 
        [0] => Array ( 
            [id] => SteamyWildCocoaEagleEye 
            [url] => https://clips.twitch.tv/SteamyWildCocoaEagleEye 
            [embed_url] => https://clips.twitch.tv/embed?clip=SteamyWildCocoaEagleEye 
            [broadcaster_id] => 51496027 
            [broadcaster_name] => loltyler1 
            [creator_id] => 142549191 
            [creator_name] => cleancurtis 
            [video_id] => 483238092 
            [game_id] => 21779 
            [language] => en 
            [title] => tyler1 lee sin cosplay 
            [view_count] => 72591 
            [created_at] => 2019-09-19T02:32:17Z 
            [thumbnail_url] => https://clips-media-assets2.twitch.tv/AT-cm%7C536825620-preview-480x272.jpg 
        ) 
    ) 
    [pagination] => Array ( ) 
)

但我只是不知道如何提取embed_url值。谢谢

$link = $_POST['link'];

preg_match("/[^\/]+$/", "$link", $matches);
$id = $matches[0];

$clientId = 'XXXXXXXXXXXXX';
$ch = curl_init();

curl_setopt_array($ch, array(
    CURLOPT_HTTPHEADER => array(
        'Client-ID: ' . $clientId
    ),
    CURLOPT_SSL_VERIFYPEER => false,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_URL => "https://api.twitch.tv/helix/clips?id=".$id
));

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

// -
$json = json_decode($response, TRUE);

echo $json['data']->embed_url;

0 个答案:

没有答案