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

时间:2017-06-18 15:20:31

标签: php json laravel

  • 已更新 - JSON文件将返回,但根本不会更改结算日期。

  • 参考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();
}

2 个答案:

答案 0 :(得分:1)

您正在构建的网址不正确。在/$subscription

之间不应该是.json

更改

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

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

修改

尝试更改逻辑以使Guzzle这样调用。

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

答案 1 :(得分:0)

非常感谢大家帮助我。

我已经在这个问题上工作了2天,这对我来说应该是正确的。最终,他们的API误导了我。

我们唯一需要做的就是改变

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

在里面加了几个。

感谢大家帮助我,祝你有个美好的一天!