How can I check the existence of a GitHub repo?
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?
Check out the GitHub API, that might be the best way to proceed.
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.
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.
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.
精彩评论