Skip to content

Commit

Permalink
Merge pull request #3 from picqer/guzzle6
Browse files Browse the repository at this point in the history
Update to Guzzle 6
  • Loading branch information
stephangroen committed Oct 27, 2015
2 parents 7cf9fa0 + b0a2705 commit c27579a
Show file tree
Hide file tree
Showing 5 changed files with 371 additions and 99 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ composer.phar
vendor/
.env
index.php
storage.txt
storage.txt
composer.lock
10 changes: 2 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,9 @@
"email": "[email protected]"
}
],
"repositories": [
{
"type": "vcs",
"url": "https://github.com/picqer/guzzle-oauth2-plugin"
}
],
"require": {
"php": ">=5.4.0",
"commerceguys/guzzle-oauth2-plugin": "~1.0"
"php": ">=5.5.0",
"guzzlehttp/guzzle": "~6.0"
},
"autoload": {
"psr-4": {
Expand Down
88 changes: 88 additions & 0 deletions example.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

require 'vendor/autoload.php';

function getValue($key)
{
$storage = json_decode(file_get_contents('storage.json'), true);
if (array_key_exists($key, $storage)) {
return $storage[$key];
}
return null;
}

function setValue($key, $value)
{
$storage = json_decode(file_get_contents('storage.json'), true);
$storage[$key] = $value;
file_put_contents('storage.json', json_encode($storage));
}

function authorize()
{
$connection = new \Picqer\Financials\Exact\Connection();
$connection->setRedirectUrl('YOUR_REDIRECT_URL');
$connection->setExactClientId('YOUR_CLIENT_ID');
$connection->setExactClientSecret('YOUR_CLIENT_SECRET');
$connection->redirectForAuthorization();
}

function connect()
{
$connection = new \Picqer\Financials\Exact\Connection();
$connection->setRedirectUrl('YOUR_REDIRECT_URL');
$connection->setExactClientId('YOUR_CLIENT_ID');
$connection->setExactClientSecret('YOUR_CLIENT_SECRET');

if (getValue('authorizationcode')) // Retrieves authorizationcode from database
{
$connection->setAuthorizationCode(getValue('authorizationcode'));
}

if (getValue('accesstoken')) // Retrieves accesstoken from database
{
$connection->setAccessToken(getValue('accesstoken'));
}

if (getValue('refreshtoken')) // Retrieves refreshtoken from database
{
$connection->setRefreshToken(getValue('refreshtoken'));
}

if (getValue('expires_in')) // Retrieves expires from database
{
$connection->setTokenExpires(getValue('expires_in'));
}

// Make the client connect and exchange tokens
try {
$connection->connect();
} catch (\Exception $e) {
throw new Exception('Could not connect to Exact: ' . $e->getMessage());
}

// Save the new tokens for next connections
setValue('accesstoken', $connection->getAccessToken());
setValue('refreshtoken', $connection->getRefreshToken());
setValue('expires_in', $connection->getTokenExpires());

return $connection;
}

if (isset($_GET['code']) && is_null(getValue('authorizationcode'))) {
setValue('authorizationcode', $_GET['code']);
}

$connection = connect();

try {
$journals = new \Picqer\Financials\Exact\Journal($connection);
$result = $journals->get();
foreach ($result as $journal) {
echo 'journal: ' . $journal->Description . '<br>';
}

echo 'done';
} catch (\Exception $e) {
echo get_class($e) . ' : ' . $e->getMessage();
}
2 changes: 1 addition & 1 deletion example/example.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ function connect()

// Make the client connect and exchange tokens
try {
$connection->client();
$connection->connect();
} catch (\Exception $e) {
throw new Exception('Could not connect to Exact: ' . $e->getMessage());
}
Expand Down
Loading

0 comments on commit c27579a

Please sign in to comment.