0 Star 0 Fork 239

coolcooldee / sns_PHP

forked from 祺爸 / sns_PHP 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
baidu.php 2.57 KB
一键复制 编辑 原始数据 按行查看 历史
piscdong 提交于 2013-05-16 16:59 . first commit
<?php
/**
* PHP Library for baidu.com
*
* @author PiscDong (http://www.piscdong.com/)
*/
class baiduPHP
{
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(
'response_type'=>'code',
'client_id'=>$this->client_id,
'redirect_uri'=>$callback_url,
'scope'=>$scope,
'state'=>md5(time()),
'display'=>'page'
);
return 'https://openapi.baidu.com/oauth/2.0/authorize?'.http_build_query($params);
}
//获取access token
public function access_token($callback_url, $code){
$params=array(
'grant_type'=>'authorization_code',
'code'=>$code,
'client_id'=>$this->client_id,
'client_secret'=>$this->client_secret,
'redirect_uri'=>$callback_url
);
$url='https://openapi.baidu.com/oauth/2.0/token';
return $this->http($url, http_build_query($params), 'POST');
}
//使用refresh token获取新的access token
public function access_token_refresh($refresh_token){
$params=array(
'grant_type'=>'refresh_token',
'refresh_token'=>$refresh_token,
'client_id'=>$this->client_id,
'client_secret'=>$this->client_secret
);
$url='https://openapi.baidu.com/oauth/2.0/token';
return $this->http($url, http_build_query($params), 'POST');
}
//获取登录用户信息
public function me(){
$params=array();
$url='https://openapi.baidu.com/rest/2.0/passport/users/getLoggedInUser';
return $this->api($url, $params);
}
//调用接口
public function api($url, $params, $method='GET'){
$params['access_token']=$this->access_token;
if($method=='GET'){
$result=$this->http($url.'?'.http_build_query($params));
}else{
$result=$this->http($url, http_build_query($params), 'POST');
}
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: baiduPHP(piscdong.com)";
curl_setopt($ci, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ci, CURLOPT_URL, $url);
$response=curl_exec($ci);
curl_close($ci);
$json_r=array();
if($response!='')$json_r=json_decode($response, true);
return $json_r;
}
}
PHP
1
https://gitee.com/dee/sns_php.git
git@gitee.com:dee/sns_php.git
dee
sns_php
sns_PHP
master

搜索帮助