我的订阅控制器中有一个公共函数,用于从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