【问题标题】:Guzzle not sending POST parametersGuzzle 不发送 POST 参数
【发布时间】:2017-05-03 03:03:52
【问题描述】:

我将此作为测试发送到测试网络服务器,但响应虽然是 201,这意味着它得到了它,但它没有显示我要发送的发布数据:

<?php
  $url = "https://jsonplaceholder.typicode.com/posts";
  $client = \Drupal::httpClient();

  $post_data = array('color' => 'red');
  $response = $client->request('POST', $url, [
    'form_params' => $post_data,
    'verify' => false
    ]);
  $body = $response->getBody();
  dsm($body);
?>

我提出的请求格式不正确吗?

我可以看到它没有获取帖子数据,因为当我对响应正文进行 dsm 时,它不存在。

【问题讨论】:

  • 这可能是由于“验证”=>错误吗?但我需要它来避免证书问题

标签: php drupal guzzle


【解决方案1】:

这对我有用,看来我需要添加标题:

$url="https://jsonplaceholder.typicode.com/posts";
    $client = \Drupal::httpClient();
    $post_data = $form_state->cleanValues()->getValues();
    $response = $client->request('POST', $url, [
    'headers' => ['Content-Type' => 'application/x-www-form-urlencoded'],
    'form_params' => $post_data,
    'verify'=>false,
  ]);
    $body = $response->getBody()->getContents();
    $status = $response->getStatusCode();
    dsm($body);
    dsm($status);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-11-19
    • 1970-01-01
    • 2016-02-14
    • 2021-07-08
    • 1970-01-01
    • 2018-03-21
    • 1970-01-01
    相关资源
    最近更新 更多