0

I tried to check on github is a json file exists but it return always false. The link is correct

var_dump (is_file('https://api.github.com/repos/ClicShopping/ClicShopping_V3/contents/includes/ClicShopping/version.json?ref=master'));

If I tried this , not works also :

var_dump (json_decode('https://api.github.com/repos/ClicShopping/ClicShopping_V3/contents/includes/ClicShopping/version.json?ref=master'));

Do you know a way to test that correctly ?

thank you

Matthias
  • 3,160
  • 2
  • 24
  • 38
Pierre
  • 3
  • 3
  • I added the `php` tag for you, good idea to have all the relevant tags. This has not much to do with github, but everything to do with php/http/stuff like that. – Matthias Sep 14 '22 at 15:22
  • turns out it did have everything to do with github.. – Matthias Sep 15 '22 at 10:49

1 Answers1

0

You have to fetch the file first with a GET request. Otherwise you are just checking if that particular string (the url) is a file (which it is not, it is a string).

In addition, that particular github api requires a User-Agent header, otherwise the request is rejected. (source). They recommend you put your github username in the header so they can contact you.

With that said, this should work (uses curl to fetch from the url):

$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_USERAGENT, "your-github-username");
curl_setopt($curl, CURLOPT_URL, "https://api.github.com/repos/ClicShopping/ClicShopping_V3/contents/includes/ClicShopping/version.json?ref=master");
$res = curl_exec($curl);
var_dump(json_decode($res));
curl_close($curl);

You might have to do sudo apt install php-curl or install it some other way if curl_init is undefined (instructions here)

Matthias
  • 3,160
  • 2
  • 24
  • 38