【发布时间】:2017-06-21 13:20:44
【问题描述】:
已更新 - 我正在尝试使用 API 文档在 Laravel 的 Http 和 Guzzle 中使用 PUT 方法更改计费日期,但是 JSON 文件会返回,但它根本不会更改计费日期。
- 参考1:changing the billing date的官方文档。
-
参考2:他们的示例代码详细(抱歉格式错误):
<?php $request = new HttpRequest(); $request->setUrl('https://subdomain.chargify.com/subscriptions/subscriptionId.json'); $request->setMethod(HTTP_METH_PUT); $request->setHeaders(array('content-type' => 'application/json')); $request->setBody('{"subscription":{"next_billing_at":"2018-12-15"}}'); try { $response = $request->send(); echo $response->getBody(); } catch (HttpException $ex) { echo $ex; }
我的代码详解:
public function changeYearlySubscriptionBillingDate(Request $request)
{
$user = $request->user();
$subscriptionId = $user->subscription->subscription_id;
$nextBilling = Carbon::now()->addYear();
$hostname = env('CHARGIFY_HOSTNAME');
$headers = [
'authorization' => 'Basic ANIDIANDIAJIJCQ',
'content-type' => 'application/json'
];
$body = ["subscription" => ["next_billing_at" =>[ $nextBilling ]]];
$config = [
'headers' => $headers,
'form_param' => $body
];
$client = new Client($config);
$res = $client->put("https://$hostname/subscriptions/$subscriptionId.json");
echo $res->getBody();
}
【问题讨论】:
-
代码中的
"next_billing_at" =>[ $nextBilling ]中的[ $nextBilling ]应该是一个数组吗?示例中似乎没有:"next_billing_at":"2018-12-15" -
是的,最初它不是一个数组,它是:
'{"subscription":{"next_billing_at":"2018-12-15"}}',我也尝试过,但到目前为止仍然无法正常工作。 -
好的。我注意到的另一件事是,您可能必须使用
$nextBilling->toDateString()格式化您的 Carbon 日期。这会将它从 Carbon 日期对象转换为 YYYY-MM-DD 字符串格式 -
是的,感谢您的通知,但这也不是这个问题,因为他们的 API 支持 YYYY-MM-DD 和更精确的时间戳:)