API Storage

API Backend
function post($file,$url,$data=[],$headers=array()){
 
    $image = fopen($file, "rb");


    $ch = curl_init();
    curl_setopt($ch, CURLOPT_INFILE, $image);
    curl_setopt($ch, CURLOPT_INFILESIZE, filesize($file));
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_POST, 1); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($ch, CURLOPT_HTTPHEADER,$headers );
      
    $result = curl_exec($ch);
    $a =  @json_decode($result);
    return is_object($a)?$a:$result; 
}
//how to use
$apikey="....";
$headers=["api-key: ".$apikey];
//
$data=[];
$file = "image.jpg";
//begin
$res = post($file,$url,$data,$headers);
if($res->code){
  echo $res->url;
}else{
   echo $res->error; 
}
Client JS Vanilla
<script src="https://storage.donggiatri.com/sdk.min.js?callback=storage_callback"></script>
<script>
window.storage_callback = function(){
    var server = {
      apikey:"...",
      host:"...",
      zone:"vn",
      user:{
         _id:0,
         name:"ABC"
      }
    };
    //how to use
    ServerStorage(server).then(function(io){
 
      io.message(function(data){
          
      });
      //upload file
      var data={
          file : null,
          progress: function(percent,loaded,total){

          }
      };
      io.upload(data).then(function(res){
         //your code
      }).then(function(res){
         //your code
      });
    }); 
};
</script>

Example

Truy cập server