2017-06-18 27 views
0
  • 已更新 - JSON文件將返回,但根本不會更改開票日期。(已更新)Laravel PUT方法不起作用

  • 參考文獻1:關於changing the billing date的官方文檔。

  • 定2:詳細的示例代碼:

    <?php 
    
    $request = new HttpRequest(); 
    $request->setUrl('https://domain.chargify.com/subscriptions/$subscriptionId.json'); 
    $request->setMethod(HTTP_METH_PUT); 
    
    $request->setHeaders(array(
        'authorization' => 'Basic YXBpa2V5Og==', 
        'content-type' => 'application/json' 
    )); 
    
    $request->setBody('{"subscription":{"next_billing_at":"2028-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", 
    ["json" => [ 
    [ "subscription" => 
     [ "next_billing_at" => $nextBilling ] 
    ] 
] 
]); 

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

盡我所能... – perror

回答

0

非常感謝大家幫助我。

我一直在處理這個問題2天,它應該是對我來說。最終,他們的API誤導了我。

我們需要做的唯一的事情,僅僅是改變

'body' => "{\"subscription\":{\"next_billing_at\":\"$nextBilling\"}}" 

增加了幾個 '\' S的內部。

謝謝大家幫助我,祝你有個美好的一天!

1

你正在建設的URL不正確。不應該有一個嘗試改變邏輯,使狂飲這樣調用/.json

之間$subscription

變化

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

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

編輯

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

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

$client = new Client(); 

$res = $client->put("https://$hostname/subscriptions/$subscriptionId.json", 
    [ 
     'headers' => $headers, 
     'body' => json_encode($body) 
    ] 
); 

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

謝謝你,我更新了問題,現在它根本無法工作(它會返回json文件,但根本沒有更改訂閱日期)。你能找我嗎? – zhiyu

+0

嘗試我的更新回答 – ayip

+0

不幸的是,雖然我得到了JSON文件,但開票日期仍然相同。這可能是JSON發回給我們的'身體'問題,我不能說如何。 – zhiyu