avatar
interact with the GitHub REST API to fetch repository PHP

• Getting started with the REST API. For example:

» JavaScript

const apiUrl = `https://api.github.com/repos/${owner}/${repoName}/contents`;
  const response = await fetch(apiUrl);
  if (!response.ok) {
	console.log(`Failed to fetch repository info. Status: ${response.status}`);
  }
  const repositoriesPath = await response.json();
  repositoriesPath.forEach(function(item) {
	if (item.type == "file") {
	  console.log("File");
	} else {
	  console.log("Directory");
	}
  });

» PHP

$apiUrl = "https://api.github.com/repos/{$request->owner}/{$request->repoName}/contents";
$ch = curl_init($apiUrl);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('User-Agent: PHP'));
$response = curl_exec($ch);
curl_close($ch);

if ($response === false) {
    echo "Failed to fetch repository info.";
} else {
    $repositoriesPath = json_decode($response, true);

    foreach ($repositoriesPath as $item) {
        if ($item['type'] === 'file') {
            echo "File\n";
        } else {
            echo "Directory\n";
        }
    }
}
You need to login to do this manipulation!