我尝试使用
GuzzleHttp
发布以下 html 表单,但由于在标题中设置 form_params
时出现错误,我无法这样做。 InvalidArgumentException: Header value must be scalar or null but array provided.
<form action="https://example.com/process" method="post">
<input type="hidden" name="data" value="<?php echo $base64_encrypted_json; ?>”>
<?php foreach($items_encoded as $item) { ?>
<input type="hidden" name="item_data[]" value="<?php echo $item; ?>”>
<?php } ?>
<input type="hidden" name="rid" value="example">
<input type="submit">
</form>
这是我在构造函数中的代码。
$this->setRequest(
new Request(
"POST",
$this->getConfig()->get(Module::MODULE_ENDPOINT_URL)["submit"]
)
);
/** @var array $submitDataArray */
$submitDataArray = $this->getConfig()->get(Module::MODULE_SUBMIT_REQUEST_DATA_CONFIG)->toArray();
$submitDataArray["data"] = json_encode($submitDataArray["data"]);
/** @var array $splitItemsData */
$splitItemsData = str_split(json_encode($submitDataArray["itemData"], true), 800);
/** @var array $itemDataEncoded */
$itemDataEncoded = [];
foreach ($splitItemsData as $itemDataString) {
$itemDataEncoded[] = json_encode($itemDataString);
}
$submitDataArray["itemData"] = $itemDataEncoded;
$submitDataModel = (new SubmitDataModel())->exchangeArray($submitDataArray);
$this->setSubmitDataModel($submitDataModel);
这是我的
init()
方法。
public function init(): void
{
/** @var array $submitDataModel */
$submitDataModel = $this->getSubmitDataModel()->exchangeArray();
/** @var array $formParams */
$formParams = array();
$formParams["rid"] = $submitDataModel["rid"];
$formParams["data"] = $submitDataModel["data"];
$formParams["item_data"] = $submitDataModel["itemData"];
$this->getRequest()->withHeader("Content-Type", "application/x-www-form-urlencoded");
$this->getRequest()->withHeader("form_params", $formParams);
}
$submitDataArray
如下
rid = "sandbox"
data = "{"username":"sandbox","request_data":{"return_urls":{"abort":"https:\/\/example.com\/checkout\/abort","fail":"https:\/\/example.com\/checkout\/rejected","success":"https:\/\/example.com\/checkout\/success","return_data":"https:\/\/example.com\/c832jdafs94kb"},"request_type":"standard_finance_request","test_mode":1,"order_details":{"order_id":"","currency":"GBP"},"customer_details":{"firstnames":"","surname":"","email":"","phone":"","address":"","city":"","country":"","postcode":"","dob":"","delivery_address":"","delivery_city":"","delivery_postcode":"","delivery_country":"UK"}}}"
itemData = {array} [1]
0 = ""[{\"description\":\"\",\"quantity\":0,\"price\":0}]""
为了保持简洁,我跳过了一些不必要的细节。
更新: 我也尝试过
multipart
来处理数组,但仍然没有帮助。
$this->getRequest()->withHeader("Content-Type", "multipart/form-data");
$this->getRequest()->withHeader("multipart", $formParams);
感谢@ITgoldman 的领导。我重构了部分代码,如下所示,它对我有用。
$this->getRequest()->withHeader("Content-Type", "application/x-www-form-urlencoded");
$this->getRequest()->withBody(\GuzzleHttp\Psr7\Utils::Streamfor(http_build_query($formParams)));
这就是它的工作原理。
http_build_query()
函数将数组转换为查询字符串,然后可以使用 \Psr\Http\Message\StreamInterface
方法将其转换为 \GuzzleHttp\Psr7\Utils::Streamfor()
,以便与 withBody()
方法一起使用。