使用 --form-params 和 --header 将 cURL 转换为 Guzzle POST
Convert cURL to Guzzle POST with --form-params and --header
我正在努力处理一个给定的 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 命令?
谢谢
尝试使用 multipart
而不是 form_params
。
http://docs.guzzlephp.org/en/latest/request-options.html#form-params
来自 Guzzle 文档:
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.
另外尝试将 Guzzle 客户端设置为启用 debug
,因为它将显示它发送的原始 HTTP 请求,因此您可以更轻松地将其与 curl 命令进行比较。
http://docs.guzzlephp.org/en/latest/request-options.html#debug
很难理解您想发送的确切请求是什么,因为 curl 示例和您的代码之间存在不一致。我试图尽可能地复制卷曲。请注意,Request
第三个参数只需要 headers,对于请求选项,您必须使用 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'
]
]
]
])
]
]
]);
我正在努力处理一个给定的 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 命令?
谢谢
尝试使用 multipart
而不是 form_params
。
http://docs.guzzlephp.org/en/latest/request-options.html#form-params
来自 Guzzle 文档:
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.
另外尝试将 Guzzle 客户端设置为启用 debug
,因为它将显示它发送的原始 HTTP 请求,因此您可以更轻松地将其与 curl 命令进行比较。
http://docs.guzzlephp.org/en/latest/request-options.html#debug
很难理解您想发送的确切请求是什么,因为 curl 示例和您的代码之间存在不一致。我试图尽可能地复制卷曲。请注意,Request
第三个参数只需要 headers,对于请求选项,您必须使用 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'
]
]
]
])
]
]
]);