Esempi di codice

Esempi di codice su GitHub

Qui troverai moltissimi esempi di codice che ti aiuteranno ad interagire con le nostre endpoint API. Altri esempi li puoi trovare nel nostro repository Github. Sentiti libero di contribuire ai nostri esempi nel nostro repository!

Repository GitHub

PHP OAuth Esempio

Ecco un esempio di codice PHP che mostra come eseguire una richiesta OAuth2 di base.

<?php

$api_url = 'https://www.cryptohopper.com';
$redirect_url = ''; // Inserisci il tuo URL di reindirizzamento. Se lo lasci vuoto, viene utilizzata la configurazione predefinita dell'app.
$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

Esempio di richiesta API PHP

Questo è un esempio di codice PHP che mostra come effettuare una richiesta API base con un access_token e 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);

Esempio Webhook PHP

Questo è un esempio di codice PHP che mostra come ricevere e validare una richiesta webhook.

<?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 'Unknown webhook message type.';
	}
}else{
	//invalid signature
	echo 'Invalid signature.';
}
?>