2017-06-20 142 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