MENU

百度图床接口上传源码分享-PHP

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

前言:

看了一下百度的,百度的应该是最稳的了吧,直接分享出来,我感觉不要太依赖图床,毕竟不是自己的,说不定什么时候就不能用了,我图片都是本地的,直接上代码,我写好以后是在本地测试的,不知道支持不支持临时文件上传,不支持的话你们改一下就行了

代码:

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

最近是真滴不知道更新什么文章,就瞎鸡儿发发吧

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

5 Comments
  1. 这图床有效期是多久啊。

  2. 可不可以写个html主页。就是自己直接形式的

    1. @沐熙没懂

    2. @教书先生就是像群文件那样的有js,css

    3. @沐熙写的有啊