Author: Mat Jung
Viewers: 661
Last month viewers: 10
Package: Github PHP API Library
An alternative way to access the project files in Github is to use its API.
Read this article to learn how you can easily access this project files hosted in Github using PHP without having to use the Git tool.
Introduction to the PHP Github Client Class
Due to a special situation, I got the inspiration to query Github repositories, not with the git program, but with the Github API.
Before I get started, do you know if anyone else already compiled useful php classes that are related with the github api?
Using Google to search for "php with github" or something similar, lead to a lot of php code that is available in a repository but not to code for accessing the repository.
In this tutorial you can learn how I work with PHP curl functions to call the web services like curl_init, curl_setopt, curl_exec, curl_getinfo.
Accessing Github from PHP using Curl Functions
Using the code below just outputs your current curl version. Let's assume the Curl extension is enabled in your PHP environment configuration set in php.ini.
echo "<table rules=\"all\" style=\"border-width: 1px; border-style: solid\">";
foreach(curl_version() as $key => $value) {
echo "<tr><td>", $key, "</td>";
if(is_array( $value )) { $value=implode(",", $value); }
echo "<td>", $value, "</tr>";
} echo "</table>";
Possible Result:
version_number | 471808 |
age | 3 |
features | 266141 |
ssl_version_number | 0 |
version | 7.51.0 |
host | i386-pc-win32 |
ssl_version | OpenSSL/1.0.2h |
libz_version | 1.2.8 |
protocols | dict, file, ftp, ftps, gopher, http, https, imap, imaps, ldap, pop3, pop3s, rtsp, scp, sftp, smtp, smtps, telnet, tftp |
How to Call Github API Using Your User Credentials?
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.github.com");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERAGENT, "php/curl");
curl_setopt($ch, CURLOPT_CAINFO, "/path/to/curl/cacert.pem");
$output = curl_exec($ch);
$err = curl_errno($ch);
$errmsg = curl_error($ch);
$info = curl_getinfo($ch);
curl_close($ch);
var_dump($err);
var_dump($errmsg);
echo "<hr>";
var_dump($info);
echo "<hr>";
var_dump($output);
if($err == 0) {
echo "great - you have access to api.github.com";
}
With the code in the first line we obtain a curl connection handler.
Then we set some parameters. You need to have an account at Github, otherwise it won't work.
For the moment, the sample is based on Username and Password based authentication.
Risk Hint: Whoever hosts your PHP code, may get your Github username and password.
Before the dumps the Curl connection handler with Github is closed.
In the Next Part of this Article
In the next part of this article, you can learn how to lookup for a repository in Github. Are you interested in this content? If so, please post a comment about this article below.Download the Code or Install the Github PHP API Library package
You need to be a registered user or login to post a comment
Login Immediately with your account on:
Comments:
No comments were submitted yet.