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('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); }
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!

PHP remains important in modern web development, especially in content management and e-commerce platforms. 1) PHP has a rich ecosystem and strong framework support, such as Laravel and Symfony. 2) Performance optimization can be achieved through OPcache and Nginx. 3) PHP8.0 introduces JIT compiler to improve performance. 4) Cloud-native applications are deployed through Docker and Kubernetes to improve flexibility and scalability.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

The reasons why PHP is the preferred technology stack for many websites include its ease of use, strong community support, and widespread use. 1) Easy to learn and use, suitable for beginners. 2) Have a huge developer community and rich resources. 3) Widely used in WordPress, Drupal and other platforms. 4) Integrate tightly with web servers to simplify development deployment.

PHP remains a powerful and widely used tool in modern programming, especially in the field of web development. 1) PHP is easy to use and seamlessly integrated with databases, and is the first choice for many developers. 2) It supports dynamic content generation and object-oriented programming, suitable for quickly creating and maintaining websites. 3) PHP's performance can be improved by caching and optimizing database queries, and its extensive community and rich ecosystem make it still important in today's technology stack.

In PHP, weak references are implemented through the WeakReference class and will not prevent the garbage collector from reclaiming objects. Weak references are suitable for scenarios such as caching systems and event listeners. It should be noted that it cannot guarantee the survival of objects and that garbage collection may be delayed.

The \_\_invoke method allows objects to be called like functions. 1. Define the \_\_invoke method so that the object can be called. 2. When using the $obj(...) syntax, PHP will execute the \_\_invoke method. 3. Suitable for scenarios such as logging and calculator, improving code flexibility and readability.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Atom editor mac version download
The most popular open source editor

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SublimeText3 Chinese version
Chinese version, very easy to use

WebStorm Mac version
Useful JavaScript development tools

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft