0

I coding a PHP script that has to check if exists a GitHub repo that the user enters. But I don't know how can I do it. I think the main matter is to know which HTTP code gives GitHub, but I can't find anything like that in cURL PHP documentation. How can I do it?

Asclepius
  • 57,944
  • 17
  • 167
  • 143
Carlos Precioso
  • 2,731
  • 3
  • 21
  • 24

4 Answers4

5

Check out the GitHub API, that might be the best way to proceed.

svick
  • 236,525
  • 50
  • 385
  • 514
4

HTTP in istelf is a rather simple protocol, so just check if you get a 404. That'd mean it doesn't exist. As an alternative to cURL, you might want to consider using get_headers, that's less intensive to write.

Berry Langerak
  • 18,561
  • 4
  • 45
  • 58
  • Assuming you're looking for a *public* repo. If you're checking a *private* repo and don't provide auth for a user that has access to it, you'll get a 404. – Tekkub Jun 04 '11 at 21:54
  • @Tekkub Fair enough. If you want to do that, the Github API would be your better chance. – Berry Langerak Jun 06 '11 at 07:41
  • You can provide a token as password to allow cURL to authenticate if you have two-factor-authentication turned on. – Henry Blyth Jan 20 '14 at 19:27
2

If the repository is private or does not exist, you should get a 404 http status header. Check against the headers that are returned by a cURL request for the HTTP status header.

damianb
  • 1,224
  • 7
  • 16
0

The 404 check is bad because it will return the same 404 and error message if the request straight up fails due to, eg auth issues. Therefore it could incorrectly tell your program that the repo doesn't exist, when it does, but you just couldn't access github API.

Asclepius
  • 57,944
  • 17
  • 167
  • 143
Ethan SK
  • 736
  • 8
  • 12