<?php
/**
* PHP Library for weibo.com
*
* @author
*/
class sinaPHP
{
function __construct($client_id, $client_secret, $access_token=NULL){
$this->client_id=$client_id;
$this->client_secret=$client_secret;
$this->access_token=$access_token;
}
function login_url($callback_url){
$params=array(
'response_type'=>'code',
'client_id'=>$this->client_id,
'redirect_uri'=>$callback_url
);
return 'https://api.weibo.com/oauth2/authorize?'.http_build_query($params);
}
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://api.weibo.com/oauth2/access_token';
return $this->http($url, http_build_query($params), 'POST');
}
/**
function access_token_refresh($refresh_token){
}
**/
function get_uid(){
$params=array();
$url='https://api.weibo.com/2/account/get_uid.json';
return $this->api($url, $params);
}
function show_user_by_id($uid){
$params=array(
'uid'=>$uid
);
$url='https://api.weibo.com/2/users/show.json';
return $this->api($url, $params);
}
function statuses_count($ids){
$params=array(
'ids'=>$ids
);
$url='https://api.weibo.com/2/statuses/count.json';
return $this->api($url, $params);
}
function get_comments_by_sid($id, $count=10, $page=1){
$params=array(
'id'=>$id,
'page'=>$page,
'count'=>$count
);
$url='https://api.weibo.com/2/comments/show.json';
return $this->api($url, $params);
}
function repost_timeline($id, $count=10, $page=1){
$params=array(
'id'=>$id,
'page'=>$page,
'count'=>$count
);
$url='https://api.weibo.com/2/statuses/repost_timeline.json';
return $this->api($url, $params);
}
function update($img_c, $pic=''){
$params=array(
'status'=>$img_c
);
if($pic!='' && is_array($pic)){
$url='https://api.weibo.com/2/statuses/upload.json';
$params['pic']=$pic;
}else{
$url='https://api.weibo.com/2/statuses/update.json';
}
return $this->api($url, $params, 'POST');
}
function user_timeline($uid, $count=10, $page=1){
$params=array(
'uid'=>$uid,
'page'=>$page,
'count'=>$count
);
$url='https://api.weibo.com/2/statuses/user_timeline.json';
return $this->api($url, $params);
}
function querymid($id, $type=1, $is_batch=0){
$params=array(
'id'=>$id,
'type'=>$type,
'is_batch'=>$is_batch
);
$url='https://api.weibo.com/2/statuses/querymid.json';
return $this->api($url, $params);
}
function api($url, $params, $method='GET'){
$params['access_token']=$this->access_token;
if($method=='GET'){
$result=$this->http($url.'?'.http_build_query($params));
}else{
if(isset($params['pic'])){
uksort($params, 'strcmp');
$str_b=uniqid('------------------');
$str_m='--'.$str_b;
$str_e=$str_m. '--';
$body='';
foreach($params as $k=>$v){
if($k=='pic'){
if(is_array($v)){
$img_c=$v[2];
$img_n=$v[1];
}elseif($v{0}=='@'){
$url=ltrim($v, '@');
$img_c=file_get_contents($url);
$url_a=explode('?', basename($url));
$img_n=$url_a[0];
}
$body.=$str_m."
";
$body.='Content-Disposition: form-data; name="'.$k.'"; filename="'.$img_n.'"'."
";
$body.="Content-Type: image/unknown
";
$body.=$img_c."
";
}else{
$body.=$str_m."
";
$body.='Content-Disposition: form-data; name="'.$k.""
";
$body.=$v."
";
}
}
$body.=$str_e;
$headers[]="Content-Type: multipart/form-data; boundary=".$str_b;
$result=$this->http($url, $body, 'POST', $headers);
}else{
$result=$this->http($url, http_build_query($params), 'POST');
}
}
return $result;
}
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: sinaPHP(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
//配置文件
header('Content-Type: text/html; charset=UTF-8');
$sina_k=''; //新浪微博应用App Key
$sina_s=''; //新浪微博应用App Secret
$callback_url='http://yoururl/callback.php'; //授权回调网址
?>
<?php
session_start(); //此示例中要使用session
require_once('config.php');
require_once('sina.php');
function getimgp($u){
//图片处理
$c=@file_get_contents($u);
$name=md5($u).'.jpg';
$mime='image/unknown';
return array($mime, $name, $c);
}
$sina_t=isset($_SESSION['sina_t'])?$_SESSION['sina_t']:'';
//检查是否已登录
if($sina_t!=''){
$sina=new sinaPHP($sina_k, $sina_s, $sina_t);
//获取登录用户id
$sina_uid=$sina->get_uid();
$uid=$sina_uid['uid'];
//获取登录用户信息
$result=$sina->show_user_by_id($uid);
var_dump($result);
/**
//发布微博
$content='微博内容';
$img='http://www.baidu.com/img/baidu_sylogo1.gif';
$img_a=getimgp($img);
if($img_a[2]!=''){
$result=$sina->update($content, $img_a);
//发布带图片微博
}else{
$result=$sina->update($content);
//发布纯文字微博
}
var_dump($result);
**/
/**
//微博列表
$result=$sina->user_timeline($uid);
var_dump($result);
**/
}else{
//生成登录链接
$sina=new sinaPHP($sina_k, $sina_s);
$login_url=$sina->login_url($callback_url);
echo '<a href="',$login_url,'">点击进入授权页面</a>';
}
?>
<?php
//授权回调页面,即配置文件中的$callback_url
session_start(); //此示例中要使用session
require_once('config.php');
require_once('sina.php');
if(isset($_GET['code']) && $_GET['code']!=''){
$o=new sinaPHP($sina_k, $sina_s);
$result=$o->access_token($callback_url, $_GET['code']);
}
if(isset($result['access_token']) && $result['access_token']!=''){
echo '授权完成,请记录<br/>access token:<input size="50" value="',$result['access_token'],'">';
//保存登录信息,此示例中使用session保存
$_SESSION['sina_t']=$result['access_token']; //access token
}else{
echo '授权失败';
}
echo '<br/><a href="./">返回</a>';
?>