Curl Post

class Mainapp{
  
    function curlExecute($url, $auth_key, $method, $post_variables=array()){
      
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,$url);    //setting url
              
        if(strtoupper($method)=="GET"){
            curl_setopt($ch, CURLOPT_POST, 0);
        }elseif(strtoupper($method)=="DELETE"){
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
        }else{
            curl_setopt($ch, CURLOPT_POST, 1);
        }
      
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        if(!empty($post_variables)){
            //$post_variables=array("name"=>"testrah","password"=>"123456");    //setting post variables
            curl_setopt($ch, CURLOPT_POSTFIELDS,$post_variables);  //Posting Fields
            //curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  
        }
      
        $headers = ['Auth: '.$auth_key];    // setting authentication in header

        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);    // setting authentication in header in curl  

        $server_output=curl_exec($ch);    // executing curl
  
        curl_close ($ch);        // closing curl;
      
        $data_decoded=json_decode($server_output);
        print_r($server_output);
        return $data_decoded;
    }
  
    function test(){
        return "working";
    }
}
Previous
Next Post »