Я хочу получить счет подписчика с моего канала YouTube с новым API v3.Подбор подписчиков YouTube с помощью нового API YouTube v3
- Я создал приложение API Google для YouTube здесь: Google API Console
- У меня есть и ключ APP и канал YouTube ID
- Я использую "json_decode", чтобы получить объект
<?php
$url_yt = "https://www.googleapis.com/youtube/v3/channels?part=statistics&id=CHANNELID&key=APIKEY";
$yt_array = file_get_contents($url_yt);
$ytcount = json_decode($yt_array, true);
$ytsubscribers = $ytcount['items'][0]['subscriberCount'];
echo $ytsubscribers;
?>
я получаю некоторые ошибки, даже если фрагмент кода выглядит нормально.
Warning: file_get_contents() [function.file-get-contents]: SSL: Success in /ytsub2.php on line 4
Warning: file_get_contents() [function.file-get-contents]: Failed to enable crypto in /ytsub2.php on line 4
Warning: file_get_contents(https://www.googleapis.com/youtube/v3/channels?part=statistics&id=CHANNELID&key=APIKEY) [function.file-get-contents]: failed to open stream: operation failed in /ytsub2.php on line 4
Я понятия не имею, как это исправить ошибки. Кажется, мой сервер сайта не может правильно получить JSON. заранее спасибо за вашу помощь
EDIT:
Я попытался с помощью Curl без результата:
<?php
\t //function to get the remote data
\t function url_get_contents ($url) {
\t if (function_exists('curl_exec')){
\t $conn = curl_init($url);
\t curl_setopt($conn, CURLOPT_SSL_VERIFYPEER, true);
\t curl_setopt($conn, CURLOPT_FRESH_CONNECT, true);
\t curl_setopt($conn, CURLOPT_RETURNTRANSFER, 1);
\t $url_get_contents_data = (curl_exec($conn));
\t curl_close($conn);
\t }elseif(function_exists('file_get_contents')){
\t $url_get_contents_data = file_get_contents($url);
\t }elseif(function_exists('fopen') && function_exists('stream_get_contents')){
\t $handle = fopen ($url, "r");
\t $url_get_contents_data = stream_get_contents($handle);
\t }else{
\t $url_get_contents_data = false;
\t }
\t return $url_get_contents_data;
\t }
\t
\t $url_yt = "https://www.googleapis.com/youtube/v3/channels?part=statistics&id=CHANNELID&key=APIKEY";
\t $yt_array = url_get_contents($url_yt);
\t $ytcount = json_decode($yt_array, true);
\t $ytsubscribers = $ytcount['items'][0]['subscriberCount'];
// echo the youtube follower count
echo $ytsubscribers;
?>
Является ли это ваш файл 'ytsub2.php'? Можем ли мы увидеть все это, если так? – jonmrich
http://stackoverflow.com/questions/1975461/how-to-get-file-get-contents-work-with-https – cmorrissey
@jonmrich Эта страница php содержит только код для отображения followercount в качестве тестовой страницы. –