【问题标题】:Convert cURL to Guzzle POST with --form-params and --header使用 --form-params 和 --header 将 cURL 转换为 Guzzle POST
【发布时间】:2019-06-30 19:27:46
【问题描述】:

我正在为一个给定的 curl 请求而苦苦挣扎,我想通过 guzzle 处理它。

curl 请求如下所示:

curl --location --request POST "https://apis.myrest.com" \
  --header "Content-Type: multipart/form-data" \
  --header "Authorization: Bearer YOUR-BEARER-TOKEN" \
  --form "mediaUrl=https://myfile.mpg" \
  --form "configuration={
    \"speechModel\": { \"language\": \"en-US\" },
    \"publish\": {
      \"callbacks\": [{        
      \"url\" : \"https://example.org/callback\"
    }]
  }
}

我希望它像这样通过 guzzle 发送:

// 1. build guzzle client:
//----------------------------------------------------------------------
$this->client = new Client([
    'base_uri' => $this->config->getBaseUri(),
]);

// 2. build guzzle request:
//----------------------------------------------------------------------
$request = new Request(
    'POST',
    'myendpoint',
    [
        'authorization' => 'Bearer ' . $this->config->getApiToken(),
        'cache-control' => 'no-cache',
        'content-type' => 'application/json',

        // maybe here, or not?
        form_params => ['mediaUrl' => 'www.media.com'],
    ]
);

// 3. send via client
//----------------------------------------------------------------------
response = $this->client->send($request, ['timeout' => self::TIMEOUT]);

我现在的问题是,我不知道如何处理这个问题。在 guzzle 的文档中,我找到了“form_params”: http://docs.guzzlephp.org/en/stable/quickstart.html#making-a-request#post-form-requests

但它似乎不起作用。如果我将 form_params-array 添加到我的请求中,接收者不会得到它们。谁能告诉我,如何用 guzzle 编写确切的 curl 命令?

谢谢

【问题讨论】:

    标签: forms curl parameters header guzzle


    【解决方案1】:

    尝试使用multipart 而不是form_params

    http://docs.guzzlephp.org/en/latest/request-options.html#form-params

    来自 Guzzle 文档:

    form_params 不能与 multipart 选项一起使用。你需要 使用其中一种。使用 form_params application/x-www-form-urlencoded 请求,以及多部分 多部分/表单数据请求。

    另外尝试使用debug 设置 Guzzle 客户端,因为它会显示它发送的原始 HTTP 请求,因此您可以更轻松地与 curl 命令进行比较。

    http://docs.guzzlephp.org/en/latest/request-options.html#debug

    很难理解您想要发送的确切请求是什么,因为 curl 示例和您的代码之间存在不一致。我试图尽可能地复制卷曲。请注意Request 第三个参数只需要标头,对于请求选项,您必须使用send 的第二个参数。

    $client = new Client([
        'base_uri' => 'https://example.org',
        'http_errors' => false
    ]);
    
    $request = new Request(
        'POST',
        '/test',
        [
            'Authorization' => 'Bearer 19237192837129387',
            'Content-Type' => 'multipart/form-data',
        ]
    );
    
    $response = $client->send($request, [
        'timeout' => 10,
        'debug' => true,
        'multipart' => [
            [
                'name'     => 'mediaUrl',
                'contents' => 'https://myfile.mpg'
            ],
            [
                'name'     => 'configuration',
                'contents' => json_encode([
                    'speechModel' => [
                        'language' => 'en-US'
                    ],
                    'publish' => [
                        'callbacks' =>
                            [
                                [
                                    'url' => 'https://example.org/callback'
                                ]
                            ]
                    ]
                ])
            ]
        ]
    ]);
    

    【讨论】:

    • 嗨@ncla,非常感谢您的回复,它有效!我还有一个问题:根据我的理解,我想将所有选项放入请求对象中。这可能吗?老实说,我不明白何时将哪个选项放入哪个对象:Guzzle-Doc (docs.guzzlephp.org/en/stable/quickstart.html) 说,您可以将每个选项作为“默认请求选项”放入客户端,那么为什么要将选项放入“发送”方法而不是请求对象?
    • @j0nnybrav0 你是对的,这似乎有点傻。如果你想简化一些事情,你可以使用 $client->post('/test', $options); 例如,然后在 $options 中设置标题。您可以在创建请求对象和创建 $options 时设置请求标头。请求类只接受一些参数(第三个只是标题)。 github.com/guzzle/psr7/blob/master/src/Request.php#L32 我使用了send 方法,因为这就是您使用的方法。你也可以像$client->request('POST', '/test', $options)一样使用->request()
    • send() 只允许您传递自己的 Request 对象,这允许您对其进行更多自定义。基本上send 是比request 更底层的方法。如果您不想担心处理 Request 对象,请使用 request()。 :)
    • 这里介绍如何通过选项添加标题。 docs.guzzlephp.org/en/stable/request-options.html#headers 它必须在选项数组中的关键“标题”内。
    • 再次:非常感谢!现在一切似乎都清楚了一点。我认为更好的 oop 方式似乎与请求对象一起使用,但我认为您的解决方案更易于使用。
    猜你喜欢
    • 2016-03-21
    • 1970-01-01
    • 2021-07-08
    • 2013-10-30
    • 1970-01-01
    • 2015-11-02
    • 2021-09-08
    • 2021-02-19
    • 2013-06-30
    相关资源
    最近更新 更多