PHP curl

By

function api_exec($url, $post_data) {
	
	$auth_username = "username";
	$auth_password = "password";

	$options = array(
		CURLOPT_USERPWD				 => $auth_username . ":" . $auth_password,
		CURLOPT_URL            => $url,
		CURLOPT_HEADER         => true,    
		CURLOPT_VERBOSE        => true,
		CURLOPT_RETURNTRANSFER => true,
		CURLOPT_FOLLOWLOCATION => true,
		CURLOPT_SSL_VERIFYPEER => false,
		CURLOPT_CUSTOMREQUEST  => "POST",
		CURLOPT_POSTFIELDS     => json_encode($post_data) ,
	);

	$ch = curl_init();
	curl_setopt_array( $ch, $options );

	try {
	  $raw_response  = curl_exec( $ch );
		// extract header
		$headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
		$header = substr($raw_response, 0, $headerSize);

		// extract body
		$body = substr($raw_response, $headerSize);
		
	  if(curl_errno($ch)) {
	  	return;
	  }

	  $status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);

	  if ($status_code != 200) return;

	} catch(Exception $ex) {
		if ($ch != null) {
			curl_close($ch);
			return;
		}
	}

	if ($ch != null) {
		curl_close($ch);
		return $body;
	}
}

$url = "http://..../backend/api/v4/api.php";
$post_data = array(
	'field_1' => "field_1_data",
	'field_2' => "field_1_data"
);

$result = api_exec($url, $post_data);