Code Examples on Github
Here you will find multiple code samples that will help you interact with our API endpoints. You will find more examples in our Github repository. Feel free to contribute to our samples in our repository!
PHP Oauth Sample
Here's a PHP code sample that shows how to perform a basic Oauth2 request.
<?php $api_url = 'https://www.cryptohopper.com'; $redirect_url = ''; // Enter your redirect url. If you leave this empty, the default setting from your app is used. $app_key = 'ENTER_YOUR_APP_KEY'; $app_secret = 'ENTER_YOUR_SECRET'; $method = 'access_tokens'; $state = '123'; // optional $data = ''; if(is_array($_GET) && !empty($_GET)){ $data = json_encode($_GET, JSON_PRETTY_PRINT); } if(!empty($data)){ echo '<h3>Endpoint: <strong>'.$method.'</strong></h3>'; echo '<h3>State: <strong>'.$state.'</strong></h3><br><hr>'; echo '<h3>Result</h3><br><br><pre>'; echo $data; echo '</pre><br><br>'; }else{ $path = '/oauth2/'.$method.'?app_key='.$app_key.'&state='.urlencode($state).'&redirect_uri='.urlencode($redirect_url); $signature = hash_hmac('sha512', $path, $app_secret); header('Location: '.$api_url.$path.'&signature='.$signature); }// if data
PHP API Request Sample
This is a PHP code sample that shows how to perform a basic API request with an access_token and access_secret.
<?php $access_token = 'ENTER_YOUR_ACCESS_TOKEN'; $operation = 'hopper'; $method = 'GET'; $data_string = '{}'; $path = '/v1/'.$operation; $headers = array( 'access-token: '.$access_token ); $ch = curl_init($api_url.$path); if($method == 'POST'){ $headers[] = 'Content-Type: application/json'; $headers[] = 'Content-Length: ' . strlen($data_string); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); }elseif($method == 'GET'){ curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); }elseif($method == 'PATCH'){ $headers[] = 'Content-Type: application/json'; $headers[] = 'Content-Length: ' . strlen($data_string); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PATCH"); curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); }elseif($method == 'DELETE'){ $headers[] = 'Content-Type: application/json'; $headers[] = 'Content-Length: ' . strlen($data_string); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE"); curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); } $result = curl_exec($ch); $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE); $error = curl_error($ch); $result_json = json_decode($result, true); if(is_array($result_json)){ $result_json = json_encode($result_json, JSON_PRETTY_PRINT); } echo '<h3>Endpoint: <strong>'.$operation.'</strong></h3>'; echo '<h3>Method: <strong>'.$method.'</strong></h3>'; echo '<h3>Status code: <strong>'.$httpcode.'</strong></h3><br><hr>'; echo '<h3>Result</h3><br><br><pre>'; if(!empty($result_json)){ echo $result_json; }else{ echo $result; } echo '</pre><br><br>'; if(!empty($error)){ echo '<h3>Error</h3><br><br>'; echo $error; } curl_close($ch);
PHP Webhook Sample
This is a PHP code sample that shows how to receive and validate a webhook request.
<?php $app_secret = 'ENTER_YOUR_APP_SECRET'; $verification_code = 'MY_VERIFICATION_CODE'; $requestBody = file_get_contents('php://input'); $headers = getallheaders(); $headers = array_change_key_case($headers, CASE_UPPER); $json = json_decode($requestBody, true); $signature = hash_hmac('sha512', $requestBody, $app_secret); if($signature == $headers['X-HUB-SIGNATURE']){ //valid webhook request if($json['type'] == 'validate'){ // validate webhook echo $verification_code; }elseif($json['type'] == 'messages'){ // receive messages print_r($json['messages'], true); }else{ // unkown type echo 'Unkown webhook message type.'; } }else{ //invalid signature echo 'Invalid signature.'; } ?>