更新 - 我试图使用API文档来改变使用PUT方法在Laravel中的Http和Guzzle中的开票日期,但是,JSON文件将返回,但它不会更改在所有。Laravel PUT方法不起作用
- 参考文献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”' – Hollings
是的,它原来不是一个数组,它是:''{“subscription”:{“next_billing_at”: “2018-12-15”}}'',我也尝试过这种方式,但仍然无法工作到目前为止。 – zhiyu
好的。我注意到的另外一件事情,你可能必须用'$ nextBilling-> toDateString()'来格式化Carbon日期。这将把它从碳日期对象转换为YYYY-MM-DD字符串格式 – Hollings