<?php
	##Aircall Api curl functions
	/*  GET
	-	/ping	#test auth responce will pong
	-	/numbers  #all no info/list
	-	/numbers/23159  #spesific no info using id i.e. numbers/id
	-	/company
	-	/users		#list fo users
	-	/users/<id>
	-	/calls		#call list
	-	/calls/<id>
	-	/contacts
	-	/contacts/<id>
	-   /numbers?page=1&per_page=50&order=desc ##pagination max per page 50

	list($response,$info) = get_aircall($apiId,$apiToken,'/contacts');
	*/
	
	/* POST

		$post_data = json_decode('{  
								  "first_name": "Aj",
								  "last_name": "neo",
								  "information": "",
								  "phone_numbers": [
									{
									  "label": "Work",
									  "value": "+33631000777"
									}
								  ],
								  "emails": [
									{
									  "label": "Work",
									  "value": "john.doe@something.ioc"
									}
								  ]
								}',1);


		//list($response,$info) = post_aircall('4de64aded2cd6a9e9381dcefa9528554','71d849b2d9f537d87f02295e6423eded','/contacts',$post_data);
	*/
	
	class class_aircall{
		
		function post_aircall($apiId,$apiToken,$url,$post_data){	 
			$username=$apiId;
			$password=$apiToken;	

			$url_path ="https://api.aircall.io/v1".$url;	

			$request['content_type'] = 'application/json';
			$json_post_data = json_encode($post_data);
			$ch = curl_init(); 
			
			$agent = $_SERVER["HTTP_USER_AGENT"];//Set the useragent
			curl_setopt($ch, CURLOPT_USERAGENT, $agent);
			curl_setopt($ch, CURLOPT_TIMEOUT, 30); //times out after 30s 		
			curl_setopt($ch, CURLOPT_URL, $url_path );	//Set the URL
			curl_setopt($ch, CURLOPT_POST,1);		
			curl_setopt($ch, CURLOPT_POSTFIELDS,$json_post_data);
			/*curl_setopt($ch, CURLOPT_HTTPHEADER, array(                
				 "Content-Type: ".$request['content_type'],           
				"Content-Length: ".strlen($json_post_data))                        
			); */ 						
			//We want the content after the query
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 

			curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
			curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
			
			curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);			 
			//Follow Location redirects
			curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);			
			 
			//Execute the action to login
			$postResult = curl_exec($ch);			
			//if($postResult == false){ return $info = curl_getinfo($ch);}
			$info = curl_getinfo($ch);
			$response	=	json_decode($postResult);
			//echo '==<pre>'; print_r($response); echo '</pre>';
			return array($response,$info); 
		}//post_aircall

	
		function get_aircall($apiId,$apiToken,$url){	 
			$username=$apiId;
			$password=$apiToken;		
			$url_path ="https://api.aircall.io/v1".$url;		
			$request['content_type'] = 'application/json';

			$ch = curl_init(); 
			
			$agent = $_SERVER["HTTP_USER_AGENT"];//Set the useragent
			curl_setopt($ch, CURLOPT_USERAGENT, $agent);
			curl_setopt($ch, CURLOPT_TIMEOUT, 30); //times out after 30s 		
			curl_setopt($ch, CURLOPT_URL, $url_path );	//Set the URL
			//curl_setopt($ch, CURLOPT_POST,1);		
			/* curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                               "Authorization: Bearer ".$access_token, 
			"User-Agent: ".$app_details,				
			)                                                                      
				);   */			
			//We want the content after the query
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 

			curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
			curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
			
			curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);			 
			//Follow Location redirects
			curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);			
			 
			//Execute the action to login
			$postResult = curl_exec($ch);			
			//if($postResult == false){ return $info = curl_getinfo($ch);}
			$info = curl_getinfo($ch);
			$response	=	json_decode($postResult);
			//echo '==<pre>'; print_r($response); echo '</pre>';
			return array($response,$info); 
		}//get_aircall	
	
	} //class_aircall


?>