Harika bir talep! Bu işlemi tek bir PHP komutuyla, konsoldan çalışacak şekilde yapalım. file_get_contents ile URL'den veri çekecek, json_decode ile JSON'u parse edecek ve json_encode ile sonuçları tekrar JSON olarak formatlayacağız.
GitHub API'nin rate limitlerine takılmamak için dikkatli olmak gerekir. Bu örnek, küçük bir veri seti için iyi çalışacaktır. Eğer çok fazla proje varsa, bir süre sonra API çağrıları engellenebilir.
Aşağıdaki komut, spring-projects organizasyonundaki depoları çekecek ve ardından her bir deponun dil bilgilerini toplayıp istediğiniz formatta çıktı verecektir.
Kullanım:
Bu tek satırlık kodu doğrudan terminalinizde çalıştırabilirsiniz.
php -r "$initialUrl='https://api.github.com/orgs/spring-projects/repos';$repoData=json_decode(@file_get_contents($initialUrl),true);if($repoData===null){echo json_encode(['error'=>'Failed to fetch initial repository list or parse JSON.']);exit(1);}$projectLanguageUrls=[];foreach($repoData as $item){if(isset($item['full_name']))$projectLanguageUrls[]=['full_name'=>$item['full_name'],'url'=>'https://api.github.com/repos/'.$item['full_name'].'/languages'];}$rawData=[];$totalLinesPerLanguage=[];$totalLinesPerProject=[];$grandTotalLines=0;foreach($projectLanguageUrls as $project){$fullName=$project['full_name'];$langUrl=$project['url'];$langJson=@file_get_contents($langUrl);if($langJson===false){error_log('Failed to fetch '.$langUrl);continue;}$languages=json_decode($langJson,true);if($languages===null){error_log('Failed to parse JSON from '.$langUrl);continue;}$rawData[$fullName]=$languages;$currentProjectTotal=0;foreach($languages as $lang=>$lines){$totalLinesPerLanguage[$lang]=($totalLinesPerLanguage[$lang]??0)+$lines;$currentProjectTotal+=$lines;}$totalLinesPerProject[$fullName]=$currentProjectTotal;$grandTotalLines+=$currentProjectTotal;}$finalResult=['rawData'=>$rawData,'totalLinesPerLanguage'=>$totalLinesPerLanguage,'totalLinesPerProject'=>$totalLinesPerProject,'grandTotalLines'=>$grandTotalLines];echo json_encode($finalResult,JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);"
Kodu Açıklaması (Parçalı Halde Okuması Daha Kolay):
<?php
// 1. Başlangıç URL'si (örneğin, bir organizasyonun depolarını listeler)
$initialUrl = 'https://api.github.com/orgs/spring-projects/repos';
// 2. İlk URL'den veriyi çek ve JSON'u parse et
// @file_get_contents: Hata durumunda uyarıları gizler, manuel kontrol yaparız
$repoData = json_decode(@file_get_contents($initialUrl), true);
// Hata kontrolü
if ($repoData === null) {
echo json_encode(['error' => 'Failed to fetch initial repository list or parse JSON.']);
exit(1); // Hata durumunda scripti sonlandır
}
// 3. Dil bilgilerini çekeceğimiz URL'leri oluşturmak için boş bir dizi
$projectLanguageUrls = [];
// 4. İlk veriden her bir projenin full_name'ini alarak dil URL'lerini oluştur
foreach ($repoData as $item) {
if (isset($item['full_name'])) {
$projectLanguageUrls[] = [
'full_name' => $item['full_name'],
'url' => 'https://api.github.com/repos/' . $item['full_name'] . '/languages'
];
}
}
// 5. Nihai çıktı için veri yapılarını başlat
$rawData = []; // Her projenin ham dil verisi
$totalLinesPerLanguage = []; // Her dilin toplam satır sayısı
$totalLinesPerProject = []; // Her projenin toplam satır sayısı
$grandTotalLines = 0; // Tüm projelerin tüm dillerdeki toplam satır sayısı
// 6. Oluşturulan her dil URL'sini tek tek çek ve işle
foreach ($projectLanguageUrls as $project) {
$fullName = $project['full_name'];
$langUrl = $project['url'];
// Curl ile çekmek yerine file_get_contents kullandık, tek satırda daha pratik
$langJson = @file_get_contents($langUrl);
// Çekme veya JSON parse etme hatası durumunda logla ve atla
if ($langJson === false) {
error_log('Failed to fetch ' . $langUrl);
continue;
}
$languages = json_decode($langJson, true);
if ($languages === null) {
error_log('Failed to parse JSON from ' . $langUrl);
continue;
}
// Ham veriyi kaydet
$rawData[$fullName] = $languages;
// Mevcut projenin toplam satır sayısını hesapla
$currentProjectTotal = 0;
foreach ($languages as $lang => $lines) {
// Her dil için genel toplamı güncelle
$totalLinesPerLanguage[$lang] = ($totalLinesPerLanguage[$lang] ?? 0) + $lines;
$currentProjectTotal += $lines;
}
// Her projenin toplam satır sayısını kaydet
$totalLinesPerProject[$fullName] = $currentProjectTotal;
// Tüm projelerin genel toplam satır sayısını güncelle
$grandTotalLines += $currentProjectTotal;
}
// 7. Tüm sonuçları tek bir dizi içinde topla
$finalResult = [
'rawData' => $rawData,
'totalLinesPerLanguage' => $totalLinesPerLanguage,
'totalLinesPerProject' => $totalLinesPerProject,
'grandTotalLines' => $grandTotalLines
];
// 8. Sonuçları JSON formatında, okunabilir (pretty print) ve slash'leri kaçırmadan yazdır
echo json_encode($finalResult, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
?>