@@ -37,9 +37,48 @@ If you use Composer, these dependencies should be handled automatically.
3737Get your ConvertKit API Key and API Secret [ here] ( https://app.convertkit.com/account/edit ) and set it somewhere in your application.
3838
3939``` php
40- $api = new \ConvertKit_API\ConvertKit_API($api_key, $api_secret);
40+ // Require the autoloader (if you're using a PHP framework, this may already be done for you).
41+ require_once 'vendor/autoload.php';
42+
43+ // Initialize the API class.
44+ $api = new \ConvertKit_API\ConvertKit_API('<your _public_api_key >', '<your _secret_api_key >');
45+ ```
46+
47+ ## Handling Errors
48+
49+ The ConvertKit PHP SDK uses Guzzle for all HTTP API requests. Errors will be thrown as Guzzle's ` ClientException ` (for 4xx errors),
50+ or ` ServerException ` (for 5xx errors).
51+
52+ ``` php
53+ try {
54+ $forms = $api->add_subscriber_to_form('invalid-form-id');
55+ } catch (GuzzleHttp\Exception\ClientException $e) {
56+ // Handle 4xx client errors.
57+ die($e->getMessage());
58+ } catch (GuzzleHttp\Exception\ServerException $e) {
59+ // Handle 5xx server errors.
60+ die($e->getMessage());
61+ }
62+ ```
63+
64+ For a more detailed error message, it's possible to fetch the API's response when a ` ClientException ` is thrown:
65+
66+ ``` php
67+ // Errors will be thrown as Guzzle's ClientException or ServerException.
68+ try {
69+ $forms = $api->form_subscribe('invalid-form-id');
70+ } catch (GuzzleHttp\Exception\ClientException $e) {
71+ // Handle 4xx client errors.
72+ // For ClientException, it's possible to inspect the API's JSON response
73+ // to output an error or handle it accordingly.
74+ $error = json_decode($e->getResponse()->getBody()->getContents());
75+ die($error->message); // e.g. "Entity not found".
76+ } catch (GuzzleHttp\Exception\ServerException $e) {
77+ // Handle 5xx server errors.
78+ die($e->getMessage());
79+ }
4180```
4281
4382## Documentation
4483
45- See the [ PHP SDK docs] ( DOCUMENTATION. md)
84+ See the [ PHP SDK docs] ( ./docs/classes/ConvertKit_API/ConvertKit_API. md)
0 commit comments