CheddarGetter API 的官方 PHP 客户端

1.2.1 2020-10-18 13:08 UTC

This package is auto-updated.

Last update: 2024-09-18 21:08:31 UTC


README

通过 Composer

  1. 获取 Composer
  2. 学习 Composer
  3. 在项目的 composer.json 文件中定义需求
"require": {
	"cheddar-getter/client": "*"
}
  1. 从您的命令行运行 composer install
  2. 请确保您已安装 cUrl 扩展或 Zend_Http_Client (ZF1) 可通过自动加载获得。我们的 composer.json 仅建议这些包,除非您自己构建 HTTP 适配器(请参阅下文),否则一个或另一个是必需的。

作为 Git 子模块

git clone git://github.com/marcguyer/cheddargetter-client-php.git /path/to/includepath/CheddarGetter

在运行上述命令之前,'CheddarGetter' 目录必须不存在。

只需下载即可,拜托

下载 并解压文件,将它们放入名为 /path/to/includepath/CheddarGetter 的目录中

基本用法

实例化客户端对象

<?php
	$client = new CheddarGetter_Client(
		'https://[theurlforcheddargetter.com]',
		'[yourusername]',
		'[yourpassword]',
		'[yourproductcode]'
	);
?>

创建客户

<?php
  $data = array(
    'code'      => 'EXAMPLE_CUSTOMER',
    'firstName' => 'Example',
    'lastName'  => 'Customer',
    'email'     => 'example_customer@example.com',
    'subscription' => array(
      'planCode'      => 'THE_PLAN_CODE',
      'ccFirstName'   => 'Example',
      'ccLastName'    => 'Customer',
      'ccNumber'      => '4111111111111111',
      'ccExpiration'  => '04/2011'
    )
  );

  $customer = $client->newCustomer($data);
	print_r($customer->toArray());
	echo $customer->toJson();
?>

获取客户

<?php
	$customer = $client->getCustomer('EXAMPLE_CUSTOMER');
	print_r($customer->toArray());
	echo $customer->toJson();
?>

错误处理

CheddarGetter_Client 抛出包含错误信息的 CheddarGetter_Client_Exception

CheddarGetter_Response 抛出包含错误信息的 CheddarGetter_Response_Exception

如果 CheddarGetter API 返回错误文档,则会抛出 CheddarGetter_Response_Exception。这可能在正常操作过程中发生,异常中的错误数据应展示给用户或按照您的规格进行处理。以下是一个快速且简单的示例

<?php
$data = array(
  'code'      => 'EXAMPLE_CUSTOMER',
  'firstName' => 'Example',
  'lastName'  => 'Customer',
  'email'     => 'example_customer@example.com',
  'subscription' => array(
    'planCode' => 'A_PAID_PLAN',
    'ccFirstName'   => 'Example',
    'ccLastName'        => 'Customer',
    'ccNumber'      => '4111111111111111',
    'ccExpiration'  => '04/2011'
  )
);
try {
	$customer = $client->newCustomer($data);
	// under some circumstances, the customer is created but
	// contains an embedded error that needs to be handled
	$customer->handleEmbeddedErrors();
} catch (CheddarGetter_Response_Exception $re) {
  die($re->getCode() . '-' . $re->getAuxCode() . ': ' . $re->getMessage());
}
?>

有关错误的更多信息,请参阅 错误处理知识库文章。您可以并应该通过 模拟可能的错误 进行测试。

高级用法

使用了适配器模式(感谢 https://github.com/stof),因此您可以指定自己的 http 适配器和/或超级全局访问适配器。默认内置适配器使用 cUrl 进行 http 通信,并直接访问超级全局变量。还包括与 Zend Framework 兼容的适配器。如果您已创建自己的适配器并希望包含在内,只需从您的分支发送拉取请求即可。以下是一个使用自定义 HTTP 适配器的示例

<?php
	$client = new CheddarGetter_Client(
		'https://theurlforcheddargetter.com',
		'[yourusername]',
		'[yourpassword]',
		'[yourproductcode]',
		new MyHTTPAdapter()
	);
	$customers = $client->getCustomers();
	print_r($customers->toArray());
	echo $customers->toJson();
?>

指定自定义 HTTP 适配器

<?php
	CheddarGetter_Client::setHttpClient(
		new MyCustomClient()
	);
?>

指定自定义超级全局访问适配器

<?php
	CheddarGetter_Client::setRequestAdapter(
		new MyCustomAdapter()
	);
?>

贡献

编写代码

欢迎贡献!请从我们的仓库分叉,编写并提交一个拉取请求。有关通过 GitHub 贡献开源项目的更多信息,请参阅 GitHub 贡献指南

提问

支持论坛

提问的最佳方式是通过 CheddarGetter 支持论坛

GitHub 问题

如果您的问题是技术性的或您认为您发现了一个错误,欢迎您打开 新问题

文档

在 /docs 目录或 GitHub 上查看: http://marcguyer.github.io/cheddargetter-client-php/

此外,原始 API 文档在此: http://cheddargetter.com/developers