2017-06-16 89 views
1

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

+0

看起来你不正确地引用了'HttpRequest'的命名空间。 – Ohgodwhy

+0

@Ohgodwhy应该是“App \ HttpRequest \ Controllers”吗? – zhiyu

+0

该工厂是解析内部请求,如果你要获取外部API使用类似Guzzle – Ohgodwhy

回答

1

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

通过作曲家安装

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();

沿着这些线的东西,实际上没有检查它是否运行正常。

+0

它的工作!非常感谢!!! – zhiyu

+0

欢迎您:) – Marcin

+0

如果我想包含带有一些内容的'$ request-> setBody('{}')',让我们假设''{“subscription”:{“next_billing_at”:'。 $ nextBilling。'}}'',我应该在代码中添加一行还是两行?谢谢! – zhiyu