Wednesday, February 5, 2020

How to download files from AWS CDN using PHP?

We have implemented the below code for download any type of file from AWS server using PHP. Please refer and implement same.


<?php 


  function downloadAWSFiles()
   {
       try{
           $fileName   = 'https://project021.s3.ap-south-1.amazonaws.com/p1dvtscgk06mh1t2sj6o3imne64.pdf';
           $fileExt   =  strtolower(pathinfo($fileName, PATHINFO_EXTENSION));           
           $pos    = strrpos($fileName, '/');
           $downloadFile  = substr($fileName, $pos + 1);
           $newFileName  = 'newFiles.'.$fileExt;
           switch($fileExt) {
               case 'pdf': $mime = 'application/pdf'; break;
               case 'zip': $mime = 'application/zip'; break;
               case 'jpeg': $mime = 'image/jpeg'; break;
               case 'jpg': $mime = 'image/jpg'; break;
               case 'png': $mime = 'image/png'; break;
               default: $mime = 'application/force-download';
             }
           header('Pragma: public');     // required
           header('Expires: 0');     // no cache
           header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
           header('Cache-Control: private', false);
           header('Content-Type: ' . $mime);
           header('Content-Disposition: attachment; filename="' . $newFileName .'"');
           header('Content-Transfer-Encoding: binary');
           header('Connection: close');
           readfile("https://s3.ap-south-1.amazonaws.com/" . 'xxxx' . "/" . $downloadFile);
           exit();
       }catch(Exception $e){
       return array();
       }    
   }
   
   Call to the function 
   
   downloadAWSFiles();
   
   
?>

Monday, February 3, 2020

How to integrate google Translate Api using PHP?

Read carefully follow below steps before you integrate the google cloud API for translate using PHP.


Step:1 -> Download the Google client direct from git repo :

   //command line download using below command
           $ composer require google/cloud-translate

Step: 2-> Creating a Service Account for Authentication and get an API_KEY

Step:3 -> Create a new file classTGooleranslate.php 

class googleTranslateMode{
          require vendor/autoload.php;
          use Google\Cloud\Translate\TranslateClient;

 //Google Trans
   public static function goolgeTransConnect() {
      if(!isset(self::$goolgeObj)) {    
          self::$goolgeObj = new TranslateClient(['key' => GOOGLE_TRANSLATE_KEY]);
      }
      return self::$goolgeObj;
     }


       //Convert translate from Google APi to Target Language
 public function doCaptiontranslate(){
  
 try {    
        $translate      =   self::goolgeTransConnect();
  //english  to translate spanish   
  $lang      =  'es'; 
  
  $result     = array();
  $content ="Translate english to spanish";
  
  if(!empty(trim($content)) && !empty($lang)){
   $result    =   $this->curlTranslateurl($lang,$content,$translate);
  $translatedContent   = $result['text'];
  return  $translatedContent;

     }catch (Exception $e) {
   echo "<pre>";print_r($e);die;
     }
 }

        
 #@ Function    : curlTranslateurl
 #@ Description   : Get language Translated content from google
 public function curlTranslateurl($lang=null,$content=null,$translate,$response=true){
  try{
   $result=array();
   if(!empty($lang) && !empty($content)){
    if($response){
     $result = $translate->translate($content, ['target' => $lang]);  
    }    
   }
   return $result;
  }catch (Exception $e) { 
   
   
   return $e;
  }
 }
    }

Create  an instance of the googleTranslateMode class
$tansObj=new googleTranslateMode();
$tansObj->doCaptiontranslate();
OutPut : Traducir inglés a español