A
A
Andrej Kopp2018-04-09 19:55:47
PHP
Andrej Kopp, 2018-04-09 19:55:47

How to parse Github API in PHP?

Hello. I'm trying to display a list of repository releases on the site through the Github API.

$url = 'https://api.github.com/repos/modxcms/revolution/tags';
$json = file_get_contents($url);
$releases = json_decode($json);

echo '<pre>';
var_dump($releases);
echo '</pre>';

displays NULL
if you follow the link https://api.github.com/repos/modxcms/revolution/tags you can see that there is JSON. You need to get an array with data. What am I doing wrong?
UPD. 04/09/2018
Working version:
<?php
$url = 'https://api.github.com/repos/modxcms/revolution/tags';
$cURL = curl_init();

curl_setopt($cURL, CURLOPT_URL, $url);
curl_setopt($cURL, CURLOPT_HTTPGET, true);
curl_setopt($cURL, CURLOPT_RETURNTRANSFER, true);
curl_setopt($cURL, CURLOPT_FOLLOWLOCATION, true);

curl_setopt($cURL, CURLOPT_HTTPHEADER, array(
    'User-Agent: https://api.github.com/meta'
));

$result = curl_exec($cURL);

curl_close($cURL);

$json = json_decode($result, true);
//print_r($json);

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Artray, 2018-04-09
@sequelone

Missing 'User-Agent' header
You can do this:

<?
$context = stream_context_create([
  'http' => [
     'header' => 'User-Agent: Awesome-Octocat-App'
   ]
]);

$url = 'https://api.github.com/repos/modxcms/revolution/tags';
$json = file_get_contents($url, false, $context);

$releases = json_decode($json, true);

echo '<pre>';
var_dump($releases);
echo '</pre>';

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question