github支持php_github.php

/**

* GitHub API client for PHP

*

* @author PiscDong (http://www.piscdong.com/)

*/

class githubPHP

{

public $api_url='https://api.github.com/';

public function __construct($client_id, $client_secret, $access_token=NULL){

$this->client_id=$client_id;

$this->client_secret=$client_secret;

$this->access_token=$access_token;

}

//生成授权网址

public function login_url($callback_url, $scope=''){

$params=array(

'client_id'=>$this->client_id,

'redirect_uri'=>$callback_url,

'scope'=>$scope

);

return 'https://github.com/login/oauth/authorize?'.http_build_query($params);

}

//获取access token

public function access_token($callback_url, $code){

$params=array(

'code'=>$code,

'client_id'=>$this->client_id,

'client_secret'=>$this->client_secret,

'redirect_uri'=>$callback_url

);

$url='https://github.com/login/oauth/access_token';

$result_str=$this->http($url, http_build_query($params), 'POST');

$json_r=array();

if($result_str!='')parse_str($result_str, $json_r);

return $json_r;

}

/**

//使用refresh token获取新的access token,GitHub暂时不支持

public function access_token_refresh($refresh_token){

}

**/

//获取登录用户信息

public function me(){

$params=array();

return $this->api('user', $params);

}

//获取登录用户代码仓库

public function repos(){

$params=array();

return $this->api('user/repos', $params);

}

//获取登录用户代码片段

public function gists(){

$params=array();

return $this->api('gists', $params);

}

//调用接口

/**

//示例:获取登录用户的issues

$result=$github->api('user/issues', array(), 'GET');

**/

public function api($url, $params=array(), $method='GET'){

$url=$this->api_url.$url;

$params['access_token']=$this->access_token;

if($method=='GET'){

$result_str=$this->http($url.'?'.http_build_query($params));

}else{

$result_str=$this->http($url, http_build_query($params), 'POST');

}

$result=array();

if($result_str!='')$result=json_decode($result_str, true);

return $result;

}

//提交请求

private function http($url, $postfields='', $method='GET', $headers=array()){

$ci=curl_init();

curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, FALSE);

curl_setopt($ci, CURLOPT_RETURNTRANSFER, 1);

curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, 30);

curl_setopt($ci, CURLOPT_TIMEOUT, 30);

if($method=='POST'){

curl_setopt($ci, CURLOPT_POST, TRUE);

if($postfields!='')curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields);

}

$headers[]='User-Agent: GitHub.PHP(piscdong.com)';

curl_setopt($ci, CURLOPT_HTTPHEADER, $headers);

curl_setopt($ci, CURLOPT_URL, $url);

$response=curl_exec($ci);

curl_close($ci);

return $response;

}

}

一键复制

编辑

Web IDE

原始数据

按行查看

历史

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值