|
| 1 | +# Usage |
| 2 | + |
| 3 | +Usage examples for SendGrid php-http-client |
| 4 | + |
| 5 | +## Initialization |
| 6 | + |
| 7 | +``` |
| 8 | +// If running this outside of this context, use the following include and |
| 9 | +// comment out the two includes below |
| 10 | +// require __DIR__ . '/vendor/autoload.php'; |
| 11 | +include(dirname(__DIR__) . '/lib/Client.php'); |
| 12 | +// This gets the parent directory, for your current directory use getcwd() |
| 13 | +$path_to_config = dirname(__DIR__); |
| 14 | +$apiKey = getenv('SENDGRID_API_KEY'); |
| 15 | +$headers = ['Authorization: Bearer ' . $apiKey]; |
| 16 | +$client = new SendGrid\Client('https://api.sendgrid.com', $headers, '/v3'); |
| 17 | +``` |
| 18 | + |
| 19 | +## Table of Contents |
| 20 | + |
| 21 | +- [GET](#get) |
| 22 | +- [DELETE](#delete) |
| 23 | +- [POST](#post) |
| 24 | +- [PUT](#put) |
| 25 | +- [PATCH](#patch) |
| 26 | + |
| 27 | +<a name="get"></a> |
| 28 | +## GET |
| 29 | + |
| 30 | +#### GET Collection |
| 31 | + |
| 32 | +``` |
| 33 | +$query_params = ['limit' => 100, 'offset' => 0]; |
| 34 | +$request_headers = ['X-Mock: 200']; |
| 35 | +$response = $client->api_keys()->get(null, $query_params, $request_headers); |
| 36 | +echo $response->statusCode(); |
| 37 | +echo $response->body(); |
| 38 | +echo $response->headers(); |
| 39 | +``` |
| 40 | + |
| 41 | +#### GET with auto retry on rate limit |
| 42 | + |
| 43 | +``` |
| 44 | +$query_params = ['limit' => 100, 'offset' => 0]; |
| 45 | +$request_headers = ['X-Mock: 200']; |
| 46 | +$retryOnLimit = true; |
| 47 | +$response = $client->api_keys()->get(null, $query_params, $request_headers, $retryOnLimit); |
| 48 | +echo $response->statusCode(); |
| 49 | +echo $response->body(); |
| 50 | +echo $response->headers(); |
| 51 | +``` |
| 52 | + |
| 53 | +<a name="delete"></a> |
| 54 | +## DELETE |
| 55 | + |
| 56 | +``` |
| 57 | +$response = $client->api_keys()->_($api_key_id)->delete(); |
| 58 | +echo $response->statusCode(); |
| 59 | +echo $response->body(); |
| 60 | +echo $response->headers(); |
| 61 | +``` |
| 62 | + |
| 63 | +<a name="post"></a> |
| 64 | +## POST |
| 65 | + |
| 66 | +``` |
| 67 | +$request_body = [ |
| 68 | + 'name' => 'My PHP API Key', |
| 69 | + 'scopes' => [ |
| 70 | + 'mail.send', |
| 71 | + 'alerts.create', |
| 72 | + 'alerts.read' |
| 73 | + ] |
| 74 | +]; |
| 75 | +$response = $client->api_keys()->post($request_body); |
| 76 | +echo $response->statusCode(); |
| 77 | +echo $response->body(); |
| 78 | +echo $response->headers(); |
| 79 | +$response_body = json_decode($response->body()); |
| 80 | +$api_key_id = $response_body->api_key_id; |
| 81 | +``` |
| 82 | +<a name="put"></a> |
| 83 | +## PUT |
| 84 | + |
| 85 | +``` |
| 86 | +$request_body = [ |
| 87 | + 'name' => 'A New Hope', |
| 88 | + 'scopes' => [ |
| 89 | + 'user.profile.read', |
| 90 | + 'user.profile.update' |
| 91 | + ] |
| 92 | +]; |
| 93 | +$response = $client->api_keys()->_($api_key_id)->put($request_body); |
| 94 | +echo $response->statusCode(); |
| 95 | +echo $response->body(); |
| 96 | +echo $response->headers(); |
| 97 | +``` |
| 98 | +<a name="patch"></a> |
| 99 | +## PATCH |
| 100 | + |
| 101 | +``` |
| 102 | +$request_body = [ |
| 103 | + 'name' => 'A New Hope' |
| 104 | +]; |
| 105 | +$response = $client->api_keys()->_($api_key_id)->patch($request_body); |
| 106 | +echo $response->statusCode(); |
| 107 | +echo $response->body(); |
| 108 | +echo $response->headers(); |
| 109 | +``` |
0 commit comments