doulou0882 2019-05-02 08:59
浏览 119
已采纳

LiveChatMessages API YouTube返回null

I am trying to get messages from the live chat chat using a GET request.

$json_result = file_get_contents("https://www.googleapis.com/youtube/v3/liveChat/messages?liveChatId=$videoId&part=snippet&key=$api_key");
// $videoId и $api_key true, there are no errors.
echo '<pre>';
var_dump(json_decode($json_result));
echo '</pre>';

In the answer I get NULL, tell me what is the error or what method can I get messages from the YouTube broadcast chat?

  • 写回答

1条回答 默认 最新

  • douwen5951 2019-05-03 09:35
    关注

    Maybe it will be useful for someone, solved the issue through cURL instead of file_get_contents.

    $curl_handle=curl_init();
    curl_setopt($curl_handle, CURLOPT_URL,"https://www.googleapis.com/youtube/v3/liveChat/messages?liveChatId=$LiveChatId&part=snippet%2CauthorDetails&maxResults=200&key=$api_key");
    curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2);
    curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl_handle, CURLOPT_USERAGENT, 'JeezLand API');
    $json_results = curl_exec($curl_handle);
    curl_close($curl_handle);
    $json_decode_results = json_decode($json_results);
    foreach($json_decode_results as $json_decode_result){
        foreach($json_decode_result as $result){
            /* echo '<pre>';
                var_dump($result);
            echo '</pre>'; */
            echo $result->authorDetails->displayName.' -> '.$result->snippet->displayMessage.'<br />';
        }
    }
    

    Special thanks to Gert de Pagter for the help on solving the problem.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 解决一个加好友限制问题 或者有好的方案
  • ¥15 关于#java#的问题,请各位专家解答!
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
  • ¥30 求解达问题(有红包)