首页 > PHP > 新浪微博开放平台接口调用类PHP版
2012
10-01

新浪微博开放平台接口调用类PHP版

新浪微博开放平台(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. }
  37. //———————————————————————————————–
  38. 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. public function __construct($appKey, $username=null, $password=null) {
    13. $this->_appKey = $appKey;
    14. $this->curl = curl_init();
    15. if(!empty($username) && !empty($password)) {
    16. $this->setUser($username, $password);
    17. }
    18. }
    19. public function setUser($username, $password) {
    20. $this->_user[‘username’] = $username;
    21. $this->_user[‘password’] = $password;
    22. curl_setopt($this->curl , CURLOPT_USERPWD , “$username:$password”);
    23. }
    24. public function request($url, $params=array(), $method=’GET’) {
    25. $apiurl = “http://api.t.sina.com.cn/”;
    26. $apiurl .= trim($url, ‘/’);
    27. $apiurl .= ‘.json’;
    28. $params[‘source’] = $this->_appKey;
    29. if($url == ‘statuses/upload’) {
    30. $content = $this->_upload($apiurl, $params);
    31. } else {
    32. $content = $this->_request($apiurl, $params, $method);
    33. }
    34. return json_decode($content, true);
    35. }
    36. public function get($url, $params=array()) {
    37. return $this->request($url, $params, ‘GET’);
    38. }
    39. public function post($url, $params=array()) {
    40. return $this->request($url, $params, ‘POST’);
    41. }
    42. protected function _request($url, $params=array(), $method=’GET’) {
    43. curl_setopt($this->curl, CURLOPT_URL, $url);
    44. $method = strtoupper($method);
    45. switch ($method) {
    46. case ‘POST’:
    47. curl_setopt($this->curl, CURLOPT_POST, true);
    48. break;
    49. case ‘DELETE’:
    50. curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, $method);
    51. break;
    52. case ‘PUT’:
    53. curl_setopt($this->curl, CURLOPT_PUT, true);
    54. break;
    55. default:
    56. break;
    57. }
    58. curl_setopt($this->curl, CURLOPT_POSTFIELDS, http_build_query($params));
    59. curl_setopt($this->curl, CURLOPT_HEADER, false);
    60. curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true);
    61. $content = curl_exec($this->curl);
    62. return $content;
    63. }
    64. protected function _upload($url, $params=array()) {
    65. $file = file_get_contents($params[‘pic’]);
    66. $mime_type = $params[‘mime_type’];
    67. $filename = $params[‘filename’];
    68. if(empty($mime_type)) {
    69. $mime_type = ‘image/jpg’;
    70. }
    71. $boundary = uniqid(‘——————‘);
    72. $MPboundary = ‘–‘.$boundary;
    73. $endMPboundary = $MPboundary. ‘–‘;
    74. $multipartbody .= $MPboundary . “\r\n”;
    75. $multipartbody .= ‘Content-Disposition: form-data; name=”pic”; filename=”‘. $filename .'”‘. “\r\n”;
    76. $multipartbody .= “Content-Type: {$mime_type}\r\n\r\n”;
    77. $multipartbody .= $file. “\r\n”;
    78. $multipartbody .= $MPboundary . “\r\n”;
    79. $multipartbody .= ‘content-disposition: form-data; name=”source”‘.”\r\n\r\n”;
    80. $multipartbody .= $params[‘source’].”\r\n”;
    81. $multipartbody .= $MPboundary . “\r\n”;
    82. $multipartbody .= ‘content-disposition: form-data; name=”status”‘.”\r\n\r\n”;
    83. $multipartbody .= $params[‘status’].”\r\n”;
    84. $multipartbody .= “\r\n”. $endMPboundary;
    85. curl_setopt($this->curl, CURLOPT_URL, $url);
    86. curl_setopt($this->curl, CURLOPT_POST, true);
    87. curl_setopt($this->curl, CURLOPT_POSTFIELDS, $multipartbody);
    88. curl_setopt($this->curl, CURLOPT_HEADER, false);
    89. curl_setopt($this->curl, CURLOPT_HTTPHEADER, array(“Content-Type: multipart/form-data; boundary=$boundary”));
    90. curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true);
    91. $content = curl_exec($this->curl);
    92. return $content;
    93. }
    94. public function __destruct() {
    95. curl_close($this->curl);
    96. }
    97. }
    98. ?>

    //————————————————————————————–

    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. 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. public function setUser($username, $password) {
    21. $this->_user[‘username’] = $username;
    22. $this->_user[‘password’] = $password;
    23. curl_setopt($this->curl , CURLOPT_USERPWD , “$username:$password”);
    24. }

    25. public function request($url, $params=array(), $method=’GET’) {
    26. $apiurl = “http://api.t.sina.com.cn/”;
    27. $apiurl .= trim($url, ‘/’);
    28. $apiurl .= ‘.json’;
    29. $params[‘source’] = $this->_appKey;
    30. if($url == ‘statuses/upload’) {
    31. $content = $this->_upload($apiurl, $params);
    32. } else {
    33. $content = $this->_request($apiurl, $params, $method);
    34. }
    35. return json_decode($content, true);
    36. }

    37. public function get($url, $params=array()) {
    38. return $this->request($url, $params, ‘GET’);
    39. }

    40. public function post($url, $params=array()) {
    41. return $this->request($url, $params, ‘POST’);
    42. }

    43. protected function _request($url, $params=array(), $method=’GET’) {
    44. curl_setopt($this->curl, CURLOPT_POSTFIELDS, http_build_query($params));
    45. $this->_setSaeHeader($url);
    46.      
    47. $method = strtoupper($method);
    48. switch ($method) {
    49. case ‘POST’:
    50. curl_setopt($this->curl, CURLOPT_POST, true);
    51. break;
    52. case ‘DELETE’:
    53. curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, $method);
    54. break;
    55. case ‘PUT’:
    56. curl_setopt($this->curl, CURLOPT_PUT, true);
    57. break;
    58. default:
    59. break;
    60. }

    61. curl_setopt($this->curl, CURLOPT_HEADER, false);
    62. curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true);
    63. $content = curl_exec($this->curl);
    64. return $content;
    65. }

    66. protected function _upload($url, $params=array()) {
    67. $file = file_get_contents($params[‘pic’]);
    68. $mime_type = $params[‘mime_type’];
    69. $filename = $params[‘filename’];
    70. if(empty($mime_type)) {
    71. $mime_type = ‘image/jpg’;
    72. }
    73. $boundary = uniqid(‘——————‘);
    74. $MPboundary = ‘–‘.$boundary;
    75. $endMPboundary = $MPboundary. ‘–‘;

    76. $multipartbody .= $MPboundary . “\r\n”;
    77. $multipartbody .= ‘Content-Disposition: form-data; name=”pic”; filename=”‘. $filename .'”‘. “\r\n”;
    78. $multipartbody .= “Content-Type: {$mime_type}\r\n\r\n”;
    79. $multipartbody .= $file. “\r\n”;

    80. $multipartbody .= $MPboundary . “\r\n”;
    81. $multipartbody .= ‘content-disposition: form-data; name=”source”‘.”\r\n\r\n”;
    82. $multipartbody .= $params[‘source’].”\r\n”;

    83. $multipartbody .= $MPboundary . “\r\n”;
    84. $multipartbody .= ‘content-disposition: form-data; name=”status”‘.”\r\n\r\n”;
    85. $multipartbody .= $params[‘status’].”\r\n”;
    86. $multipartbody .= “\r\n”. $endMPboundary;

    87. curl_setopt($this->curl, CURLOPT_POST, true);
    88. curl_setopt($this->curl, CURLOPT_POSTFIELDS, $multipartbody);
    89. curl_setopt($this->curl, CURLOPT_HEADER, false);
    90. $http_header = array(“Content-Type: multipart/form-data; boundary=$boundary”);

    91. $this->_setSaeHeader($url, $http_header);

    92. curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true);
    93. $content = curl_exec($this->curl);
    94. return $content;
    95. }

    96. protected function _setSaeHeader($url, $http_header=array()) {
    97. $header_array = array();
    98.        $header_array[“FetchUrl”] = $url;
    99.        $header_array[‘TimeStamp’] = date(‘Y-m-d H:i:s’);
    100.        $header_array[‘AccessKey’] = $_SERVER[‘HTTP_ACCESSKEY’];
    101.       
    102.        $content=”FetchUrl”;
    103.        $content.=$header_array[“FetchUrl”];
    104.        $content.=”TimeStamp”;
    105.        $content.=$header_array[‘TimeStamp’];
    106.        $content.=”AccessKey”;
    107.        $content.=$header_array[‘AccessKey’];
    108.         
    109.        $header_array[‘Signature’] = base64_encode(hash_hmac(‘sha256’,$content, $_SERVER[‘HTTP_SECRETKEY’] ,true));

    110.        foreach($header_array as $k => $v)
    111.          array_push($http_header, $k.’: ‘.$v);
    112.         
    113.        curl_setopt($this->curl, CURLOPT_HTTPHEADER, $http_header);
    114.        curl_setopt($this->curl, CURLOPT_URL, SAE_FETCHURL_SERVICE_ADDRESS);
    115. }

    116. public function __destruct() {
    117. curl_close($this->curl);
    118. }
    119. }

    120. ?>
最后编辑:
作者:sunny5156
喜欢技术....

留下一个回复