葫芦侠、百度图床、京东图床接口源码案例分享
Sonder
2020-09-16
8343字
21分钟
浏览 (4.2k)
我感觉不要太依赖图床,毕竟不是自己的,说不定什么时候就不能用了,我图片都是本地的,直接上代码,我写好以后是在本地测试的,不知道支持不支持临时文件上传,不支持的话你们改一下就行了
1.葫芦侠图床
<?php
header('Access-Control-Allow-Origin:*');
header('Content-type:application/json; charset=utf-8');
error_reporting(0);
$allowedExts = array("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)) {
if ($_FILES["file"]["error"] > 0) {
error("文件错误");
} else {
$ImageCachePath='images/hulu/';
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"];
$data = json_decode(upload($files)) ;
unlink($files);
if ($data->status!==1){
error("上传错误");
}else{
exit(json_encode([
"code"=>1,
"msg"=>"上传成功!",
"imgurl"=>str_replace("http","https",$data->url),
"fid"=>$data->fid,
"size"=>$data->size,
],JSON_UNESCAPED_UNICODE));
}
}
}else {
error("非法的文件格式");
}
function upload($file){
$post = array(
'file'=>new \CURLFile(realpath($file))
);
$ch = curl_init("http://upload.huluxia.com/upload/image/avatar?platform=2&gkey=000000&app_version=4.0.1.3.1&versioncode=275&market_id=tool_web&_key=这里的key自己去抓");
curl_setopt_array($ch, array(
CURLOPT_POST => true,
CURLOPT_VERBOSE => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => array(),
CURLOPT_POSTFIELDS => $post,
));
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
function error($str){
exit(json_encode([
"code"=>-1,
"msg"=>$str
],JSON_UNESCAPED_UNICODE));
}
![](https://blog.oioweb.cn/usr/uploads/2019/09/4285961571.png)
2.百度图床
<?php
header('Access-Control-Allow-Origin:*');
header('Content-type:application/json; charset=utf-8');
error_reporting(0);
$allowedExts = array("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)) {
if ($_FILES["file"]["error"] > 0) {
error("文件错误");
} else {
$post_data = [
"image"=>new \CURLFile(realpath($_FILES['file']['tmp_name'])),
];
$data = Curl_POST("https://graph.baidu.com/upload",$post_data);
if ($data==""){
error("上传失败");
}elseif (json_decode($data)->msg!=="Success"){
error("上传失败");
}else{
$pic = "https://graph.baidu.com/resource/".json_decode($data)->data->sign.".jpg";
echo json_encode([
"code"=>1,
"imgurl"=>$pic
]);
}
}
}else {
error("非法的文件格式");
}
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);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
function error($str){
exit(json_encode([
"code"=>-1,
"msg"=>$str
],JSON_UNESCAPED_UNICODE));
}
3.京东图床
<?php
header('Access-Control-Allow-Origin:*');
header('Content-type:application/json; charset=utf-8');
error_reporting(0);
$allowedExts = array("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)) {
if ($_FILES["file"]["error"] > 0) {
error("文件错误");
} else {
if (class_exists('CURLFile')) { // php 5.5
$post_data = [
"file"=>new \CURLFile(realpath($_FILES['file']['tmp_name']))
];
} else {
$post_data = [
"file"=>'@'.realpath($_FILES['file']['tmp_name'])
];
}
$rel = Curl_POST('https://search.jd.com/image?op=upload',$post_data);
preg_match('/callback(?:\(\")(.*)(?:\"\))/i',$rel,$matches);
if (!@$matches[1]) {
error("图片上传失败!");
}
$arr = array(
'code' =>1,
'imgurl'=>'https://img'.rand(10,14).'.360buyimg.com/uba/'.$matches[1]
);
echo json_encode($arr);
}
}else {
error("非法的文件格式");
}
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(),
"Accept:application/json",
"Accept-Encoding:gzip,deflate,sdch",
"Accept-Language:zh-CN,zh;q=0.8",
"Connection:close"
];
$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_REFERER, 'http://m.qzone.com/infocenter?g_f=');
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");
curl_setopt($ch, CURLOPT_HTTPHEADER,$header);
curl_setopt($ch, CURLOPT_ENCODING, '');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS,$post_data);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
function error($str){
exit(json_encode([
"code"=>-1,
"msg"=>$str
],JSON_UNESCAPED_UNICODE));
}