找回密码
 注册帐号
查看: 4493|回复: 0

新浪图床上传接口源码分享

[复制链接]

1400

主题

46

回帖

6880

积分

版主

发表于 2018-8-6 18:28:02 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?注册帐号

×
部署源码之后自行修改账号密码为自己的新浪账号
代码:
  1. <?php
  2. /**
  3. * 上传图片到微博图床
  4. * @author Youngxj & mengkun & 阿珏
  5. * @param $file 图片文件/图片url
  6. * @param $multipart 是否采用multipart方式上传
  7. * @return 返回的json数据
  8. * @code  200:正常;201:错误;203:cookie获取失败;404:请勿直接访问
  9. * @ps    图片尺寸可供选择:square、thumb150、orj360、orj480、mw690、mw1024、mw2048、small、bmiddle、large 默认为:thumb150,请自行替换
  10. */
  11. header("Access-Control-Allow-Origin:*");
  12. header('Content-type: application/json');
  13. error_reporting(0);
  14. if (!is_file('sina_config.php')) {
  15.   CookieSet('SUB;','0');
  16. }
  17. include 'sina_config.php';
  18. //账号
  19. $sinauser = 'admin';
  20. //密码
  21. $sinapwd = 'password';
  22. if (time() - $config['time'] >20*3600||$config['cookie']=='SUB;') {
  23.   $cookie = login($sinauser,$sinapwd);
  24.   if($cookie&&$cookie!='SUB;'){
  25.     CookieSet($cookie,$time = time());
  26.   }else{
  27.     return error('203','获取cookie出现错误,请检查账号状态或者重新获取cookie');
  28.   }
  29. }
  30. if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {exit;}
  31. $type=$_GET['type'];
  32. if($type=='multipart'){
  33.   $multipart = true;
  34.   $file = $_FILES["file"]["tmp_name"];
  35. }elseif(isset($_GET['img'])){
  36.   $multipart = false;
  37.   $file = $_GET['img'];
  38. }else{
  39.   return error('404','请勿直接访问');
  40. }
  41. if (isset($file) && $file != "") {
  42.   include 'sina_config.php';
  43.   $cookie = $config['cookie'];
  44.   echo upload($file, $multipart,$cookie);
  45. }else{
  46.   return error('201','上传错误');
  47. }

  48. function CookieSet($cookie,$time){
  49.   $newConfig = '<?php
  50.   $config = array(
  51.     "cookie" => "'.$cookie.'",
  52.     "time" => "'.$time.'",
  53.   );';
  54.   @file_put_contents('sina_config.php', $newConfig);
  55. }

  56. function error($code,$msg){
  57.   $arr = array('code'=>$code,'msg'=>$msg);
  58.   echo json_encode($arr);
  59. }
  60. /**
  61.      * 新浪微博登录(无加密接口版本)
  62.      * @param  string $u 用户名
  63.      * @param  string $p 密码
  64.      * @return string    返回最有用最精简的cookie
  65.      */
  66. function login($u,$p){
  67.   $loginUrl = 'https://login.sina.com.cn/sso/login.php?client=ssologin.js(v1.4.15)&_=1403138799543';
  68.   $loginData['entry'] = 'sso';
  69.   $loginData['gateway'] = '1';
  70.   $loginData['from'] = 'null';
  71.   $loginData['savestate'] = '30';
  72.   $loginData['useticket'] = '0';
  73.   $loginData['pagerefer'] = '';
  74.   $loginData['vsnf'] = '1';
  75.   $loginData['su'] = base64_encode($u);
  76.   $loginData['service'] = 'sso';
  77.   $loginData['sp'] = $p;
  78.   $loginData['sr'] = '1920*1080';
  79.   $loginData['encoding'] = 'UTF-8';
  80.   $loginData['cdult'] = '3';
  81.   $loginData['domain'] = 'sina.com.cn';
  82.   $loginData['prelt'] = '0';
  83.   $loginData['returntype'] = 'TEXT';
  84.   return loginPost($loginUrl,$loginData);
  85. }

  86. /**
  87.      * 发送微博登录请求
  88.      * @param  string $url  接口地址
  89.      * @param  array  $data 数据
  90.      * @return json         算了,还是返回cookie吧//返回登录成功后的用户信息json
  91.      */
  92. function loginPost($url,$data){
  93.   $tmp = '';
  94.   if(is_array($data)){
  95.     foreach($data as $key =>$value){
  96.       $tmp .= $key."=".$value."&";
  97.     }
  98.     $post = trim($tmp,"&");
  99.   }else{
  100.     $post = $data;
  101.   }
  102.   $ch = curl_init();
  103.   curl_setopt($ch,CURLOPT_URL,$url);
  104.   curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
  105.   curl_setopt($ch,CURLOPT_HEADER,1);
  106.   curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  107.   curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  108.   curl_setopt($ch,CURLOPT_POST,1);
  109.   curl_setopt($ch,CURLOPT_POSTFIELDS,$post);
  110.   $return = curl_exec($ch);
  111.   curl_close($ch);
  112.   return 'SUB' . getSubstr($return,"Set-Cookie: SUB",'; ') . ';';
  113. }

  114. /**
  115. * 取本文中间
  116. */
  117. function getSubstr($str,$leftStr,$rightStr){
  118.   $left = strpos($str, $leftStr);
  119.   //echo '左边:'.$left;
  120.   $right = strpos($str, $rightStr,$left);
  121.   //echo '<br>右边:'.$right;
  122.   if($left <= 0 or $right < $left) return '';
  123.   return substr($str, $left + strlen($leftStr), $right-$left-strlen($leftStr));
  124. }


  125. function upload($file, $multipart = true,$cookie) {
  126.   $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';
  127.   if($multipart) {
  128.     $url .= '&cb=http://weibo.com/aj/static/upimgback.html?_wv=5&callback=STK_ijax_'.time();
  129.     if (class_exists('CURLFile')) {     // php 5.5
  130.       $post['pic1'] = new \CURLFile(realpath($file));
  131.     } else {
  132.       $post['pic1'] = '@'.realpath($file);
  133.     }
  134.   } else {
  135.     $post['b64_data'] = base64_encode(file_get_contents($file));
  136.   }
  137.   // Curl提交
  138.   $ch = curl_init($url);
  139.   curl_setopt_array($ch, array(
  140.     CURLOPT_POST => true,
  141.     CURLOPT_VERBOSE => true,
  142.     CURLOPT_RETURNTRANSFER => true,
  143.     CURLOPT_HTTPHEADER => array("Cookie: $cookie"),
  144.     CURLOPT_POSTFIELDS => $post,
  145.   ));
  146.   $output = curl_exec($ch);
  147.   curl_close($ch);
  148.   // 正则表达式提取返回结果中的json数据
  149.   preg_match('/({.*)/i', $output, $match);
  150.   if(!isset($match[1])) return error('201','上传错误');
  151.   $a=json_decode($match[1],true);
  152.   $width = $a['data']['pics']['pic_1']['width'];
  153.   $size = $a['data']['pics']['pic_1']['size'];
  154.   $height = $a['data']['pics']['pic_1']['height'];
  155.   $pid = $a['data']['pics']['pic_1']['pid'];
  156.   if(!$pid){return error('201','上传错误');}
  157.   $arr = array('code'=>'200','width'=>$width,"height"=>$height,"size"=>$size,"pid"=>$pid,"url"=>"http://ws3.sinaimg.cn/thumb150/".$pid.".jpg");
  158.   return json_encode($arr);
  159. }
复制代码


哎...今天够累的,签到来了1...
您需要登录后才可以回帖 登录 | 注册帐号

本版积分规则

Archiver|sitemap|小黑屋|手机版原版|安全之家

GMT+8, 2025-4-4 18:48 , Processed in 0.041705 second(s), 8 queries , Gzip On, Redis On.

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表