我需要将标头类型添加到下面的 guzzle 请求中,但无法弄清楚如何将其放入而不出现错误这是我要添加的内容:
$command->set('command.headers', array('content-type' => 'application/x-www-form-urlencoded
Run Code Online (Sandbox Code Playgroud)
下面这段代码:
<?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);
?>
Run Code Online (Sandbox Code Playgroud)
小智 5
当我需要在 D8 使用 Guzzle 进行 POST 时,我像这样传递了 Content-Type:
$url = "https://jsonplaceholder.typicode.com/posts";
$client = \Drupal::httpClient();
$post_data = array('color' => 'red');
$response = $client->request('POST', $url, [
'headers' => ['Content-Type' => 'application/json'],
'body' => rawData($post_data),
]);
$body = $response->getBody()->getContents();
$status = $response->getStatusCode();
Run Code Online (Sandbox Code Playgroud)
一个好主意是使用 D8 依赖注入来传递 HTTP_CLIENT。
| 归档时间: |
|
| 查看次数: |
4118 次 |
| 最近记录: |