【问题标题】:Laravel PUT Method Not Working [duplicate]Laravel PUT方法不起作用[重复]
【发布时间】:2017-06-21 13:20:44
【问题描述】:

已更新 - 我正在尝试使用 API 文档在 Laravel 的 Http 和 Guzzle 中使用 PUT 方法更改计费日期,但是 JSON 文件会返回,但它根本不会更改计费日期。

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

【问题讨论】:

  • 代码中的"next_billing_at" =&gt;[ $nextBilling ] 中的[ $nextBilling ] 应该是一个数组吗?示例中似乎没有:"next_billing_at":"2018-12-15"
  • 是的,最初它不是一个数组,它是:'{"subscription":{"next_billing_at":"2018-12-15"}}',我也尝试过,但到目前为止仍然无法正常工作。
  • 好的。我注意到的另一件事是,您可能必须使用 $nextBilling-&gt;toDateString() 格式化您的 Carbon 日期。这会将它从 Carbon 日期对象转换为 YYYY-MM-DD 字符串格式
  • 是的,感谢您的通知,但这也不是这个问题,因为他们的 API 支持 YYYY-MM-DD 和更精确的时间戳:)

标签: php json api chargify


【解决方案1】:

改变这个:

echo $response->getBody();

dd($response->getBody());

并repost返回响应数据。

【讨论】:

  • Stream {#726 ▼ -stream: stream resource @11 ▼ wrapper_type: "PHP" stream_type: "TEMP" mode: "w+b" unread_bytes: 0 seekable: true uri: "php://temp" options: [] } -size: null -seekable: true -readable: true -writable: true -uri: "php://temp" -customMetadata: [] }
  • 改用dd($res-&gt;getBody());
  • 我确实使用了dd($res-&gt;getBody());,它与上面给出的信息相同。 :)
  • 使用 GuzzleHttp\Stream\Stream; $request = $client->createRequest('PUT', 'httpbin.org/put', ['body' => 'testing...']);回声 $request->getBody()->read(4); // 测试回显 $request->getBody()->read(4); // 荷兰国际集团。回声 $request->getBody()->read(1024); // .. var_export($request->eof());
猜你喜欢
  • 2018-08-25
  • 1970-01-01
  • 1970-01-01
  • 2020-04-15
  • 2015-02-08
  • 1970-01-01
  • 2014-05-27
  • 1970-01-01
  • 2017-07-17
相关资源
最近更新 更多