syropian
1/5/2012 - 6:21 PM

Function for obtaining a response from a digest-based API in Laravel. The session must contain a username and password, and you must have MC

Function for obtaining a response from a digest-based API in Laravel. The session must contain a username and password, and you must have MCrypt installed for the Crypter class to work.

<?php
class API {
	public static function call($url) {
		$user = Session::get('username');
		$password = Session::get('password');
		$process = curl_init($url);                                                                         
		curl_setopt($process, CURLOPT_USERPWD, $user.':'.Crypter::decrypt($password)); 
		curl_setopt($process, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);                                               
		curl_setopt($process, CURLOPT_TIMEOUT, 30);                                                                         
		curl_setopt($process, CURLOPT_RETURNTRANSFER, TRUE);                                                                
		$response = curl_exec($process);
		$status = curl_getinfo($process);
		curl_close($process);
		if ($status['http_code'] != '200'){
			return "Authentication Failed";
		}
		else {
		    return $response;
		}
	}
}
?>