Laravel客户端错误:`PUT'导致了“404 Not Found”响应:

时间:2017-06-16 19:45:35

标签: php laravel chargify

我的订阅控制器中有一个公共功能,用于从chargify中删除订阅的待处理状态。

从他们的文档中,使用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

1 个答案:

答案 0 :(得分:1)

它看起来像是一个基本的DELETE调用。您可以使用Guzzle

通过composer

安装
composer require guzzlehttp/guzzle:~6.0

然后将其包含在您的控制器中:

use GuzzleHttp\Client;

最后使用它:

$config = [
            'headers' => [
               'authorization' => 'Basic YXBpLWtleTp4'
         ]
];

$client  = new Client($config); 
$res = $client->delete("https://subdomain.chargify.com/subscriptions/$subscriptionId/delayed_cancel.json",
  [ "json" =>
    [
      [ "subscription" => ["next_billing_at" => $nextBilling]] 
    ]
  ]
);
echo $res->getBody();

沿着这些方向做的事情,实际上并没有检查它是否正常运行。