Location: PHPKode > scripts > HTTPS Request send files & custom headers > https-request-send-files-custom-headers/HttpsRequest.class.php
<?php
/**************************************************************************

Send files and custom headers in https POST requests

Requires	:	Curl extension fro PHP5, 
				user and pass base64_encoded passed to contructor 

Author 		: 	Jakub Pezacki
email		:	hide@address.com

***************************************************************************/

class HttpsRequest{

var $user;
var $pass;

function send_file($url,$file){
 
		$header = array();
	   	$header[] = "Content-Type: text/xml";
	   
       	$ch = curl_init();       
       	curl_setopt($ch, CURLOPT_URL,$url);
       	
       	//send the file
       	$fp = fopen($file, "r");
       	curl_setopt($ch, CURLOPT_PUT, true);
       	curl_setopt($ch, CURLOPT_UPLOAD, true);
		curl_setopt($ch, CURLOPT_INFILE, $fp); 
       	curl_setopt($ch, CURLOPT_INFILESIZE, filesize($file)); 
       	
       	//headers
       	curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
       	curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
		       	
       	//verification	//login:pass ~ gets base64 encoded by curl
       	curl_setopt($ch, CURLOPT_USERPWD, $this->user.":".$this->pass);
        curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
       	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
       	curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
       	
       	//errors reporting
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
       	curl_setopt($ch, CURLOPT_VERBOSE, true);
       	curl_setopt($ch, CURLOPT_PROGRESS, true);
       	curl_setopt($ch, CURLOPT_MUTE, 0);
       	
		
		$data = curl_exec($ch);
		      	 
       	//display errors
       	$cErr = curl_errno($ch);
         
       	  if ($cErr != '') {
        	$err = 'cURL ERROR: '.curl_errno($ch).': '.$cErr.'<br>';
			foreach(curl_getinfo($ch) as $k => $v){
				$err .= "$k: $v<br>";
			}
			echo("Error $err");
			curl_close($ch);
	    	return false;
		}
       	
       	curl_close($ch);
		return $data;
}
	
function send_custom_headers($url,$header){
 
	   	$header[] = "Content-Type: text/xml";
	    $header[] = "Content-Length: 0";
	   	
       	$ch = curl_init();       
       	curl_setopt($ch, CURLOPT_URL,$url);
       	     	
       	//headers
       	curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
       	curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
		       	
       	//verification	//login:pass ~ gets base64 encoded by curl
       	curl_setopt($ch, CURLOPT_USERPWD, $this->user.":".$this->pass);
        curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
       	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
       	curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
       	
       	//errors reporting
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
       	curl_setopt($ch, CURLOPT_VERBOSE, true);
       	curl_setopt($ch, CURLOPT_PROGRESS, true);
       	curl_setopt($ch, CURLOPT_MUTE, 0);
       	
		
		$data = curl_exec($ch);
		      	 
       	//display errors
       	$cErr = curl_errno($ch);
         
       	  if ($cErr != '') {
        	$err = 'cURL ERROR: '.curl_errno($ch).': '.$cErr.'<br>';
			foreach(curl_getinfo($ch) as $k => $v){
				$err .= "$k: $v<br>";
			}
			echo("Error $err");
			curl_close($ch);
	    	return false;
		}
       	
       	curl_close($ch);
		return $data;
}


function HttpsRequest($user,$pass){

	$this->user = base64_decode($user);
	$this->pass = base64_decode($pass);
	}

}
?>
Return current item: HTTPS Request send files & custom headers