Contoh kode
Contoh Kode di GitHub
Di sini je temukan banyak contoh kode yang bakal bantu je berinteraksi dengan endpoint API kita. Je temukan contoh lain di repository Github kita. Jangan ragu untuk berkontribusi ke contoh-contoh di repository kita!
Contoh PHP OAuth
Nih contoh kode PHP yang memperlihatkan bagaimana melakukan permintaan Oauth2 dasar.
<?php
$api_url = 'https://www.cryptohopper.com';
$redirect_url = ''; // Masukkan URL redirect mu. Jika dibiarkan kosong, pengaturan default dari app mu akan digunakan.
$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
Contoh Permintaan API PHP
Ini contoh kode PHP yang memperlihatkan bagaimana melakukan permintaan API dasar dengan *access_token* dan *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);
Contoh Webhook PHP
Ini contoh kode PHP yang memperlihatkan bagaimana menerima dan memvalidasi permintaan 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.';
}
?>