我的訂閱控制器中有一個公共函數,用於從chargify中刪除訂閱的掛起狀態。Laravel客戶端錯誤:`PUT'導致404 Not Found`響應:
從他們的文檔,使用PECL/HTTP1方法的代碼應該是這樣的:
$request = new HttpRequest();
$request->setUrl('https://subdomain.chargify.com/subscriptions/$subscriptionId/delayed_cancel.json');
$request->setMethod(HTTP_METH_DELETE);
$request->setHeaders(array(
'authorization' => 'Basic YXBpLWtleTp4'
));
$request->setBody('{}');
try {
$response = $request->send();
echo $response->getBody();
} catch (HttpException $ex) {
echo $ex;
}
所以我把這個代碼我的功能
public function changeYearlySubscriptionBillingDate(Request $request)
{
$chargify = new \Crucial\Service\Chargify([
'hostname' => env('CHARGIFY_HOSTNAME'),
'api_key' => env('CHARGIFY_KEY'),
'shared_key' => env('CHARGIFY_SHARED_KEY')
]);
$subscription = $chargify->subscription();
$user = $request->user();
$subscriptionId = $user->subscription->subscription_id;
$nextBilling = Carbon::now()->addYear();
$hostname = env('CHARGIFY_HOSTNAME');
$config = [
'headers' => [
'authorization' => 'Basic YXBpLWtleTp4',
'content-type' => 'application/json'
]
];
$client = new Client($config);
$res = $client->put("https://$hostname/subscriptions/$subscriptionId/.json",
["json" => [
[ "subscription" =>
[ "next_billing_at" => $nextBilling ]
]
]]);
echo $res->getBody();
}
內因爲我是新來的Laravel,這文檔一般是針對PHP的,我不確定如何在Laravel框架內轉換此代碼。
參考:
錯誤消息:
Client error: `PUT https://(the hostname)/subscriptions/(the id)/.json` `resulted in a `404 Not Found` response:`
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>404 File Not Found</title>
<style>
b (truncated...)
定2:官方文檔
https://reference.chargify.com/v1/subscriptions/update-subscription-calendar-billing-day-change
看起來你不正確地引用了'HttpRequest'的命名空間。 – Ohgodwhy
@Ohgodwhy應該是「App \ HttpRequest \ Controllers」嗎? – zhiyu
該工廠是解析內部請求,如果你要獲取外部API使用類似Guzzle – Ohgodwhy