Mengambil Access Token dari Tokopedia menggunakan PHP Curl
Diketahui:
Dari dokumentasi tokopedia di https://developer.tokopedia.com/openapi/guide/#/authentication maka untuk mendapatkan access token harus menggunakan format Curl seperti dibawah ini:
curl -X POST \
'https://accounts.tokopedia.com/token?grant_type=client_credentials' \
-H 'Authorization: Basic Y2xpZW50X2lkOmNsaWVudF9zZWNyZXQK' \
-H 'Content-Length: 0' \
-H 'User-Agent: PostmanRuntime/7.17.1'
Pertanyaan:
Bagaimana cara untuk membuatnya menggunakan PHP …. ?
Jawaban:
Kamu bisa gunakan kode berikut ini:
<?php
$host="https://accounts.tokopedia.com/token";
$user='nama_usermu';
$password='passwordmu';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$host);
curl_setopt($ch, CURLOPT_USERPWD, "$user:$password");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,
"grant_type=client_credentials");
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
$hasil = curl_exec($ch);
if (curl_errno($ch)) {
print curl_error($ch);
}
curl_close($ch);
echo $hasil;
?>
Sekian dan terima kasih