Share Sina Tubed upload interface source code

藏色散人
Release: 2023-04-07 22:12:01
forward
3673 people have browsed it

After deploying the source code, change the account password to your own Sina account

<?php
/**
 * 上传图片到微博图床
 * @author Youngxj & mengkun & 阿珏
 * @param $file 图片文件/图片url
 * @param $multipart 是否采用multipart方式上传
 * @return 返回的json数据
 * @code  200:正常;201:错误;203:cookie获取失败;404:请勿直接访问
 * @ps    图片尺寸可供选择:square、thumb150、orj360、orj480、mw690、mw1024、mw2048、small、bmiddle、large 默认为:thumb150,请自行替换
 */
header("Access-Control-Allow-Origin:*");
header(&#39;Content-type: application/json&#39;);
error_reporting(0);
if (!is_file(&#39;sina_config.php&#39;)) {
  CookieSet(&#39;SUB;&#39;,&#39;0&#39;);
}
include &#39;sina_config.php&#39;;
//账号
$sinauser = &#39;admin&#39;;
//密码
$sinapwd = &#39;password&#39;;
if (time() - $config[&#39;time&#39;] >20*3600||$config[&#39;cookie&#39;]==&#39;SUB;&#39;) {
  $cookie = login($sinauser,$sinapwd);
  if($cookie&&$cookie!=&#39;SUB;&#39;){
    CookieSet($cookie,$time = time());
  }else{
    return error(&#39;203&#39;,&#39;获取cookie出现错误,请检查账号状态或者重新获取cookie&#39;);
  }
}
if ($_SERVER[&#39;REQUEST_METHOD&#39;] == &#39;OPTIONS&#39;) {exit;}
$type=$_GET[&#39;type&#39;];
if($type==&#39;multipart&#39;){
  $multipart = true;
  $file = $_FILES["file"]["tmp_name"];
}elseif(isset($_GET[&#39;img&#39;])){
  $multipart = false;
  $file = $_GET[&#39;img&#39;];
}else{
  return error(&#39;404&#39;,&#39;请勿直接访问&#39;);
}
if (isset($file) && $file != "") {
  include &#39;sina_config.php&#39;;
  $cookie = $config[&#39;cookie&#39;];
  echo upload($file, $multipart,$cookie);
}else{
  return error(&#39;201&#39;,&#39;上传错误&#39;);
}

function CookieSet($cookie,$time){
  $newConfig = &#39;<?php 
  $config = array(
    "cookie" => "&#39;.$cookie.&#39;",
    "time" => "&#39;.$time.&#39;",
  );&#39;;
  @file_put_contents(&#39;sina_config.php&#39;, $newConfig);
}

function error($code,$msg){
  $arr = array(&#39;code&#39;=>$code,&#39;msg&#39;=>$msg);
  echo json_encode($arr);
}
/**
     * 新浪微博登录(无加密接口版本)
     * @param  string $u 用户名
     * @param  string $p 密码
     * @return string    返回最有用最精简的cookie
     */
function login($u,$p){
  $loginUrl = &#39;https://login.sina.com.cn/sso/login.php?client=ssologin.js(v1.4.15)&_=1403138799543&#39;;
  $loginData[&#39;entry&#39;] = &#39;sso&#39;;
  $loginData[&#39;gateway&#39;] = &#39;1&#39;;
  $loginData[&#39;from&#39;] = &#39;null&#39;;
  $loginData[&#39;savestate&#39;] = &#39;30&#39;;
  $loginData[&#39;useticket&#39;] = &#39;0&#39;;
  $loginData[&#39;pagerefer&#39;] = &#39;&#39;;
  $loginData[&#39;vsnf&#39;] = &#39;1&#39;;
  $loginData[&#39;su&#39;] = base64_encode($u);
  $loginData[&#39;service&#39;] = &#39;sso&#39;;
  $loginData[&#39;sp&#39;] = $p;
  $loginData[&#39;sr&#39;] = &#39;1920*1080&#39;;
  $loginData[&#39;encoding&#39;] = &#39;UTF-8&#39;;
  $loginData[&#39;cdult&#39;] = &#39;3&#39;;
  $loginData[&#39;domain&#39;] = &#39;sina.com.cn&#39;;
  $loginData[&#39;prelt&#39;] = &#39;0&#39;;
  $loginData[&#39;returntype&#39;] = &#39;TEXT&#39;;
  return loginPost($loginUrl,$loginData); 
}

/**
     * 发送微博登录请求
     * @param  string $url  接口地址
     * @param  array  $data 数据
     * @return json         算了,还是返回cookie吧//返回登录成功后的用户信息json
     */
function loginPost($url,$data){
  $tmp = &#39;&#39;;
  if(is_array($data)){
    foreach($data as $key =>$value){
      $tmp .= $key."=".$value."&";
    }
    $post = trim($tmp,"&");
  }else{
    $post = $data;
  }
  $ch = curl_init();
  curl_setopt($ch,CURLOPT_URL,$url); 
  curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); 
  curl_setopt($ch,CURLOPT_HEADER,1);
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  curl_setopt($ch,CURLOPT_POST,1);
  curl_setopt($ch,CURLOPT_POSTFIELDS,$post);
  $return = curl_exec($ch);
  curl_close($ch);
  return &#39;SUB&#39; . getSubstr($return,"Set-Cookie: SUB",&#39;; &#39;) . &#39;;&#39;;
}

/**
 * 取本文中间
 */
function getSubstr($str,$leftStr,$rightStr){
  $left = strpos($str, $leftStr);
  //echo &#39;左边:&#39;.$left;
  $right = strpos($str, $rightStr,$left);
  //echo &#39;<br>右边:&#39;.$right;
  if($left <= 0 or $right < $left) return &#39;&#39;;
  return substr($str, $left + strlen($leftStr), $right-$left-strlen($leftStr));
}


function upload($file, $multipart = true,$cookie) {
  $url = &#39;http://picupload.service.weibo.com/interface/pic_upload.php&#39;.&#39;?mime=image%2Fjpeg&data=base64&url=0&markpos=1&logo=&nick=0&marks=1&app=miniblog&#39;;
  if($multipart) {
    $url .= &#39;&cb=http://weibo.com/aj/static/upimgback.html?_wv=5&callback=STK_ijax_&#39;.time();
    if (class_exists(&#39;CURLFile&#39;)) {     // php 5.5
      $post[&#39;pic1&#39;] = new \CURLFile(realpath($file));
    } else {
      $post[&#39;pic1&#39;] = &#39;@&#39;.realpath($file);
    }
  } else {
    $post[&#39;b64_data&#39;] = base64_encode(file_get_contents($file));
  }
  // Curl提交
  $ch = curl_init($url);
  curl_setopt_array($ch, array(
    CURLOPT_POST => true,
    CURLOPT_VERBOSE => true,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HTTPHEADER => array("Cookie: $cookie"),
    CURLOPT_POSTFIELDS => $post,
  ));
  $output = curl_exec($ch);
  curl_close($ch);
  // 正则表达式提取返回结果中的json数据
  preg_match(&#39;/({.*)/i&#39;, $output, $match);
  if(!isset($match[1])) return error(&#39;201&#39;,&#39;上传错误&#39;);
  $a=json_decode($match[1],true);
  $width = $a[&#39;data&#39;][&#39;pics&#39;][&#39;pic_1&#39;][&#39;width&#39;];
  $size = $a[&#39;data&#39;][&#39;pics&#39;][&#39;pic_1&#39;][&#39;size&#39;];
  $height = $a[&#39;data&#39;][&#39;pics&#39;][&#39;pic_1&#39;][&#39;height&#39;];
  $pid = $a[&#39;data&#39;][&#39;pics&#39;][&#39;pic_1&#39;][&#39;pid&#39;];
  if(!$pid){return error(&#39;201&#39;,&#39;上传错误&#39;);}
  $arr = array(&#39;code&#39;=>&#39;200&#39;,&#39;width&#39;=>$width,"height"=>$height,"size"=>$size,"pid"=>$pid,"url"=>"http://ws3.sinaimg.cn/thumb150/".$pid.".jpg");
  return json_encode($arr);
}
Copy after login

Related recommendations: "PHP Tutorial"

The above is the detailed content of Share Sina Tubed upload interface source code. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:cnblogs.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template