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.

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

报告相同问题?

悬赏问题

  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?