发现一个微博图床API和图片上传代码
Posted 网站前端vw
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了发现一个微博图床API和图片上传代码相关的知识,希望对你有一定的参考价值。
网上寻到一款微博图床的插件,然后顺藤摸瓜找到了原作者的API代码。
API文件:
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 49 function CookieSet($cookie,$time){ 50 $newConfig = \'<?php 51 $config = array( 52 "cookie" => "\'.$cookie.\'", 53 "time" => "\'.$time.\'", 54 );\'; 55 @file_put_contents(\'sina_config.php\', $newConfig); 56 } 57 58 function error($code,$msg){ 59 $arr = array(\'code\'=>$code,\'msg\'=>$msg); 60 echo json_encode($arr); 61 } 62 /** 63 * 新浪微博登录(无加密接口版本) 64 * @param string $u 用户名 65 * @param string $p 密码 66 * @return string 返回最有用最精简的cookie 67 */ 68 function login($u,$p){ 69 $loginUrl = \'https://login.sina.com.cn/sso/login.php?client=ssologin.js(v1.4.15)&_=1403138799543\'; 70 $loginData[\'entry\'] = \'sso\'; 71 $loginData[\'gateway\'] = \'1\'; 72 $loginData[\'from\'] = \'null\'; 73 $loginData[\'savestate\'] = \'30\'; 74 $loginData[\'useticket\'] = \'0\'; 75 $loginData[\'pagerefer\'] = \'\'; 76 $loginData[\'vsnf\'] = \'1\'; 77 $loginData[\'su\'] = base64_encode($u); 78 $loginData[\'service\'] = \'sso\'; 79 $loginData[\'sp\'] = $p; 80 $loginData[\'sr\'] = \'1920*1080\'; 81 $loginData[\'encoding\'] = \'UTF-8\'; 82 $loginData[\'cdult\'] = \'3\'; 83 $loginData[\'domain\'] = \'sina.com.cn\'; 84 $loginData[\'prelt\'] = \'0\'; 85 $loginData[\'returntype\'] = \'TEXT\'; 86 return loginPost($loginUrl,$loginData); 87 } 88 89 /** 90 * 发送微博登录请求 91 * @param string $url 接口地址 92 * @param array $data 数据 93 * @return json 算了,还是返回cookie吧//返回登录成功后的用户信息json 94 */ 95 function loginPost($url,$data){ 96 $tmp = \'\'; 97 if(is_array($data)){ 98 foreach($data as $key =>$value){ 99 $tmp .= $key."=".$value."&"; 100 } 101 $post = trim($tmp,"&"); 102 }else{ 103 $post = $data; 104 } 105 $ch = curl_init(); 106 curl_setopt($ch,CURLOPT_URL,$url); 107 curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); 108 curl_setopt($ch,CURLOPT_HEADER,1); 109 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 110 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); 111 curl_setopt($ch,CURLOPT_POST,1); 112 curl_setopt($ch,CURLOPT_POSTFIELDS,$post); 113 $return = curl_exec($ch); 114 curl_close($ch); 115 return \'SUB\' . getSubstr($return,"Set-Cookie: SUB",\'; \') . \';\'; 116 } 117 118 /** 119 * 取本文中间 120 */ 121 function getSubstr($str,$leftStr,$rightStr){ 122 $left = strpos($str, $leftStr); 123 //echo \'左边:\'.$left; 124 $right = strpos($str, $rightStr,$left); 125 //echo \'<br>右边:\'.$right; 126 if($left <= 0 or $right < $left) return \'\'; 127 return substr($str, $left + strlen($leftStr), $right-$left-strlen($leftStr)); 128 } 129 130 131 function upload($file, $multipart = true,$cookie) { 132 $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\'; 133 if($multipart) { 134 $url .= \'&cb=http://weibo.com/aj/static/upimgback.html?_wv=5&callback=STK_ijax_\'.time(); 135 if (class_exists(\'CURLFile\')) { // php 5.5 136 $post[\'pic1\'] = new \\CURLFile(realpath($file)); 137 } else { 138 $post[\'pic1\'] = \'@\'.realpath($file); 139 } 140 } else { 141 $post[\'b64_data\'] = base64_encode(file_get_contents($file)); 142 } 143 // Curl提交 144 $ch = curl_init($url); 145 curl_setopt_array($ch, array( 146 CURLOPT_POST => true, 147 CURLOPT_VERBOSE => true, 148 CURLOPT_RETURNTRANSFER => true, 149 CURLOPT_HTTPHEADER => array("Cookie: $cookie"), 150 CURLOPT_POSTFIELDS => $post, 151 )); 152 $output = curl_exec($ch); 153 curl_close($ch); 154 // 正则表达式提取返回结果中的json数据 155 preg_match(\'/({.*)/i\', $output, $match); 156 if(!isset($match[1])) return error(\'201\',\'上传错误\'); 157 $a=json_decode($match[1],true); 158 $width = $a[\'data\'][\'pics\'][\'pic_1\'][\'width\']; 159 $size = $a[\'data\'][\'pics\'][\'pic_1\'][\'size\']; 160 $height = $a[\'data\'][\'pics\'][\'pic_1\'][\'height\']; 161 $pid = $a[\'data\'][\'pics\'][\'pic_1\'][\'pid\']; 162 if(!$pid){return error(\'201\',\'上传错误\');} 163 $arr = array(\'code\'=>\'200\',\'width\'=>$width,"height"=>$height,"size"=>$size,"pid"=>$pid,"url"=>"http://ws3.sinaimg.cn/thumb150/".$pid.".jpg"); 164 return json_encode($arr); 165 }
js代码:
/** * @act 新浪图床上传 * @version 1.0 * @author youngxj * @date <?php echo date("Y-m-d");?> * @url http://www.youngxj.cn */ $(document).ready(function() { $("input[type=\'file\']").change(function(e) { images_upload(this.files) }); var obj = $(\'body\'); obj.on(\'dragenter\', function(e) { e.stopPropagation(); e.preventDefault() }); obj.on(\'dragover\', function(e) { e.stopPropagation(); e.preventDefault() }); obj.on(\'drop\', function(e) { e.preventDefault(); images_upload(e.originalEvent.dataTransfer.files) }) });
//远程上传 var url_upload = function() { var urls = $("#urls").val(); var url_arr = urls.split("\\n"); if (urls == "" || url_arr.length == 0) { layer.alert("请贴入需要上传的网络图片地址."); return; } $(\'#url_upload_model\').modal(\'hide\'); $(\'.mselector > button\')[1].innerHTML = \'上传中\'; for (var i = 0; i < url_arr.length; i++) { $.ajax({ url: \'https://api.yum6.cn/sinaimg.php\', type: \'GET\', data: { img: url_arr[i] }, cache: false, dataType: \'json\', success: function(data) { layer.msg(\'上传中\'); if (typeof data.pid != \'undefined\') { $(\'#url-res-txt\').append(\'https://ws3.sinaimg.cn/large/\'+data.pid + \'.jpg\\n\'); $(\'.mselector > button\')[1].innerHTML = \'成功 \' + (i + 1) + \'/\' + url_arr.length; var apc = "<img src=\'" + data.url + "\' alt=\'" + data.url + "\'><p><a id=\'copy\' data-clipboard-target=\'#copy\' href=\'#\' title=\'点击复制链接\'>https://ws3.sinaimg.cn/large/" + data.pid + ".jpg</a></p><br>"; $(\'.preview\').css(\'display\', \'block\'); $(".preview>hr").after(apc) } else { $(\'.mselector > button\')[1].innerHTML = \'第\' + (i + 1) + \'张上传失败\' } if (typeof data.pid != \'undefined\') { $(\'.mselector > button\')[1].innerHTML = \'上传成功\' } else { $(\'.mselector > button\')[1].innerHTML = \'上传失败\'; $(\'#url-res-txt\').append(data.code + \'\\n\'); layer.alert(data.code) } }, error: function(XMLResponse) { layer.alert("error:" + XMLResponse.responseText) } }) } };
//本地上传 var images_upload = function(files) { var flag = 0; $(\'textarea\').empty(); $(files).each(function(key, value) { $(\'.mselector > button\')[0].innerHTML = \'上传中\'; image_form = new FormData(); image_form.append(\'file\', value); $.ajax({ url: \'https://api.yum6.cn/sinaimg.php?type=multipart\', //此处填写API地址,这个地址是原作者提供调用,可以直接使用无问题 type: \'POST\', data: image_form, mimeType: \'multipart/form-data\', contentType: false, cache: false, processData: false, dataType: \'json\', success: function(data) { flag++; if (typeof data.url != \'undefined\') { $(\'#url-res-txt\').append(\'https://ws3.sinaimg.cn/large/\'+data.pid + \'.jpg\\n\'); $(\'.mselector > button\')[0].innerHTML = \'成功 \' + flag + \'/\' + files.length; var apc = "<img src=\'" + data.url + "\' alt=\'" + data.url + "\'><p><a id=\'copy"+ flag +"\' data-clipboard-target=\'#copy"+ flag +"\' href=\'#\' title=\'点击复制链接\' >https://ws3.sinaimg.cn/large/" + data.pid + ".jpg</a></p><br>"; $(\'.preview\').css(\'display\', \'block\'); $(".preview>hr").after(apc) } else { $(\'.mselector > button\')[0].innerHTML = \'第\' + flag + \'张上传失败\' } if (flag == $("input[type=\'file\']")[0].files.length) { if (typeof data.url != \'undefined\') { $(\'.mselector > button\')[0].innerHTML = \'上传成功\' } else { $(\'.mselector > button\')[0].innerHTML = \'上传失败\'; $(\'#url-res-txt\').append(data.code + \'\\n\'); layer.alert(data.code) } } }, error: function(XMLResponse) { layer.alert("error:" + XMLResponse.responseText) } }) }) }; document.onpaste = function(e) { var data = e.clipboardData; for (var i = 0; i < data.items.length; i++) { var item = data.items[i]; if (item.kind == \'file\' && item.type.match(/^image\\//i)) { var blob = item.getAsFile(); images_upload(blob) } } } //复制 var clipboard = new ClipboardJS(\'a\'); clipboard.on(\'success\',function(e){ e.clearSelection(); layer.msg(\'复制成功!\'); }); clipboard.on(\'error\',function(e){ e.clearSelection(); layer.msg(\'复制失败!\'); });
html代码:
1 <div class="container clearfix"> 2 <div class="panel panel-default"> 3 <div class="panel-heading">新浪图床外链<small class="text-capitalize">-Sina Img</small></div> 4 <div class="panel-body"> 5 <div class="text-center"> 6 <div class="mselector"> 7 <input type="file" accept="image/*" multiple=""> 8 <button type="button" class="btn btn-primary">选择本地图片</button> 9 <button type="button" class="btn btn-success" data-toggle="modal" data-target="#url_upload_model">上传远程图片</button> 10 </div> 11 <hr/> 12 13 <textarea id="url-res-txt" class="form-control" rows="5" placeholder="上传后的图片外链地址将显示在此处哦、下方会同时显示外链地址和预览图。" ></textarea> 14 <div class="preview"> 15 <hr/> 16 </div> 17 <div id="url_upload_model" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"> 18 <div class="modal-dialog"> 19 <div class="modal-content"> 20 <div class="modal-header"> 21 <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button> 22 <h4 class="modal-title" id="myModalLabel">上传远程图片</h4> 23 </div> 24 <div class="modal-body"> 25 <p class="lead">请在下方输入远程图片地址~每行一个~</p> 26 <textarea class="form-control" name="urls" rows="3" id="urls"></textarea> 27 <p id="urlUploadNotice"></p> 28 </div> 29 <div class="modal-footer"> 30 <button type="button" class="btn btn-default" data-dismiss="modal">关闭</button> 31 <button type="button" class="btn btn-primary" onclick="url_upload();">上传</button> 32 </div> 33 </div> 34 </div> 35 </div> 36 </div> 37 </div> 38 </div> 39 <script type="text/javascript" src="sinaimg.js"></script>
另外还有一个html网页,现成的,复制到本地打开就行:
1 <!DOCTYPE html> 2 <html lang=\'zh-cn\'> 3 <head> 4 <meta charset=\'UTF-8\'> 5 <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=0, minimum-scale=1.0, maximum-scale=1.0"> 6 <title>免费多图床</title> 7 8 <link rel=\'stylesheet\' href=\'https://cdn.bootcss.com/normalize/5.0.0/normalize.min.css\'> 9 <script src="https://cdn.bootcss.com/jquery/3.3.0/jquery.min.js"></script> 10 <style> 11 html,body {由微博图床挂掉之后想到的