【发布时间】: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