hi,你好!欢迎访问本站!登录
本站由简数采集腾讯云宝塔系统阿里云强势驱动
当前位置:首页 - 文章 - 后端开发 - 正文 看Cosplay古风插画小姐姐,合集图集打包下载:炫龙网 · 炫龙图库

分享新浪图床上传接口源码_后端开发

2019-12-05后端开发ki4网17°c
A+ A-
布置源码以后自行修正账号暗码为本身的新浪账号

<?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('Content-type: application/json');
error_reporting(0);
if (!is_file('sina_config.php')) {
  CookieSet('SUB;','0');
}
include 'sina_config.php';
//账号
$sinauser = 'admin';
//暗码
$sinapwd = 'password';
if (time() - $config['time'] >20*3600||$config['cookie']=='SUB;') {
  $cookie = login($sinauser,$sinapwd);
  if($cookie&&$cookie!='SUB;'){
    CookieSet($cookie,$time = time());
  }else{
    return error('203','猎取cookie涌现毛病,请搜检账号状况或许从新猎取cookie');
  }
}
if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {exit;}
$type=$_GET['type'];
if($type=='multipart'){
  $multipart = true;
  $file = $_FILES["file"]["tmp_name"];
}elseif(isset($_GET['img'])){
  $multipart = false;
  $file = $_GET['img'];
}else{
  return error('404','请勿直接接见');
}
if (isset($file) && $file != "") {
  include 'sina_config.php';
  $cookie = $config['cookie'];
  echo upload($file, $multipart,$cookie);
}else{
  return error('201','上传毛病');
}

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

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

/**
     * 发送微博登录要求
     * @param  string $url  接口地点
     * @param  array  $data 数据
     * @return json         算了,照样返回cookie吧//返回登录胜利后的用户信息json
     */
function loginPost($url,$data){
  $tmp = '';
  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 'SUB' . getSubstr($return,"Set-Cookie: SUB",'; ') . ';';
}

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


function upload($file, $multipart = true,$cookie) {
  $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';
  if($multipart) {
    $url .= '&cb=http://weibo.com/aj/static/upimgback.html?_wv=5&callback=STK_ijax_'.time();
    if (class_exists('CURLFile')) {     // php 5.5
      $post['pic1'] = new \CURLFile(realpath($file));
    } else {
      $post['pic1'] = '@'.realpath($file);
    }
  } else {
    $post['b64_data'] = 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('/({.*)/i', $output, $match);
  if(!isset($match[1])) return error('201','上传毛病');
  $a=json_decode($match[1],true);
  $width = $a['data']['pics']['pic_1']['width'];
  $size = $a['data']['pics']['pic_1']['size'];
  $height = $a['data']['pics']['pic_1']['height'];
  $pid = $a['data']['pics']['pic_1']['pid'];
  if(!$pid){return error('201','上传毛病');}
  $arr = array('code'=>'200','width'=>$width,"height"=>$height,"size"=>$size,"pid"=>$pid,"url"=>"http://ws3.sinaimg.cn/thumb150/".$pid.".jpg");
  return json_encode($arr);
}

相干引荐:《PHP教程》

以上就是分享新浪图床上传接口源码的细致内容,更多请关注ki4网别的相干文章!

  选择打赏方式
微信赞助

打赏

QQ钱包

打赏

支付宝赞助

打赏

  选择分享方式
  移步手机端
分享新浪图床上传接口源码_后端开发

1、打开你手机的二维码扫描APP
2、扫描左则的二维码
3、点击扫描获得的网址
4、可以在手机端阅读此文章
标签:

发表评论

选填

必填

必填

选填

请拖动滑块解锁
>>