Php ile Youtube Kanal Bilgisi Çekme | Turkish Tutorial

in #utopian-io7 years ago (edited)

English Description

Hi friends In this article I will try to explain how you can draw the information of youtubeaddic channels with php visually.

First of all, let's talk about how we're going to do something. We will post the YouTube channel url information with the help of the textarea, which is one of the html form tags. We will check if the channel url information is posted with the if control, and if it has been postpone, we will split the incoming channel channels with explode. With these URLs, we'll get the channel's information with the API YouTube has provided us. The functions we'll take advantage of are explode, array_map, end, strlen, file_get_contents, json_decode, strtotime and date functions. I will include in detail below how these functions are used and how they are used. I hope it will be useful for you.

Türkçe Açıklama

Merhaba arkadaşlar bu yazımda sizlere php ile youtubeda ki kanalların bilgisini nasıl çekebiliriz görsel şekilde anlatmaya çalışacağım.

Öncelikle nasıl birşey yapacağımızdan bahsedelim. Html form etiketlerinden olan textarea ve buton yardımı ile youtube kanal url bilgilerini post ettireceğiz. if kontrolü ile kanal url bilgisinin post edilip edilmediğini kontrol edeceğiz eğer post edilmişse explode ile birden fazla gelen kanal urlleri böleceğiz. Bu urller ile youtube'un bize sağladığı api ile kanalın bilgisini çekeceğiz. Yararlanacağımız fonksiyonlar explode, array_map, end, strlen, file_get_contents, json_decode, strtotime ve date fonksiyonlarıdır. Bu fonksiyonların ne işe yaradığınız nasıl kullanıldığına aşağıda detaylı şekilde yer vereceğim. Umarım sizler için yararlı olur.

Yararlanacağımız fonksiyonlar;

isset — Değişken tanımlı mı diye bakar
explode — dizgeyi bir ayraca göre bölüp bir dizi (array) haline getirir
array_map — Belirtilen dizilerin (array) elemanlarına geri çağırım işlevini uygular
end — Bir dizinin son elemanını verir.
file_get_contents — Verilen url bağlanır kaynak kodunu verir.
json_decode — Bir JSON verisini çözümler.
strtotime — Verilen zamanı Unix timestamp formatına çevirir.
date — Tarihi veya Saati biçimlendirir
number_format — Sayıyı binlik bölümlere ayırır

Github Proje Linki;

Github Project.png

https://github.com/Ruhum36/Youtube-Channel-Information

index.php

<?php
    
    // Ruhum
    // 20.12.2017

?>
<!DOCTYPE html>
<html>
<head>
    <title>Youtube Channel Information</title>
    <link rel="stylesheet" type="text/css" href="Style.css" />
</head>
<body>

<?php
    
    if(isset($_POST['KanalURL'])){

?>
    <center><h2>Youtube Channel Information</h2></center>

    <div class="Duzen">
        
        <div class="GenelBilgiler">
            <div class="Bilgiler Bold" style="width: 40px;">Count</div>
            <div class="Bilgiler Bold KanalAdi">Channel Name</div>
            <div class="Bilgiler Bold">Views</div>
            <div class="Bilgiler Bold">Videos</div>
            <div class="Bilgiler Bold">Subscriber</div>
            <div class="Bilgiler Bold SonDiv" style="width: 150px;">Date</div>
            <div class="Temizle"></div>
        </div>
                            
<?php
        $YtApiKey = 'REPLACE_API_KEY';

        $Kanallar = $_POST['KanalURL'];
        $Kanallar = explode("\n", $Kanallar);
        $Kanallar = array_map('trim', $Kanallar);
        foreach ($Kanallar as $Key => $Kanal) {

            $KanalUser  = explode('/',$Kanal);
            $KanalUser  = end($KanalUser);
            $ApiUrl = 'https://www.googleapis.com/youtube/v3/channels?part=id,statistics,snippet&'.(strlen($KanalUser)==24?'id='.$KanalUser:'forUsername='.$KanalUser).'&key='.$YtApiKey;

            $Kaynak = file_get_contents($ApiUrl);
            $KaynakDecode = json_decode($Kaynak);

            $KanalID = $KaynakDecode->items[0]->id;

            $KanalTakipAdi = $KaynakDecode->items[0]->snippet->title;
            $KanalTarihi = $KaynakDecode->items[0]->snippet->publishedAt;
            $KanalTarihi = strtotime($KanalTarihi);
            $KanalTarihi = date('d.m.Y H:i:s',$KanalTarihi);

            $ToplamIzlenme = $KaynakDecode->items[0]->statistics->viewCount;
            $ToplamAbone = $KaynakDecode->items[0]->statistics->subscriberCount;
            $ToplamVideo = $KaynakDecode->items[0]->statistics->videoCount;

            $Baglanti = 'https://www.youtube.com/channel/'.$KanalID;
?>

            <div class="GenelBilgiler" <?=$Key%2?'':'style="background-color: #eff7ff;"'?>>
                <div class="Bilgiler" style="width: 40px;"><?=($Key+1)?></div>
                <div class="Bilgiler KanalAdi"><a href="<?=$Baglanti?>" target="_blank"><?=$KanalTakipAdi?></a></div>
                <div class="Bilgiler"><?=number_format($ToplamIzlenme)?></div>
                <div class="Bilgiler"><?=number_format($ToplamVideo)?></div>
                <div class="Bilgiler"><?=number_format($ToplamAbone)?></div>
                <div class="Bilgiler SonDiv" style="width: 150px;"><?=$KanalTarihi?></div>
                <div class="Temizle"></div>
            </div>


<?php

        }

?>

    </div>

<?php

    }

?>
    <center><h2>Youtube Channel Links</h2></center>
    <div class="Duzen">
        
        <form action="" method="post">
            <label style="margin-left: 10px; margin-top: 5px; font-weight: bold; color: #474747; float: left;">Channel Links</label>
            <textarea placeholder="Examples:&#10;https://www.youtube.com/user/googlechrome&#10;https://www.youtube.com/user/Google&#10;https://www.youtube.com/channel/UCtxkCXCD8bWpE8Ea_l4tV2A" name="KanalURL" id="KanalURL"></textarea>
            <input type="submit" value="Check" id="KontrolEt">
        </form>
        <div class="Temizle"></div>

    </div>

</body>
</html>

Script Görseli;

1.png

index.php Kodları;

PhpCodes.png

Style Kodları;

CssCodes.png

Kodlar ve Açıklamaları;

Formlar.png

    <center><h2>Youtube Channel Links</h2></center>
    <div class="Duzen">
        
        <form action="" method="post">
            <label style="margin-left: 10px; margin-top: 5px; font-weight: bold; color: #474747; float: left;">Channel Links</label>
            <textarea placeholder="Examples:&#10;https://www.youtube.com/user/googlechrome&#10;https://www.youtube.com/user/Google&#10;https://www.youtube.com/channel/UCtxkCXCD8bWpE8Ea_l4tV2A" name="KanalURL" id="KanalURL"></textarea>
            <input type="submit" value="Check" id="KontrolEt">
        </form>
        <div class="Temizle"></div>

    </div>

Burada html form etiketleri ile kanalların url bilgilerini post ettireceğiz.

php codes.png

                            
<?php
        $YtApiKey = 'REPLACE_API_KEY';

        $Kanallar = $_POST['KanalURL'];
        $Kanallar = explode("\n", $Kanallar);
        $Kanallar = array_map('trim', $Kanallar);
        foreach ($Kanallar as $Key => $Kanal) {

            $KanalUser  = explode('/',$Kanal);
            $KanalUser  = end($KanalUser);
            $ApiUrl = 'https://www.googleapis.com/youtube/v3/channels?part=id,statistics,snippet&'.(strlen($KanalUser)==24?'id='.$KanalUser:'forUsername='.$KanalUser).'&key='.$YtApiKey;

            $Kaynak = file_get_contents($ApiUrl);
            $KaynakDecode = json_decode($Kaynak);

            $KanalID = $KaynakDecode->items[0]->id;

            $KanalTakipAdi = $KaynakDecode->items[0]->snippet->title;
            $KanalTarihi = $KaynakDecode->items[0]->snippet->publishedAt;
            $KanalTarihi = strtotime($KanalTarihi);
            $KanalTarihi = date('d.m.Y H:i:s',$KanalTarihi);

            $ToplamIzlenme = $KaynakDecode->items[0]->statistics->viewCount;
            $ToplamAbone = $KaynakDecode->items[0]->statistics->subscriberCount;
            $ToplamVideo = $KaynakDecode->items[0]->statistics->videoCount;

            $Baglanti = 'https://www.youtube.com/channel/'.$KanalID;
?>

            <div class="GenelBilgiler" <?=$Key%2?'':'style="background-color: #eff7ff;"'?>>
                <div class="Bilgiler" style="width: 40px;"><?=($Key+1)?></div>
                <div class="Bilgiler KanalAdi"><a href="<?=$Baglanti?>" target="_blank"><?=$KanalTakipAdi?></a></div>
                <div class="Bilgiler"><?=number_format($ToplamIzlenme)?></div>
                <div class="Bilgiler"><?=number_format($ToplamVideo)?></div>
                <div class="Bilgiler"><?=number_format($ToplamAbone)?></div>
                <div class="Bilgiler SonDiv" style="width: 150px;"><?=$KanalTarihi?></div>
                <div class="Temizle"></div>
            </div>


<?php

        }

?>

-38. Satır: Gelen kanal urlleri explode ile bölüyoruz. $Kanallar değişkenine dizi olarak atıyoruz.
-39. Satır: array_map fonksiyonu ile dizilerdeki boşlukları siliyoruz.
-40. Satır: $Kanallar değişkenine aktardığımız dizileri foreach ile tek tek okuyoruz.
-42. 43. Satır: Youtube kanal urllerindeki son kısımda yazan yt id sini alıyoruz.
-46. Satır: Youtube'un bize verdiği api url adresi ile aldığımız kanal id sini file_get_contents ile kaynağını alıyoruz.
-47. Satır: Youtube api bize yanıtı json ile verdiği için json_decode ile bunları parçalara ayırıyoruz. json_decode bize sonucu object olarak veriyor.
-49. 51. 52. 56. 57. 58. Satır: json_decode ile parçaladığımız değerliri çekip alıyoruz.
-53. Satır: Youtube bize verdiği tarih formatını unix türüne çeviriyoruz.
-54. Satır: Unix formatına çevrilen tarihi date fonksiyonu ile istediğimiz formata yani gg.aa.yyyy ss:dd:ss formatına çeviriyoruz.

Aklınıza takılan soru olursa, önerileriniz, anlatmamı istediğiniz bir eğitim içeriği varsa lütfen konu altında bana bildirin. Elimden geleni seve seve yaparım. Umarım hepiniz için yararlı bir konu olmuştur.



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

Your contribution cannot be approved because it does not follow the Utopian Rules.

You already shared the code in https://utopian.io/utopian-io/@ruhum/5f63wq-youtube-channel-information-script-or-php-open-source . Submitting the same content is not allowed.

This behaviour might be seen as an attempt to abuse the Utopian system, which could lead to ban. You created the code (the repository) just for the sake of these posts.

You can contact us on Discord.
[utopian-moderator]

Coin Marketplace

STEEM 0.16
TRX 0.15
JST 0.028
BTC 54391.40
ETH 2283.95
USDT 1.00
SBD 2.29