(更新)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();
}

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!

The url you are building is incorrect. There shouldn't be a / between $subscription and .json

Change

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

to

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

EDIT

Try changing the logic to make Guzzle call like this.

$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();