PHP Classes

How to Implement a PHP GitHub API Client to Access Github Web Services - Github PHP API Library package blog

Recommend this page to a friend!
  All package blogs All package blogs   Github PHP API Library Github PHP API Library   Blog Github PHP API Library package blog   RSS 1.0 feed RSS 2.0 feed   Blog How to Implement a PH...  
  Post a comment Post a comment   See comments See comments (0)   Trackbacks (0)  

Author:

Viewers: 661

Last month viewers: 10

Package: Github PHP API Library

Github is a very popular site among developers of many programmers languages, as it can be used to host repositories of code that can be accessed with the Git version control tool.

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.




Loaded Article

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_number471808
age3
features266141
ssl_version_number0
version7.51.0
hosti386-pc-win32
ssl_versionOpenSSL/1.0.2h
libz_version1.2.8
protocolsdict, 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?

Using the code below, you can access GitHub API using your user credentials. Below follows some a explanations.
$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

A more direct way to access Github API from PHP, is to use the Github PHP API Library package. To use this package, you can either download the code archive or install it using the PHP Composer tool.



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.



  Post a comment Post a comment   See comments See comments (0)   Trackbacks (0)  
  All package blogs All package blogs   Github PHP API Library Github PHP API Library   Blog Github PHP API Library package blog   RSS 1.0 feed RSS 2.0 feed   Blog How to Implement a PH...