douqie6454 2017-06-18 15:20
浏览 171
已采纳

(更新)Laravel PUT方法不起作用

  • updated - the JSON file would return but it will not change the billing date at all.

  • Reference 1: The official documentation about changing the billing date.

  • Reference2: their sample code in detail:

    <?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;
    }
    

My code in detail:

   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();
}
  • 写回答

2条回答 默认 最新

  • dongwei5794 2017-06-20 19:37
    关注

    thanks everyone so much for helping me out.

    I've been working on this problem for 2 days and it was supposed to be right to me. Eventually, it is their API that misled me.

    the only thing we need to do, is simply change

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

    added a few '\' s inside.

    Thanks everyone for helping me out, have a good day!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 高价求中通快递查询接口
  • ¥15 解决一个加好友限制问题 或者有好的方案
  • ¥15 关于#java#的问题,请各位专家解答!
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?