Home > Software engineering >  Have a good resut on testing json on github
Have a good resut on testing json on github

Time:09-16

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

CodePudding user response:

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)

  • Related