코드 예시

깃허브 코드 예시

여기서 API 엔드포인트와 상호 작용하는 데 도움이 되는 여러 코드 예제를 찾을 수 있습니다. Github 저장소에서 더 많은 예제를 확인할 수 있습니다. 저희 저장소에 있는 예제에 기여해 주세요!

깃허브 저장소

PHP OAuth 샘플

여기 PHP 코드 예제가 있어요. 기본적인 Oauth2 요청을 어떻게 하는지 보여줍니다.

<?php

$api_url = 'https://www.cryptohopper.com';
$redirect_url = ''; // 리다이렉션 URL을 입력하세요. 이를 비워두면 앱의 기본 설정이 사용됩니다.
$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 요청 샘플

이건 PHP 코드 예시로, access_token과 access_secret을 이용한 기본 API 요청 방법을 보여줍니다.

<?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 웹훅 샘플

이것은 PHP 코드 예제로, 웹훅 요청을 받고 유효성을 검사하는 방법을 보여줍니다.

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