2017-06-16 179 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

相關問題