我有一个外部api,它将json主体作为有效负载。我正在尝试发送多个并行请求。所以我没有使用wp_remote_post()
我的代码的wp\\u remote\\u post()版本工作得很好。
使用发送JSON负载wp_remote_post()
这很有效! $response = wp_remote_post($url, [
\'body\' => json_encode($registrants), // requried keys [firstName, lastName, email]
\'headers\' => [
\'Authorization\' => \'Bearer \' . $accessToken,
\'Content-Type\' => \'application/json; charset=utf-8\'
],
\'data_format\' => \'body\',
\'timeout\' => 10,
]);
return json_decode(wp_remote_retrieve_body($response));
现在我也在尝试Request::request_multiple()
但数据不是作为json主体发送的。使用发送JSON负载Request::request_multiple()
不工作! $requests[] = [
\'url\' => $url,
\'type\' => \'POST\',
\'body\' => json_encode($registrant), // requried keys [firstName, lastName, email]
\'headers\' => [
\'Authorization\' => \'Bearer \' . $accessToken,
\'Content-Type\' => \'application/json; charset=utf-8\'
],
\'data_format\' => \'body\',
\'timeout\' => 30,
];
$options = [
\'data_format\' => \'body\'
];
$resp = Requests::request_multiple($requests, $options);
foreach($resp as $response){
var_dump($response);
$responses[] = json_decode($response->body);
}
我从API中得到的错误非常具体,当它没有获得JSON正文负载时抛出。