m1ke/xero

旧版Xero OAuth私有应用程序API的分支

dev-master 2014-06-17 13:16 UTC

This package is auto-updated.

Last update: 2024-09-13 21:36:47 UTC


README

这是Xero PHP库早期版本的分支,主要是因为团队不再维护它(见下文)。我还将它添加到了Packagist,这意味着它可以与Composer一起安装。

不再维护

请注意,PHP-Xero包装库不再处于活跃开发状态。所有开发努力都投入到XeroOAuth-PHP库

简介

一个用于与xero(xero.com)私有应用程序API交互的类。它也可以用于公共应用程序API,但尚未对其进行测试。更多Xero文档可以在http://blog.xero.com/developer/api-overview/找到。建议在使用此类之前熟悉API,否则它可能对您来说没有太多意义 - http://blog.xero.com/developer/api/

感谢Andy Smith提供的Oauth*类,更多关于它们的信息可以在http://oauth.googlecode.com/找到。OAuthSignatureMethod_Xero类是我根据Oauth类的要求编写的。ArrayToXML类来自wwwzealdcom的工作,如2009年8月30日在此页面上所示:http://snipplr.com/view/3491/convert-php-array-to-xml-or-simple-xml-object-if-you-wish/我对该代码进行了一些小的修改以克服一些错误。

需求

PHP5+

作者

Ronan Quirke,Xero(只有一些非常小的错误修复,大部分工作由David Pitman完成)

M1ke的Composer包

安装

composer.phar require m1ke/xero

用法

基本上,使用您的凭据和所需的输出格式实例化Xero类。然后调用API中概述的任何方法。将API方法名作为属性调用与调用不带选项的该API方法相同。将API方法作为仅有一个数组作为输入参数的方法调用,类似于调用相应的POST或PUT API方法。您可以使用方法上的最多四个参数来创建更复杂的GET请求。如果您已经阅读了xero API文档,它应该很清楚。

GET请求用法

从Xero检索结果集涉及识别您要访问的端点,以及可选地,设置一些参数以进一步过滤结果集。有5个可能的参数

  1. 记录过滤器:第一个参数可以是布尔值"false"或唯一的资源标识符:文档ID或唯一编号,例如:$xero->Invoices('INV-2011', false, false, false, false);
  2. 自何时起:第二个参数可以是日期/时间过滤器,以仅返回自某个日期/时间以来修改的数据,例如:$xero->Invoices(false, "2012-05-11T00:00:00");
  3. 自定义过滤器:一个过滤器数组,数组键是过滤器字段(操作符左侧),数组值是操作符右侧的值。数组值可以是字符串或array(操作符, 值),或布尔值,例如:$xero->Invoices(false, false, $filterArray);
  4. 按什么排序:设置结果集的排序,例如:$xero->Invoices('', '', '', 'Date', '');
  5. 接受类型:仅在您想检索文档的PDF版本时需要设置,例如:$xero->Invoices($invoice_id, '', '', '', 'pdf');

关于过滤GET请求的详细信息请见此处:http://blog.xero.com/developer/api-overview/http-get/

示例用法

<?php

// Include the class file
include_once "xero.php";

// Define your application key and secret (find these at https://api.xero.com/Application)
define('XERO_KEY','[APPLICATION KEY]');
define('XERO_SECRET','[APPLICATION SECRET]');

// Instantiate the Xero class with your key, secret and paths to your RSA cert and key
// The last argument is optional and may be either "xml" or "json" (default)
// "xml" will give you the result as a SimpleXMLElement object, while 'json' will give you a plain array object
$xero = new Xero(XERO_KEY, XERO_SECRET, '[path to public certificate]', '[path to private key]', 'xml' );

if($_REQUEST['sample']=="") {

	// The input format for creating a new contact see http://blog.xero.com/developer/api/contacts/ to understand more
	$new_contact = array(
		array(
			"Name" => "API TEST Contact",
			"FirstName" => "TEST",
			"LastName" => "Contact",
			"Addresses" => array(
				"Address" => array(
					array(
						"AddressType" => "POBOX",
						"AddressLine1" => "PO Box 100",
						"City" => "Someville",
						"PostalCode" => "3890"
					),
					array(
						"AddressType" => "STREET",
						"AddressLine1" => "1 Some Street",
						"City" => "Someville",
						"PostalCode" => "3890"
					)
				)
			)
		)
	);

	// Create the contact
	$contact_result = $xero->Contacts( $new_contact );

	// The input format for creating a new invoice (or credit note) see http://blog.xero.com/developer/api/invoices/
	$new_invoice = array(
		array(
			"Type"=>"ACCREC",
			"Contact" => array(
				"Name" => "API TEST Contact"
			),
			"Date" => "2010-04-08",
			"DueDate" => "2010-04-30",
			"Status" => "AUTHORISED",
			"LineAmountTypes" => "Exclusive",
			"LineItems"=> array(
				"LineItem" => array(
					array(
						"Description" => "Just another test invoice",
						"Quantity" => "2.0000",
						"UnitAmount" => "250.00",
						"AccountCode" => "200"
					)
				)
			)
		)
	);

	// The input format for creating a new payment see http://blog.xero.com/developer/api/payments/ to understand more
	$new_payment = array(
		array(
			"Invoice" => array(
				"InvoiceNumber" => "INV-0002"
			),
			"Account" => array(
				"Code" => "[account code]"
			),
			"Date" => "2010-04-09",
			"Amount"=>"100.00",
		)
	);


	// Raise an invoice
	$invoice_result = $xero->Invoices( $new_invoice );

	$payment_result = $xero->Payments( $new_payment );

	// Get details of an account, with the name "Test Account"
	$result = $xero->Accounts(false, false, array("Name"=>"Test Account") );
	// The params above correspond to the "Optional params for GET Accounts" on http://blog.xero.com/developer/api/accounts/

	// To do a POST request, the first and only param must be a multidimensional array as shown above in $new_contact etc.

	// Get details of all accounts
	$all_accounts = $xero->Accounts;

	// Echo the results back
	if ( is_object($result) ) {
		// Use this to see the source code if the $format option is "xml"
		echo htmlentities($result->asXML()) . "<hr />";
	} else {
		// Use this to see the source code if the $format option is "json" or not specified
		echo json_encode($result) . "<hr />";
	}

}

if($_REQUEST['sample']=="pdf"){

	// first get an invoice number to use

	$org_invoices = $xero->Invoices;

	$invoice_count = sizeof($org_invoices->Invoices->Invoice);

	$invoice_index = rand(0,$invoice_count);

	$invoice_id = (string) $org_invoices->Invoices->Invoice[$invoice_index]->InvoiceID;

	if(!$invoice_id) {
		echo "You will need some invoices for this...";
	}

	// Now retrieve that and display the pdf
	$pdf_invoice = $xero->Invoices($invoice_id, '', '', '', 'pdf');

	header('Content-type: application/pdf');
	header('Content-Disposition: inline; filename="the.pdf"');

	echo ($pdf_invoice);
}