Loading... <div class="tip share">请注意,本文编写于 335 天前,最后修改于 27 天前,其中某些信息可能已经过时。</div> ## 写在前面 很多人因为服务器空间少、带宽小等原因会选择把图片放在别的网站上然后插入自己的网站。这在节省带宽的同时也带来了一些问题,像是那些网站本身不稳定的话就会造成图片无法加载之类的,,,, 我在网上找了很多搜狗图床接口的源码**也**都已经不能用了,这里找到了[教书先生](https://blog.oioweb.cn/index.php/archives/1226.html "教书先生")的一个源码,修复了无法上传后缀名为大写的图片的问题。 **重要提示:有效期只有一两天,只适合给别人传个图片!!** ## 代码 ```php <?php header('Access-Control-Allow-Origin:*'); header('Content-type:application/json; charset=utf-8'); error_reporting(0); $allowedExts = array("gif", "jpeg", "jpg", "png", "GIF", "JPEG", "JPG", "PNG"); $temp = explode(".", $_FILES["file"]["name"]); $extension = end($temp); //文件格式及大小做一下限制限制 if ((($_FILES["file"]["type"] == "image/gif") || ($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/jpg") || ($_FILES["file"]["type"] == "image/pjpeg") || ($_FILES["file"]["type"] == "image/x-png") || ($_FILES["file"]["type"] == "image/png")) && ($_FILES["file"]["size"] < 10*1024*1024) && in_array($extension, $allowedExts)) { $ImageCachePath='images/sougou/';//临时缓存路径 //判断目录存在否,存在给出提示,不存在则创建目录 if (!is_dir($ImageCachePath)){ $res = mkdir($ImageCachePath, 0777, true); } //因为不支持直接使用临时文件上传 所以要先保存一下 move_uploaded_file($_FILES["file"]["tmp_name"], $ImageCachePath . $_FILES["file"]["name"]); //文件储存位置 $files = $ImageCachePath . $_FILES["file"]["name"]; $post_data = [ "pic_path"=>new CURLFile(realpath($files)) ]; $str=urldecode(Curl_POST("http://pic.sogou.com/ris_upload",$post_data)); unlink($files); //使用完销毁一下文件 $imgurl = str_replace("http","https",GetBetween($str,".com/ris?query=","&oname=")); if ($imgurl==1 || $imgurl==""){ exit(json_encode([ "code"=>-1, "msg"=>"上传错误" ],JSON_UNESCAPED_UNICODE)); }else{ exit(json_encode([ "code"=>1, "imgurl"=>$imgurl ],JSON_UNESCAPED_UNICODE)); } }else { error("非法的文件格式"); } function GetBetween($content, $start, $end) { $r = explode($start, $content); if (isset($r[1])) { $r = explode($end, $r[1]); return $r[0]; } return ''; } function randIp() { return mt_rand(0, 255) . '.' . mt_rand(0, 255) . '.' . mt_rand(0, 255) . '.' . mt_rand(0, 255); } function Curl_POST($url,$post_data){ $header=[ 'X-FORWARDED-FOR:'.randIp(), 'CLIENT-IP:'.randIp() ]; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_NOBODY, false); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, false); curl_setopt($ch, CURLOPT_TIMEOUT, 30); curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt($ch, CURLOPT_AUTOREFERER, true); curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36"); // 伪造ua curl_setopt($ch, CURLOPT_HTTPHEADER,$header); curl_setopt($ch, CURLOPT_ENCODING, ''); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS,$post_data); curl_exec($ch); $data = curl_getinfo($ch,CURLINFO_EFFECTIVE_URL); return $data; } ``` ## 写在后面 #### 搜狗图床的缺点 **重要提示:有效期只有一两天,只适合给别人传个图片!!** 图片链接没有后缀名????可能问题不大吧,搜狗图床总体挺好的。 #### 使用问题 关于如何使用我将会在以后的文章中更新 会包括如何调用接口,如何解决防盗链等 #### 搭建接口教程 [点这里~~](https://www.wangkai88.com/archives/74/ "点这里~~") #### 即将更新阿里图床,阿里图床没有防盗链!!! 最后修改:2021 年 02 月 09 日 11 : 27 AM © 允许规范转载 赞赏 如果觉得我的文章对你有用,请随意赞赏 ×Close 赞赏作者 扫一扫支付 支付宝支付 微信支付