我正在尝试通过 Sage Accounting API 调用创建销售发票(其文档可在此处找到:https ://developer.sage.com/api/accounting/api/ )
为了使我的代码更清晰,我创建了一个类来帮助我相应地进行这些调用。
这是我用来进行这些调用的方法:
public function postRequest()
{
$url = $this->baseEndpoint . $this->request;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
if (isset($this->params)) {
curl_setopt($ch, CURLOPT_POSTFIELDS, $this->params);
}
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Authorization: Bearer $this->token",
"Host: api.accounting.sage.com",
"Content-Type: application/json"
));
$response = json_decode(curl_exec($ch), true);
curl_close($ch);
return $response;
}
我如何调用此方法:
$params = array(
"sales_invoice" => array(
"contact_id" => "485fdfe0be154f9c9af44351de16e5be",
"date" => "2019-06-13",
"invoice_lines" => array(
array(
"description" => "description",
"ledger_account_id" => "f04157c90ff0496ab3a22f2558e46010",
"unit_price" => 10 ,
"quantity" => 1,
"tax_rate_id" => "ES_RE_STANDARD",
"tax_rate" => 0.1
)
)
)
);
$params = json_encode($params);
$request = "v3.1/sales_invoices";
$sageRequest = new SageRequest($token, $request, $params);
$sageRequest->postRequest();
根据 API 文档,这应该可以,但我仍然收到此错误:
[$severity] => error
[$dataCode] => UnexpectedError
[$message] => An unexpected error occurred.
[$source] =>
如果有人对 Sage Accounting API 有一些经验,我会非常感激知道我做错了什么。