MENU

京东图床接口源码案例分享-PHP

• September 12, 2019 • Read: 4413 • 经验分享,原创作品阅读设置

前言:

昨天想着搜狗的都发了,干脆也给京东图床的接口源码发一下,大家看到这两个,其他的图床接口也就能写了,支持临时文件和不支持临时文件的都写的有,Curl也封装的好好的直接用就行。直接上代码吧!

代码:

<?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));
}

另外我博客图床那个大家可以投票选一下用哪个图床,我不加自行切换的原因是 很多人已经安装我这个在线上传的代码了,在改动有点不好,所以选一个吧 比较好,搜狗图床听说几个月就不能用了,我推荐京东图床吧,大家可以去图床页面投投票。

Archives QR Code Tip
QR Code for this page
Tipping QR Code
Leave a Comment

2 Comments
  1. 这个接口能用多久@(滑稽)

    1. @南风基本上可以一直用