Skip to content

Instantly share code, notes, and snippets.

@syropian
Created January 5, 2012 18:21
Show Gist options
  • Save syropian/1566487 to your computer and use it in GitHub Desktop.
Save syropian/1566487 to your computer and use it in GitHub Desktop.
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;
}
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment