-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcomplete_workflow.php
More file actions
75 lines (59 loc) · 2.4 KB
/
complete_workflow.php
File metadata and controls
75 lines (59 loc) · 2.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?php
require_once __DIR__ . '/../vendor/autoload.php';
use OpenApi\OpenapiOauthClient;
use OpenApi\OpenapiClient;
use OpenApi\OpenapiException;
try {
echo "=== OpenAPI PHP SDK Complete Workflow Example ===" . PHP_EOL . PHP_EOL;
// Step 1: Create OAuth client
echo "Step 1: Creating OAuth client..." . PHP_EOL;
$oauthClient = new OpenapiOauthClient('<your_username>', '<your_apikey>', true);
echo "✓ OAuth client created" . PHP_EOL . PHP_EOL;
// Step 2: Generate token
echo "Step 2: Generating access token..." . PHP_EOL;
$scopes = [
'GET:test.imprese.openapi.it/advance',
'POST:test.postontarget.com/fields/country'
];
$tokenResult = $oauthClient->createToken($scopes, 3600);
$tokenData = json_decode($tokenResult, true);
if (!isset($tokenData['token'])) {
throw new OpenapiException('Failed to generate token: ' . $tokenResult);
}
$token = $tokenData['token'];
echo "✓ Token generated: " . substr($token, 0, 20) . "..." . PHP_EOL . PHP_EOL;
// Step 3: Create API client
echo "Step 3: Creating API client..." . PHP_EOL;
$apiClient = new OpenapiClient($token);
echo "✓ API client created" . PHP_EOL . PHP_EOL;
// Step 4: Make API calls
echo "Step 4: Making API calls..." . PHP_EOL;
// GET request
echo " → Making GET request..." . PHP_EOL;
$getParams = [
'denominazione' => 'altravia',
'provincia' => 'RM',
'codice_ateco' => '6201'
];
$getResponse = $apiClient->get('https://test.imprese.openapi.it/advance', $getParams);
echo " ✓ GET response received (" . strlen($getResponse) . " bytes)" . PHP_EOL;
// POST request
echo " → Making POST request..." . PHP_EOL;
$postPayload = [
'limit' => 10,
'query' => [
'country_code' => 'IT'
]
];
$postResponse = $apiClient->post('https://test.postontarget.com/fields/country', $postPayload);
echo " ✓ POST response received (" . strlen($postResponse) . " bytes)" . PHP_EOL . PHP_EOL;
echo "=== Workflow completed successfully! ===" . PHP_EOL;
} catch (OpenapiException $e) {
echo "✗ Error: " . $e->getMessage() . PHP_EOL;
if ($e->getHttpCode()) {
echo " HTTP Code: " . $e->getHttpCode() . PHP_EOL;
}
if ($e->getServerResponse()) {
echo " Server Response: " . json_encode($e->getServerResponse()) . PHP_EOL;
}
}