classy-org/constantcontact-php-client

此软件包最新版本(1.1.0)没有可用的许可信息。

围绕 Guzzle Http Client 的包装,用于与 Constant Contact APIv2 交互

1.1.0 2018-05-01 18:15 UTC

This package is auto-updated.

Last update: 2024-09-12 03:37:06 UTC


README

围绕 Guzzle Http Client 的包装,用于与 Constant Contact APIv2 交互

安装

ConstantContact API php 客户端可以通过 Composer 安装

composer require classy-org/constantcontact-php-client

确保您在应用程序中包含了 composer 自动加载器

require_once '/path/to/your/project/vendor/autoload.php';

用法

// Instantiate the client
$client = new \Classy\ConstantContactClient('API-KEY', 'ACCESS-TOKEN');

// Make a request. 
$httpResponse = $client->request('GET', 'contacts');
$contacts = json_decode($httpResponse->getBody()->getContents());

//Or Grab Data Quickly.
$contacts = $client->getData('contacts');

//Store a Contact Quickly.
$payload = [
    'lists' => [
        [
            'id' => (string)1
        ]
    ],
    'email_addresses' => [
        [
            'email_address' => (string)'person@constantcontact.com'
        ]
    ],
    'first_name' => (string)'My',
    'last_name'  => (string)'Name',
];
$contacts = $client->addContact($payload);

异常处理

此客户端使用 Guzzle Http client。当 Http 响应不是 200 (OK) 时,会抛出异常

try {
    $response = $client->request('POST', 'path/to/route', ['body' => ['check' => 1]]);
} catch (Exception $e) {
    if ($e instanceof \GuzzleHttp\Exception\ConnectException) {
        // there was a networking error
    }

    if ($e instanceof \GuzzleHttp\Exception\ClientException) {
        // Mailchimp API returned a 4xx response.
        $httpStatusCode = $e->getCode();
        if ($httpStatusCode == 404) {
            // resource doesn't exist
        }
        if ($httpStatusCode == 401) {
            // you're unauthorized (api key must be invalid)
        }
        if ($httpStatusCode == 403) {
            // you're not allowed to request this endpoint
        }
        if ($httpStatusCode == 400) {
            // body payload is invalid
        }
        if (...) {
            //
        }

        $bodyResponse = $e->getResponse()->getBody()->getContents();
    }

    if ($e instanceof \GuzzleHttp\Exception\ServerException) {
        // ConstantContact returned a 5xx response, which means they experience technical difficulties.
    }
}