通过 Laravel Http 客户端发送嵌套数组
Send nested array via Laravel Http Client
我想通过 Laravel Http 客户端 (Laravel 7.x)
向包含嵌套参数的 API 网站发送请求
$params = [
'type' => 'books',
'variables' => [
'color' => 'red',
'size' => 'large'
]
]
我希望我的 url 喜欢这个:
http://example.com/?type=books&variables={"color":"red","size":"large"}
以上编码url:
http://example.com/?type=books&variables=%7B%22color%22%3A%22red%22%2C%22size%22%3A%22large%22%7D
但是当我使用时:
Http::get('http://example.com', $params);
API 服务器 returns 错误。
但是当我使用时:
Http::get('http://example.com/?type=books&variables={"color":"red","size":"large"}');
效果很好。
那么如何将我的参数数组转换成 url 呢?
(我无法访问 API 服务器)
尝试json_encode()
$params = [
'type' => 'books',
'variables' => json_encode([
'color' => 'red',
'size' => 'large'
])
]
$url = "http://example.com?".http_build_query($params);
Http::get($url);
和http_build_query()
参考 link https://www.php.net/manual/en/function.http-build-query.php
我想通过 Laravel Http 客户端 (Laravel 7.x)
向包含嵌套参数的 API 网站发送请求$params = [
'type' => 'books',
'variables' => [
'color' => 'red',
'size' => 'large'
]
]
我希望我的 url 喜欢这个:
http://example.com/?type=books&variables={"color":"red","size":"large"}
以上编码url:
http://example.com/?type=books&variables=%7B%22color%22%3A%22red%22%2C%22size%22%3A%22large%22%7D
但是当我使用时:
Http::get('http://example.com', $params);
API 服务器 returns 错误。
但是当我使用时:
Http::get('http://example.com/?type=books&variables={"color":"red","size":"large"}');
效果很好。
那么如何将我的参数数组转换成 url 呢?
(我无法访问 API 服务器)
尝试json_encode()
$params = [
'type' => 'books',
'variables' => json_encode([
'color' => 'red',
'size' => 'large'
])
]
$url = "http://example.com?".http_build_query($params);
Http::get($url);
和http_build_query()
参考 link https://www.php.net/manual/en/function.http-build-query.php