PHP code example of snlbaral / coinbase-api-php

1. Go to this page and download the library: Download snlbaral/coinbase-api-php library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

snlbaral / coinbase-api-php example snippets




return [
    /**
     * Your coinbase client ID.
     */
    'COINBASE_CLIENT_ID' => '<COINBASE_CLIENT_ID>',

    /**
     * Your coinbase client secret.
     */
    'COINBASE_CLIENT_SECRET' => '<COINBASE_CLIENT_SECRET>',

    /**
     * Your coinbase account id/address.
     */
    'COINBASE_ACCOUNT_ID' => '<COINBASE_ACCOUNT_ID>',
];


session_start();
onfig.php') or die('Configuration file not found');
$redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];

$client = new GuzzleHttp\Client();

if(isset($_GET['code'])) {
	$code = $_GET['code'];
	$response = $client->Request(
		'POST',
		'https://api.coinbase.com/oauth/token',
		[
			'form_params' => [
				'grant_type' => 'authorization_code',
				'code' => $code,
				'client_id' => $config['COINBASE_CLIENT_ID'],
				'client_secret' => $config['COINBASE_CLIENT_SECRET'],
				'redirect_uri' => $redirect,
			],
		]
	);
	$data = json_decode($response->getBody()->getContents(),TRUE);
	$access_token = $data['access_token'];
	$refresh_token = $data['refresh_token'];
	file_put_contents('refreshToken.txt', $refresh_token);
	$_SESSION['coinbase_token'] = $access_token;
	header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
	return;
}

if(isset($_REQUEST['logout'])) {
	unset($_SESSION['coinbase_token']);
}

if(!isset($_SESSION['coinbase_token'])) {
	echo '<a href="https://www.coinbase.com/oauth/authorize?client_id='.$config['COINBASE_CLIENT_ID'].'&redirect_uri='.$redirect.'&response_type=code&scope=wallet%3Aaddresses%3Acreate,wallet%3Aaddresses%3Aread,wallet%3Anotifications%3Aread" target="_blank">Authorize Me</a>';
} else {
	echo '<h2>Save this access token for later use.</h2>';
	echo 'Access Token: '.$_SESSION['coinbase_token'];
	echo '<br/><a href="?logout">Logout</a>';
}


use Snlbaral\Coinbase\Coinbase;

$client = new Coinbase();
$accessToken = "<Access Token from Step 3>";

$address = $client->getAddress($accessToken);

$transactions = $client->listTransactions($accessToken);

$refreshToken = file_get_contents('refreshToken.txt');
$accessToken = $client->getAccessToken($refreshToken); //returns new access token valid for next 2 hours.

try {
	$address = $client->getAddress($accessToken);
	print_r($address);
	//$address = json_decode($address,true);
	//echo $address['data']['address'];
} catch (Exception $e) {

	if($e->getCode()==401) {
		$refreshToken = file_get_contents('refreshToken.txt');
		$accessToken = $client->getAccessToken($refreshToken);
		//Save/Update the this new access token to use it for next 2 hours
		
		//Get Address using new access token
		$address = $client->getAddress($accessToken);
		print_r($address);		
	} else {
		print_r($e->getMessage());
	}

}