免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 15348 | 回复: 6
打印 上一主题 下一主题

新浪微博开放平台接口调用类PHP版 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-05-29 10:47 |只看该作者 |倒序浏览
本帖最后由 网鬼 于 2010-05-29 10:59 编辑

新浪微博开放平台(http://open.t.sina.com.cn)接口调用PHP版
下载地址:http://code.google.com/p/sinatopenapi/downloads/list
懒得下载的直接到二楼复制代码
另有新浪云平台SAE(Sina App Engine)http://sae.sina.com.cn/ 版本

下面是测试DEMO:
  1. header("Content-type:text/html;charset=utf-8");
  2. require('SinaOpenApi.php');
  3. $openapi = new SinaOpenApi('App Key');
  4. $openapi->setUser('微博登录账户', '密码');

  5. //上传图片测试
  6. $upload_params = array(
  7.         'status'=>'This is a upload file',
  8.         'pic'=>'E:\Images\animal\01.jpg',
  9.         'mime_type'=>'image/jpg',
  10.         'filename'=>'01.jpg',
  11. );
  12. //开放平台接口
  13. $urls = array(
  14. //url:请求地址,params:请求参数(参照接口说明),method:请求方式,目前只支持GET和POST方法
  15.         array('url'=>'statuses/public_timeline','params'=>array('page'=>1, 'count'=>5), 'method'=>'GET'),
  16. //        array('url'=>'statuses/friends_timeline','params'=>array(), 'method'=>'GET'),
  17. //        array('url'=>'statuses/user_timeline','params'=>array(), 'method'=>'GET'),
  18. //        array('url'=>'statuses/mentions','params'=>array(), 'method'=>'GET'),
  19. //        array('url'=>'statuses/comments_timeline','params'=>array(), 'method'=>'GET'),
  20. //        array('url'=>'statuses/comments_by_me','params'=>array(), 'method'=>'GET'),
  21. //        array('url'=>'statuses/comments','params'=>array(), 'method'=>'GET'),
  22. //        array('url'=>'statuses/counts','params'=>array(), 'method'=>'GET'),
  23. //        array('url'=>'statuses/unread','params'=>array(), 'method'=>'GET'),
  24. //        array('url'=>'statuses/show/123456','params'=>array('id'=>'123456'), 'method'=>'GET'),
  25. //        array('url'=>'statuses/update','params'=>array('status'=>'This is a test blog!'), 'method'=>'POST'),
  26. //        array('url'=>'statuses/upload','params'=>$upload_params, 'method'=>'POST'),
  27. //        array('url'=>'statuses/destroy/123456','params'=>array('id'=>123456), 'method'=>'POST'),
  28. //        array('url'=>'statuses/repost','params'=>array('id'=>123456, 'status'=>'test repost'), 'method'=>'POST'),
  29. //        array('url'=>'statuses/comment','params'=>array('id'=>123456, 'comment'=>'test comment'), 'method'=>'POST'),
  30. //        array('url'=>'direct_messages','params'=>array('page'=>1, 'count'=>5), 'method'=>'GET'),
  31. //.....
  32. );

  33. foreach ($urls as $url) {
  34.         $content = $openapi->request($url['url'], $url['params'], $url['method']);
  35.         var_dump($content);
  36. }
复制代码

论坛徽章:
0
2 [报告]
发表于 2010-05-29 10:48 |只看该作者
本帖最后由 网鬼 于 2010-05-29 11:03 编辑

SinaOpenApi.php

  1. <?php
  2. /**
  3. * 新浪微博开放平台接口调用处理类
  4. * 此类只适用于PHP5环境及需要CURL扩展库支持
  5. *
  6. */

  7. class SinaOpenApi
  8. {
  9.         public $curl;
  10.         protected $_user = array();
  11.         protected $_appKey;
  12.        
  13.         public function __construct($appKey, $username=null, $password=null) {
  14.                 $this->_appKey = $appKey;
  15.                 $this->curl = curl_init();
  16.                 if(!empty($username) && !empty($password)) {
  17.                         $this->setUser($username, $password);
  18.                 }
  19.         }
  20.        
  21.         public function setUser($username, $password) {
  22.                 $this->_user['username'] = $username;
  23.                 $this->_user['password'] = $password;
  24.                 curl_setopt($this->curl , CURLOPT_USERPWD , "$username:$password");
  25.         }
  26.        
  27.         public function request($url, $params=array(), $method='GET') {
  28.                 $apiurl = "http://api.t.sina.com.cn/";
  29.                 $apiurl .= trim($url, '/');
  30.                 $apiurl .= '.json';
  31.                 $params['source'] = $this->_appKey;
  32.                 if($url == 'statuses/upload') {
  33.                         $content = $this->_upload($apiurl, $params);
  34.                 } else {
  35.                         $content = $this->_request($apiurl, $params, $method);
  36.                 }
  37.                 return json_decode($content, true);
  38.         }
  39.        
  40.         public function get($url, $params=array()) {
  41.                 return $this->request($url, $params, 'GET');
  42.         }
  43.        
  44.         public function post($url, $params=array()) {
  45.                 return $this->request($url, $params, 'POST');
  46.         }
  47.        
  48.         protected function _request($url, $params=array(), $method='GET') {
  49.                 curl_setopt($this->curl, CURLOPT_URL, $url);
  50.                 $method = strtoupper($method);
  51.                 switch ($method) {
  52.                         case 'POST':
  53.                                 curl_setopt($this->curl, CURLOPT_POST, true);
  54.                                 break;
  55.                         case 'DELETE':
  56.                                 curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, $method);
  57.                                 break;
  58.                         case 'PUT':
  59.                                 curl_setopt($this->curl, CURLOPT_PUT, true);
  60.                                 break;
  61.                         default:
  62.                                 break;
  63.                 }
  64.                 curl_setopt($this->curl, CURLOPT_POSTFIELDS, http_build_query($params));
  65.                 curl_setopt($this->curl, CURLOPT_HEADER, false);
  66.                 curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true);
  67.                 $content = curl_exec($this->curl);
  68.                 return $content;
  69.         }
  70.        
  71.         protected function _upload($url, $params=array()) {
  72.                 $file = file_get_contents($params['pic']);
  73.                 $mime_type = $params['mime_type'];
  74.                 $filename = $params['filename'];
  75.                 if(empty($mime_type)) {
  76.                         $mime_type = 'image/jpg';
  77.                 }
  78.                 $boundary = uniqid('------------------');
  79.                 $MPboundary = '--'.$boundary;
  80.                 $endMPboundary = $MPboundary. '--';
  81.                
  82.                 $multipartbody .= $MPboundary . "\r\n";
  83.                 $multipartbody .= 'Content-Disposition: form-data; name="pic"; filename="'. $filename .'"'. "\r\n";
  84.                 $multipartbody .= "Content-Type: {$mime_type}\r\n\r\n";
  85.                 $multipartbody .= $file. "\r\n";

  86.                 $multipartbody .= $MPboundary . "\r\n";
  87.                 $multipartbody .= 'content-disposition: form-data; name="source"'."\r\n\r\n";
  88.                 $multipartbody .= $params['source']."\r\n";
  89.                
  90.                 $multipartbody .= $MPboundary . "\r\n";
  91.                 $multipartbody .= 'content-disposition: form-data; name="status"'."\r\n\r\n";
  92.                 $multipartbody .= $params['status']."\r\n";
  93.                 $multipartbody .= "\r\n". $endMPboundary;
  94.                
  95.                 curl_setopt($this->curl, CURLOPT_URL, $url);
  96.                 curl_setopt($this->curl, CURLOPT_POST, true);       
  97.                 curl_setopt($this->curl, CURLOPT_POSTFIELDS, $multipartbody);
  98.                 curl_setopt($this->curl, CURLOPT_HEADER, false);
  99.                 curl_setopt($this->curl, CURLOPT_HTTPHEADER, array("Content-Type: multipart/form-data; boundary=$boundary"));
  100.                 curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true);
  101.                 $content = curl_exec($this->curl);
  102.                 return $content;
  103.         }
  104.        
  105.         public function __destruct() {
  106.                 curl_close($this->curl);
  107.         }
  108. }

  109. ?>
复制代码

论坛徽章:
0
3 [报告]
发表于 2010-05-29 11:04 |只看该作者
SaeOpenApi.php
  1. <?php
  2. /**
  3. * 新浪微博开放平台接口调用处理类
  4. * 此类只适用于Sina App Engine,PHP5环境及需要CURL扩展库支持
  5. * 使用HTTP普通鉴权(Basic Authentication)方式
  6. *
  7. */
  8. class SaeOpenApi
  9. {
  10.         public $curl;
  11.         protected $_user = array();
  12.         protected $_appKey;
  13.        
  14.         public function __construct($appKey, $username=null, $password=null) {
  15.                 $this->_appKey = $appKey;
  16.                 $this->curl = curl_init();
  17.                 if(!empty($username) && !empty($password)) {
  18.                         $this->setUser($username, $password);
  19.                 }
  20.         }
  21.        
  22.         public function setUser($username, $password) {
  23.                 $this->_user['username'] = $username;
  24.                 $this->_user['password'] = $password;
  25.                 curl_setopt($this->curl , CURLOPT_USERPWD , "$username:$password");
  26.         }
  27.        
  28.         public function request($url, $params=array(), $method='GET') {
  29.                 $apiurl = "http://api.t.sina.com.cn/";
  30.                 $apiurl .= trim($url, '/');
  31.                 $apiurl .= '.json';
  32.                 $params['source'] = $this->_appKey;
  33.                 if($url == 'statuses/upload') {
  34.                         $content = $this->_upload($apiurl, $params);
  35.                 } else {
  36.                         $content = $this->_request($apiurl, $params, $method);
  37.                 }
  38.                 return json_decode($content, true);
  39.         }
  40.        
  41.         public function get($url, $params=array()) {
  42.                 return $this->request($url, $params, 'GET');
  43.         }
  44.        
  45.         public function post($url, $params=array()) {
  46.                 return $this->request($url, $params, 'POST');
  47.         }
  48.        
  49.         protected function _request($url, $params=array(), $method='GET') {
  50.                 curl_setopt($this->curl, CURLOPT_POSTFIELDS, http_build_query($params));
  51.                 $this->_setSaeHeader($url);   
  52.                      
  53.                 $method = strtoupper($method);
  54.                 switch ($method) {
  55.                         case 'POST':
  56.                                 curl_setopt($this->curl, CURLOPT_POST, true);
  57.                                 break;
  58.                         case 'DELETE':
  59.                                 curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, $method);
  60.                                 break;
  61.                         case 'PUT':
  62.                                 curl_setopt($this->curl, CURLOPT_PUT, true);
  63.                                 break;
  64.                         default:
  65.                                 break;
  66.                 }
  67.                
  68.                 curl_setopt($this->curl, CURLOPT_HEADER, false);
  69.                 curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true);
  70.                 $content = curl_exec($this->curl);
  71.                 return $content;
  72.         }
  73.        
  74.         protected function _upload($url, $params=array()) {
  75.                 $file = file_get_contents($params['pic']);
  76.                 $mime_type = $params['mime_type'];
  77.                 $filename = $params['filename'];
  78.                 if(empty($mime_type)) {
  79.                         $mime_type = 'image/jpg';
  80.                 }
  81.                 $boundary = uniqid('------------------');
  82.                 $MPboundary = '--'.$boundary;
  83.                 $endMPboundary = $MPboundary. '--';
  84.                
  85.                 $multipartbody .= $MPboundary . "\r\n";
  86.                 $multipartbody .= 'Content-Disposition: form-data; name="pic"; filename="'. $filename .'"'. "\r\n";
  87.                 $multipartbody .= "Content-Type: {$mime_type}\r\n\r\n";
  88.                 $multipartbody .= $file. "\r\n";

  89.                 $multipartbody .= $MPboundary . "\r\n";
  90.                 $multipartbody .= 'content-disposition: form-data; name="source"'."\r\n\r\n";
  91.                 $multipartbody .= $params['source']."\r\n";
  92.                
  93.                 $multipartbody .= $MPboundary . "\r\n";
  94.                 $multipartbody .= 'content-disposition: form-data; name="status"'."\r\n\r\n";
  95.                 $multipartbody .= $params['status']."\r\n";
  96.                 $multipartbody .= "\r\n". $endMPboundary;
  97.                
  98.                 curl_setopt($this->curl, CURLOPT_POST, true);       
  99.                 curl_setopt($this->curl, CURLOPT_POSTFIELDS, $multipartbody);
  100.                 curl_setopt($this->curl, CURLOPT_HEADER, false);
  101.                 $http_header = array("Content-Type: multipart/form-data; boundary=$boundary");
  102.                
  103.                 $this->_setSaeHeader($url, $http_header);
  104.                
  105.                 curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true);
  106.                 $content = curl_exec($this->curl);
  107.                 return $content;
  108.         }
  109.        
  110.         protected function _setSaeHeader($url, $http_header=array()) {
  111.                 $header_array = array();
  112.         $header_array["FetchUrl"] = $url;
  113.         $header_array['TimeStamp'] = date('Y-m-d H:i:s');
  114.         $header_array['AccessKey'] = $_SERVER['HTTP_ACCESSKEY'];
  115.         
  116.         $content="FetchUrl";
  117.         $content.=$header_array["FetchUrl"];
  118.         $content.="TimeStamp";
  119.         $content.=$header_array['TimeStamp'];
  120.         $content.="AccessKey";
  121.         $content.=$header_array['AccessKey'];
  122.             
  123.         $header_array['Signature'] = base64_encode(hash_hmac('sha256',$content, $_SERVER['HTTP_SECRETKEY'] ,true));
  124.                
  125.         foreach($header_array as $k => $v)
  126.             array_push($http_header, $k.': '.$v);
  127.             
  128.         curl_setopt($this->curl, CURLOPT_HTTPHEADER, $http_header);
  129.         curl_setopt($this->curl, CURLOPT_URL, SAE_FETCHURL_SERVICE_ADDRESS);
  130.         }
  131.        
  132.         public function __destruct() {
  133.                 curl_close($this->curl);
  134.         }
  135. }

  136. ?>
复制代码

论坛徽章:
0
4 [报告]
发表于 2010-05-31 12:04 |只看该作者
不错不错~

论坛徽章:
0
5 [报告]
发表于 2010-06-04 21:45 |只看该作者
收藏了。

论坛徽章:
0
6 [报告]
发表于 2010-06-09 11:43 |只看该作者
现在是新浪的微博厉害
还是qq的微博厉害?

论坛徽章:
0
7 [报告]
发表于 2010-06-09 12:32 |只看该作者
欢迎收听 http://t.qq.com/lajabs
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP