2017-06-20 42 views
1

更新 - 我試圖使用API​​文檔來改變使用PUT方法在Laravel中的Http和Guzzle中的開票日期,但是,JSON文件將返回,但它不會更改在所有。Laravel PUT方法不起作用

  • 參考文獻1:關於changing the billing date的官方文檔。
  • 定2:詳細的示例代碼(約壞格式不好意思):

    <?php 
    
    $request = new HttpRequest(); 
    $request->setUrl('https://subdomain.chargify.com/subscriptions/subscriptionId.json'); 
    $request->setMethod(HTTP_METH_PUT); 
    
    $request->setHeaders(array('content-type' => 'application/json')); 
    
    $request->setBody('{"subscription":{"next_billing_at":"2018-12-15"}}'); 
    
    try { 
         $response = $request->send(); 
    
         echo $response->getBody(); 
    } catch (HttpException $ex) { 
         echo $ex; 
    } 
    

我的詳細代碼:

public function changeYearlySubscriptionBillingDate(Request $request) 
{ 
    $user = $request->user(); 
    $subscriptionId = $user->subscription->subscription_id; 
    $nextBilling = Carbon::now()->addYear(); 
    $hostname = env('CHARGIFY_HOSTNAME'); 

    $headers = [ 
     'authorization' => 'Basic ANIDIANDIAJIJCQ', 
     'content-type' => 'application/json' 
    ]; 

    $body = ["subscription" => ["next_billing_at" =>[ $nextBilling ]]]; 

    $config = [ 
     'headers' => $headers, 
     'form_param' => $body 
    ]; 

    $client = new Client($config); 

    $res = $client->put("https://$hostname/subscriptions/$subscriptionId.json"); 

    echo $res->getBody(); 
} 
+0

''next_billing_at「=> [$ nextBilling]'中的'[$ nextBilling]'應該是一個數組嗎?它看起來並不像這樣:'「next_billing_at」:「2018-12-15」' – Hollings

+0

是的,它原來不是一個數組,它是:''{「subscription」:{「next_billing_at」: 「2018-12-15」}}'',我也嘗試過這種方式,但仍然無法工作到目前爲止。 – zhiyu

+0

好的。我注意到的另外一件事情,你可能必須用'$ nextBilling-> toDateString()'來格式化Carbon日期。這將把它從碳日期對象轉換爲YYYY-MM-DD字符串格式 – Hollings

回答

0

更改此:

echo $response->getBody(); 

dd($response->getBody()); 

並重新發送響應數據。

+0

'流{#726▼ -stream:流資源@ 11▼ wrapper_type: 「PHP」 的stream_type: 「TEMP」 模式: 「W + b」 的 unread_bytes:0 可查找:真 URI:「PHP: // TEMP」 選擇:[] } -size:空 -seekable:真 可讀:真 -writable:真 -uri: 「PHP:// TEMP」 -customMetadata:[] }' – zhiyu

+0

嘗試'dd($ res-> getBody());'而不是。 – Hollings

+0

我確實使用了'dd($ res-> getBody());'並且它與上面給出的信息相同。 :) – zhiyu