laravel - Guzzle 发布嵌套数组问题

标签 laravel curl xdebug guzzle

我使用 Laravel 5.5 和 Guzzle 6.3 开发网站。
我在尝试使用 API 为 BOX 创建文件夹时尝试大量发布嵌套数组时遇到问题。

$url = $this->api_url . "/folders";
$headers = [
    'Authorization' => 'Bearer ' . $this->access_token,        
];
$client = new Client();
$response = $client->post($url, [
    'headers' => $headers, 
    'form_params' => [
        'name' => $name,
        'parent' => [
            'id' => $parent_id
        ]
    ]
]);
它向我显示了这样的错误:

Entity body should be a correctly nested resource attribute name/value pair


我也已经尝试使用 shell_exec curl 所以它从命令提示符运行 curl 它给了我同样的错误
picture
但是当我尝试从 cygwin 运行时,curl 工作正常。
我也可以使用多部分请求嵌套数组进行上传工作正常。
当嵌套数组在多部分请求中正常工作时,我不知道为什么会遇到此嵌套数组问题。
Box 文档 POST 的引用 is here .

最佳答案

根据docs您不能使用 multipart 选项:

form_params cannot be used with the multipart option. You will need to use one or the other. Use form_params for application/x-www-form-urlencoded requests, and multipart for multipart/form-data requests.

This option cannot be used with body, multipart, or json


因此,也许可以尝试在创建 Client 实例时设置 header :
$url = $this->api_url . "/folders";

$client = new Client([
    'headers' => [
        'Authorization' => 'Bearer ' . $this->access_token,
        'Accept'        => 'application/json',        
    ]
]);

$response = $client->post($url, [ 
    'json' => [
        'name' => $name,
        'parent' => [
            'id' => $parent_id
        ]
    ]
]);
实际上在再次阅读框引用后,没有文件上传的发布请求接受 application/json ,
这是用于 application/x-www-form-urlencoded 的 form_params

关于laravel - Guzzle 发布嵌套数组问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52411226/

相关文章:

reactjs - 如何在惯性 ReactJs 中应用 Laravel 模型中的 hasMany 关系

symfony - HWIOAuthBundle - SSL 证书

python - 使用 Python 或 Curl 获取页面时出现不同的页面

php - 安装了 Xdebug 但我无法在 phpinfo 中找到它

mysql - laravel 5中如何将一个表的一个字段插入到另一个表中?

php - 如何在 laravel 中使用 sql NOW( ) 函数

php - 如何 “dynamize” Dockerfile/Docker Compose?

php - 在 Docker 中安装 XDebug

mysql - 如何使用 laravel 或 mysql 创建嵌套 json?

node.js - Ubuntu + Node + curl : (52) Empty reply from server