Kod örnekleri

GitHub'taki Kod Örnekleri

Burada API uç noktalarımızla etkileşime geçmenize yardımcı olacak çoklu kod örneği bulacaksınız. Daha fazla örnek, Github deposunda mevcuttur. Deponun örneklerine katkıda bulunmaktan çekinme!

GitHub deposu

PHP OAuth Örneği

İşte temel bir Oauth2 isteği nasıl gerçekleştirileceğine dair PHP kod örneği.

<?php

$api_url = 'https://www.cryptohopper.com';
$redirect_url = ''; // Yönlendirme URL'ni gir.  Bu alanı boş bırakırsan, uygulamanın varsayılan ayarı kullanılır.
$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 İstek Örneği

Bu, temel API isteği gerçekleştirmek için bir access_token ve access_secret ile nasıl kullanılacağını gösteren bir PHP kod örneğidir.

<?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 Örneği

Bu, webhook isteğini nasıl alacağınız ve doğrulayacağınızı gösteren bir PHP kod örneğidir.

<?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.';
}
?>