PHP使用聚合图床的上传接口PHP版和html版

折腾了好久 官方连个像样的例子都没给  折腾折腾

<?php 

    function http_request($url,$data = null,$headers=array())
    {
        $curl = curl_init();
        if(count($headers) >= 1 ){
            curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        }
        curl_setopt($curl, CURLOPT_URL, $url);
    
    
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
    
    
        if (!empty($data)){
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        }
        
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  
        $output = curl_exec($curl);
        curl_close($curl);
        
        return $output;
    }
    
    if($_POST) {
        $data=array(
            'file'=>curl_file_create($_FILES['file']['tmp_name'],$_FILES['file']['type'],'test_name'),
        );
 
        $url = "https://api.superbed.cn/upload?token=233";//这里替换成你自己的token
        $res=http_request($url,$data,array());
        var_dump($res);exit;
    } else  {
    
?>
 
        <form enctype="multipart/form-data"  method="post"     action="2.php"  >
     
        <input type="file" name="file" id ="file" class ="up-file" style="height:34px;width:600px;margin-left:-8px;border:1px solid #ccc">
        <input type="hidden" name="aa" id="aa" value=0>
        <input type="submit" id = 'btn' value = "提交" class = "btn">
        </form>
    <?php 
    }
    ?>

 

下面html的

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
</head>
<body>
    <input type="file" id="uploadFile" name="smfile"   enctype="multipart/form-data"/>
    <div id="res"></div>
<script>
   //上传图片  返回一个网络图片路径url
    $('#uploadFile').change(function(){
        var f=this.files[0];
        var formData=new FormData();
        formData.append('smfile',f);    //强调一下这个对象的name一定是smfile,不能变
        $.ajax({
            url: 'https://api.superbed.cn/upload?token=3dsf',//token去官方申请个吧
            type: 'POST',
            success: function(data){
               // console.log(data);
                $('#res').html(JSON.stringify(data.url));//即图片的网络url//获取后可以使用
            },
            error: function(data){
                console.log(data);
            },
            data:formData,
            cache: false,
            contentType: false,
            processData: false

        });
    });

</script>
</body>
</html>

 

 

上一篇:php 微信退款


下一篇:php网络请求