3

微博是个好图床,上传后就可以通过一个url来访问了。今天就用php来上传图片到微博,这也是来自sf的一个问题, 里面还提到一个python版本.

有2种方式实现上传图片:

如果要用 http://picupload.service.weib... 这个 URL 的话POST 参数必须是 b64_data,值为经过 base64 编码后的字符串。

如果要使用 pic1 参数的话,则要用 multipart 方式进行上传,且 URL 中必须包含 cb 参数,cb 参数的值为 http://weibo.com/aj/static/up... 加(js)时间戳

以下为php实现,感谢这个问题下@consatan 的回复,这里做个总结。

使用base64上传

$cookie = 'your cookie';//登录微博network获取
$ch = curl_init('http://picupload.service.weibo.com/interface/pic_upload.php'
    . '?mime=image%2Fjpeg&data=base64&url=0&markpos=1&logo=&nick=0&marks=1&app=miniblog');

curl_setopt_array($ch, [
    CURLOPT_POST => true,
    CURLOPT_VERBOSE => true,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HTTPHEADER => ["Cookie: $cookie"],
    CURLOPT_POSTFIELDS => ['b64_data' => base64_encode(file_get_contents('./sf.jpg'))],
]);

$res = curl_exec($ch);
curl_close($ch);
print_r($res);
/*
{"code":"A00006","data":{"count":1,"data":"eyJ1aWQiOjMyNDMwMjYyMzcsImFwcCI6Im1pbmlibG9nIiwiY291bnQiOjEsInRpbWUiOjE0ODc3NDIyMDYuMzIxLCJwaWNzIjp7InBpY18xIjp7IndpZHRoIjo1MTAsInNpemUiOjUyOTMzLCJyZXQiOjEsImhlaWdodCI6MzMwLCJuYW1lIjoicGljXzEiLCJwaWQiOiJjMTRjYTczZGx5MWZjejcxejNyN25qMjBlNjA5NnE1ZiJ9fX0=","pics":{"pic_1":{"width":510,"size":52933,"ret":1,"height":330,"name":"pic_1","pid":"c14ca73dly1fcz71z3r7nj20e6096q5f"}}}}
$data=base64_decode($res['data']['data'])
{"uid":3243026237,"app":"miniblog","count":1,"time":1487742206.321,"pics":{"pic_1":{"width":510,"size":52933,"ret":1,"height":330,"name":"pic_1","pid":"c14ca73dly1fcz71z3r7nj20e6096q5f"}}}
*/
//c14ca73dly1fcz71z3r7nj20e6096q5f就是微博图片id,访问http://ww3.sinaimg.cn/large/c14ca73dly1fcz39h7mo3j20e6096q5f即可打开图片,这里我上传的是sf的广告图。

sf

multipart 方式上传

$cookie = 'your cookie';//登录微博network获取
$post_data['pic1']=new CURLFile(realpath('sf.jpg'));
$time=substr(strval(microtime(true) * 1000), 0, 13) . 1;
        $url="http://picupload.service.weibo.com/interface/pic_upload.php?mime=image%2Fjpeg&data=base64&url=0&markpos=1&logo=&nick=0&marks=1&app=miniblog&cb=http://weibo.com/aj/static/upimgback.html?_wv=5&callback=STK_ijax_".time();
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_COOKIE, $cookie); 
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//multipart 方式上传需要注意传递的参数是数组,不是字符串
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
//curl_setopt($ch, CURLOPT_HEADER, 1);
// curl_setopt($ch, CURLOPT_NOBODY, 0);
$output = curl_exec($ch);
$rinfo=curl_getinfo($ch); 
print_r($output);
curl_close($ch);
/*
{"code":"A20001","data":{"count":2,"data":"eyJ1aWQiOjMyNDMwMjYyMzcsImFwcCI6Im1pbmlibG9nIiwiY291bnQiOjIsInRpbWUiOjE0ODc3MzQzMzUuNDU3LCJwaWNzIjp7InBpY18yIjp7InJldCI6LTIsIm5hbWUiOiJwaWNfMiJ9LCJwaWNfMSI6eyJ3aWR0aCI6NTEwLCJzaXplIjo1MjkzMywicmV0IjoxLCJoZWlnaHQiOjMzMCwibmFtZSI6InBpY18xIiwicGlkIjoiYzE0Y2E3M2RseTFmY3ozOWg3bW8zajIwZTYwOTZxNWYifX19","pics":{"pic_2":{"ret":-2,"name":"pic_2"},"pic_1":{"width":510,"size":52933,"ret":1,"height":330,"name":"pic_1","pid":"c14ca73dly1fcz39h7mo3j20e6096q5f"}}}} 
*/
//从结果中获取pid即可,这里发现上传同一张图片返回的pid是相同的,看来微博有处理,也许是比较文件的md5.

这里需要注意php不同版本上传图片的处理,参考这个链接 ,传统上,PHP的cURL支持通过在数组数据中,使用“@+文件全路径”的语法附加文件,供cURL读取上传,但PHP从5.5开始引入了新的CURLFile类用来指向文件,所以做个处理:

if (class_exists('\CURLFile')) {
    $field = array('fieldname' => new \CURLFile(realpath($filepath)));
} else {
    $field = array('fieldname' => '@' . realpath($filepath));
}

苏生不惑
18.9k 声望1.9k 粉丝

« 上一篇
jquery记录