Skip to content

Commit 8b87d50

Browse files
committed
README and usage example update
1 parent a447130 commit 8b87d50

File tree

3 files changed

+62
-62
lines changed

3 files changed

+62
-62
lines changed

README.md

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -107,30 +107,48 @@ Here is a quick example:
107107
```php
108108
// include __DIR__ . '/loader.php';
109109
require 'vendor/autoload.php';
110-
$global_headers = array(Authorization: Basic XXXXXXX);
111-
$client = new SendGrid\Client('base_url', global_headers);
110+
$apiKey = YOUR_SENDGRID_API_KEY;
111+
$authHeaders = [
112+
'Authorization: Bearer ' . $apiKey
113+
];
114+
$client = new SendGrid\Client('https://api.sendgrid.com', $authHeaders);
115+
$param = 'foo';
112116
$response = $client->your()->api()->_($param)->call()->get();
113-
print $response->statusCode();
114-
print $response->headers();
115-
print $response->body();
117+
118+
var_dump(
119+
$response->statusCode(),
120+
$response->headers(),
121+
$response->body()
122+
);
116123
```
117124

118125
`POST /your/api/{param}/call` with headers, query parameters and a request body with versioning.
119126

120127
```php
121128
// include __DIR__ . '/loader.php';
122129
require 'vendor/autoload.php';
123-
$global_headers = array(Authorization: Basic XXXXXXX);
124-
$client = new SendGrid\Client('base_url', global_headers);
125-
$query_params = array('hello' => 0, 'world' => 1);
126-
$request_headers = array('X-Test' => 'test');
127-
$data = array('some' => 1, 'awesome' => 2, 'data' => 3);
128-
$response = $client->your()->api()->_($param)->call()->post('data',
129-
'query_params',
130-
'request_headers');
131-
print $response->statusCode();
132-
print $response->headers();
133-
print $response->body();
130+
$apiKey = YOUR_SENDGRID_API_KEY;
131+
$authHeaders = [
132+
'Authorization: Bearer ' . $apiKey
133+
];
134+
$client = new SendGrid\Client('https://api.sendgrid.com', $authHeaders);
135+
$queryParams = [
136+
'hello' => 0, 'world' => 1
137+
];
138+
$requestHeaders = [
139+
'X-Test' => 'test'
140+
];
141+
$data = [
142+
'some' => 1, 'awesome' => 2, 'data' => 3
143+
];
144+
$param = 'bar';
145+
$response = $client->your()->api()->_($param)->call()->post($data, $queryParams, $requestHeaders);
146+
147+
var_dump(
148+
$response->statusCode(),
149+
$response->headers(),
150+
$response->body()
151+
);
134152
```
135153

136154
<a name="usage"></a>

composer.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,6 @@
33
"description": "HTTP REST client, simplified for PHP",
44
"type": "library",
55
"version": "3.8.0",
6-
"require-dev": {
7-
"phpunit/phpunit": "~4.4",
8-
"squizlabs/php_codesniffer": "~2.0"
9-
},
106
"homepage": "http://github.com/sendgrid/php-http-client",
117
"keywords": ["SendGrid", "HTTP", "REST", "API", "Fluent"],
128
"license": "MIT",
@@ -23,6 +19,10 @@
2319
"require": {
2420
"php": ">=5.6"
2521
},
22+
"require-dev": {
23+
"phpunit/phpunit": "~4.4",
24+
"squizlabs/php_codesniffer": "~2.0"
25+
},
2626
"autoload": {
2727
"psr-4": {
2828
"SendGrid\\": "lib/"

examples/example.php

Lines changed: 24 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -10,69 +10,51 @@
1010
$headers = ['Authorization: Bearer ' . $apiKey];
1111
$client = new SendGrid\Client('https://api.sendgrid.com', $headers, '/v3');
1212

13-
// GET Collection
14-
$query_params = ['limit' => 100, 'offset' => 0];
15-
$request_headers = ['X-Mock: 200'];
16-
$response = $client->api_keys()->get(null, $query_params, $request_headers);
13+
// GET /v3/api_keys - retrieve all API Keys that belong to the user
14+
$queryParams = ['limit' => 100, 'offset' => 0];
15+
$requestHeaders = ['X-Mock: 200'];
16+
$response = $client->api_keys()->get(null, $queryParams, $requestHeaders);
1717
echo $response->statusCode();
1818
echo $response->body();
1919
echo $response->headers();
2020

21-
// GET with auto retry on rate limit
22-
$query_params = ['limit' => 100, 'offset' => 0];
23-
$request_headers = ['X-Mock: 200'];
24-
$retryOnLimit = true;
25-
$response = $client->api_keys()->get(null, $query_params, $request_headers, $retryOnLimit);
26-
echo $response->statusCode();
27-
echo $response->body();
28-
echo $response->headers();
21+
// GET /v3/api_keys - retrieve all API Keys that belong to the user
22+
$queryParams = ['limit' => 100, 'offset' => 0];
23+
$requestHeaders = ['X-Mock: 200'];
24+
$retryOnLimit = true; // with auto retry on rate limit
25+
$response = $client->api_keys()->get(null, $queryParams, $requestHeaders, $retryOnLimit);
2926

30-
// POST
31-
$request_body = [
27+
// POST /v3/api_keys - create a new user API Key
28+
$requestBody = [
3229
'name' => 'My PHP API Key',
3330
'scopes' => [
3431
'mail.send',
3532
'alerts.create',
3633
'alerts.read'
3734
]
3835
];
39-
$response = $client->api_keys()->post($request_body);
40-
echo $response->statusCode();
41-
echo $response->body();
42-
echo $response->headers();
43-
$response_body = json_decode($response->body());
44-
$api_key_id = $response_body->api_key_id;
36+
$response = $client->api_keys()->post($requestBody);
37+
$responseBody = json_decode($response->body(), true);
38+
$apiKeyId = $responseBody['api_key_id'];
4539

46-
// GET Single
47-
$response = $client->version('/v3')->api_keys()->_($api_key_id)->get();
48-
echo $response->statusCode();
49-
echo $response->body();
50-
echo $response->headers();
40+
// GET /v3/api_keys/{api_key_id} - retrieve a single API Key
41+
$response = $client->api_keys()->_($apiKeyId)->get();
5142

52-
// PATCH
53-
$request_body = [
43+
// PATCH /v3/api_keys/{api_key_id} - update the name of an existing API Key
44+
$requestBody = [
5445
'name' => 'A New Hope'
5546
];
56-
$response = $client->api_keys()->_($api_key_id)->patch($request_body);
57-
echo $response->statusCode();
58-
echo $response->body();
59-
echo $response->headers();
47+
$response = $client->api_keys()->_($apiKeyId)->patch($requestBody);
6048

61-
// PUT
62-
$request_body = [
49+
// PUT /v3/api_keys/{api_key_id} - update the name and scopes of a given API Key
50+
$requestBody = [
6351
'name' => 'A New Hope',
6452
'scopes' => [
6553
'user.profile.read',
6654
'user.profile.update'
6755
]
6856
];
69-
$response = $client->api_keys()->_($api_key_id)->put($request_body);
70-
echo $response->statusCode();
71-
echo $response->body();
72-
echo $response->headers();
57+
$response = $client->api_keys()->_($apiKeyId)->put($requestBody);
7358

74-
// DELETE
75-
$response = $client->api_keys()->_($api_key_id)->delete();
76-
echo $response->statusCode();
77-
echo $response->body();
78-
echo $response->headers();
59+
// DELETE /v3/api_keys/{api_key_id} - revoke an existing API Key
60+
$response = $client->api_keys()->_($apiKeyId)->delete();

0 commit comments

Comments
 (0)