Statuses/upload
目录 |
statuses/upload
上传图片及发布微博信息。请求必须用POST方式提交。为防止重复,发布的信息与当前最新信息一样话,将会被忽略。目前上传图片大小限制为<1M。
URL
curl http://api.t.sina.com.cn/statuses/upload.format
格式
xml, json
HTTP请求方式
POST, 必须用Multipart/form-data方式
是否需要身份验证
true
请求数限制
true
请求参数
- status. 必填参数, 要更新的微博信息。必须做URLEncode,信息内容不超过140个汉字。支持全角、半角字符。
- pic. 必填参数。仅支持JPEG,GIF,PNG图片,为空返回400错误。目前上传图片大小限制为<1M。
- lat. 可选参数,纬度,发表当前微博所在的地理位置,有效范围 -90.0到+90.0, +表示北纬。只有用户设置中geo_enabled=true时候地理位置才有效。(保留字段,暂不支持)
- long. 可选参数,经度。有效范围-180.0到+180.0, +表示东经。(保留字段,暂不支持)
使用说明
- 如果没有登录或超过发布上限,将返回403错误
- 系统将忽略重复发布的信息。每次发布将比较最后一条发布消息,如果一样将被忽略。因此用户不能连续提交相同信息。发布成功返回发布的信息ID,否则返回为空。
- 如果使用的Oauth认证,图片参数pic不参与签名。
返回结果
XML示例:
<?xml version="1.0" encoding="UTF-8"?> <status> <id>8374590857</id> <text>博客已老 围脖当道 http://sinaurl.cn/hMxBc </text> <source> <a href="http://t.sina.com.cn">Web</a> </source> <favorited>false</favorited> <truncated>false</truncated> <created_at>Tue Nov 17 20:11:24 +0800 2009</created_at> <user> <id>10503</id> <screen_name>timyang</screen_name> <name>Tim Yang</name> <location>广州</location> <description>blog: timyang.net</description> <url>http://timyang.net</url> <profile_image_url>http://tt8.sinaimg.cn/10503/50/1256306049</profile_image_url> <followers_count>0</followers_count> <friends_count>0</friends_count> <statuses_count>0</statuses_count> <favourites_count>0</favourites_count> </user> </status>
JSON示例:
{"created_at":"Thu Jan 07 10:51:19 +0800 2010", "id":142296, "text":"http://sinaurl.cn/hi4sD", "source":"<a id=\"0\" href=\"http://t.sina.com.cn/\" rel=\"nofollow\">Web</a>", "favorited":false, "truncated":false, "in_reply_to_status_id":"", "in_reply_to_user_id":"", "in_reply_to_screen_name":"", "thumbnail_pic":"http://static12.photo.sina.com.cn/thumbnail/5219ba9d072da9d51f7bb", "bmiddle_pic":"http://static12.photo.sina.com.cn/bmiddle/5219ba9d072da9d51f7bb", "original_pic":"http://static12.photo.sina.com.cn/orignal/5219ba9d072da9d51f7bb", "geo":null, "user":
{"id":11075, "screen_name":"name_11075", "name":"name_11075", "province":"0", "city":"0", "location":"", "description":"", "url":"", "profile_image_url":"http://tp4.sinaimg.cn/11075/50/0", "domain":"11075", "followers_count":28, "friends_count":50, "statuses_count":0, "favourites_count":29, "created_at":"Thu Jan 01 08:00:00 +0800 1970", "following":false, "geo_enabled":false, "verified":true} }
使用示例
- xml:
curl -u uid:password -F "pic=@aaa.jpg" -F "source=appkey" -F "status=abaaaa" http://api.t.sina.com.cn/statuses/upload.xml
- json:
curl -u uid:password -F "pic=@aaa.jpg" -F "source=appkey" -F "status=abaaaa" http://api.t.sina.com.cn/statuses/upload.json
PHP示例
请从 微博SDK开发包下载 处下载PHP SDK(支持OAuth验证之版本)
代码示例如下:
<?php $boundary = uniqid('------------------'); $MPboundary = '--'.$boundary; $endMPboundary = $MPboundary. '--'; // 需要上传的图片所在路径 $filename = '/tmp/wiki.png'; $file = file_get_contents($filename); $multipartbody .= $MPboundary . "\r\n"; $multipartbody .= 'Content-Disposition: form-data; name="pic"; filename="wiki.png"'. "\r\n"; $multipartbody .= 'Content-Type: image/png'. "\r\n\r\n"; $multipartbody .= $file. "\r\n"; $k = "source"; // 这里改成 appkey $v = "appkey"; $multipartbody .= $MPboundary . "\r\n"; $multipartbody.='content-disposition: form-data; name="'.$k."\"\r\n\r\n"; $multipartbody.=$v."\r\n"; $k = "status"; $v = "要上传的文件,这里是描述文字"; $multipartbody .= $MPboundary . "\r\n"; $multipartbody.='content-disposition: form-data; name="'.$k."\"\r\n\r\n"; $multipartbody.=$v."\r\n"; $multipartbody .= "\r\n". $endMPboundary; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, 'http://api.t.sina.com.cn/statuses/upload.xml' ); curl_setopt($ch , CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS,$multipartbody ); curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: multipart/form-data; boundary=$boundary")); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // 修改成当前用户名及密码 curl_setopt($ch, CURLOPT_USERPWD, "username:password"); $msg = curl_exec($ch); //echo $multipartbody; echo 'ok.'; ?>
Java示例
请从 微博SDK开发包下载 下载Java SDK 参看 examples/OauthUpload.java, 运行 java OauthUpload accesstoken accesstoken_secret filepath 代码示意如下
public static void main(String[] args) { try { System.setProperty("twitter4j.oauth.consumerKey", Twitter.CONSUMER_KEY); System.setProperty("twitter4j.oauth.consumerSecret", Twitter.CONSUMER_SECRET); Twitter twitter = new Twitter(); // you can get access token and access token secret returned from OAuthUpdate.java twitter.setToken(args[0], args[1]); try { byte[] content = readFileImage(args[2]); System.out.println("content length:" + content.length); ImageItem pic = new ImageItem("pic", content); Status status = twitter.uploadStatus("测试文字", pic); System.out.println("Successfully upload the status to [" + status.getText() + "]."); } catch (Exception e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } catch (Exception ioe) { System.out.println("Failed to read the system input."); } } public static byte[] readFileImage(String filename) throws IOException { BufferedInputStream bufferedInputStream = new BufferedInputStream( new FileInputStream(filename)); int len = bufferedInputStream.available(); byte[] bytes = new byte[len]; int r = bufferedInputStream.read(bytes); if (len != r) { bytes = null; throw new IOException("读取文件不正确"); } bufferedInputStream.close(); return bytes; }
其他语言
可参考以下测试方式,将以下代码存成HTML,在微博登录的情况下用浏览器打开此页面可以上传图片,通过比较浏览器的调用(使用HttpWatch, FireBug等工具监测HTTP传送数据)参数来调整你的程序。注意将下面代码中appkey改成对应真实值。