Created
September 4, 2014 11:08
-
-
Save ArthurGuy/b97ed60551053bcdc843 to your computer and use it in GitHub Desktop.
An example oauth request for the Crunch API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Create the access token | |
$accessToken = new \ZendOAuth\Token\Access(); | |
$accessToken->setToken($key); | |
$accessToken->setSecret($secret); | |
$oauth_options = array( | |
'consumerKey' => $this->key, | |
'consumerSecret' => $this->secret, | |
'requestTokenUrl' => $this->base_url.$this->request_token_url, | |
'userAuthorizationUrl' => $this->base_url.$this->authorise_url, | |
'accessTokenUrl' => $this->base_url.$this->access_token_url, | |
'signatureMethod' => 'HMAC-SHA1', | |
'version' => '1.0', | |
'requestScheme' => \ZendOAuth\OAuth::REQUEST_SCHEME_QUERYSTRING, | |
'requestMethod' => \ZendOAuth\OAuth::POST | |
); | |
//Setup the client | |
$client = $this->accessToken->getHttpClient($oauth_options); | |
$adapter = new \Zend\Http\Client\Adapter\Curl(); | |
$adapter = $adapter->setCurlOption(CURLOPT_SSL_VERIFYHOST,false); | |
$adapter = $adapter->setCurlOption(CURLOPT_SSL_VERIFYPEER,false); | |
$client->setAdapter($adapter); | |
//Make a request | |
//$client->reset(); | |
$client->setMethod(\ZendOAuth\OAuth::GET); | |
$client->setUri($this->base_url.$this->api_base_url.$endpoint); | |
$params = []; | |
$client->setParameterGet($params); | |
try { | |
$response = $client->send(); | |
} catch (\Exception $e) { | |
throw new \Crunch\Exception\Server('Error connecting to the Crunch API'); | |
} | |
if (($response->getStatusCode() >= 200) && ($response->getStatusCode() < 300)) | |
{ | |
//The xml response | |
$response->getBody(); | |
} | |
elseif ($response->getStatusCode() == 404) | |
{ | |
throw new \Crunch\Exception\Server('404 - Endpoint not found ('.$this->base_url.$this->api_base_url.$endpoint.')'); | |
} | |
else | |
{ | |
\Crunch\Exception\Handler::throwException($response->getStatusCode().':'.$response->getBody()); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment