我正在建立一个mailchimp集成,它们需要使用JSON代码进行POST调用。

不,我正在使用实际上有效的代码:

$data = wp_remote_post($url, array(
    'headers'   => array('Content-Type' => 'application/json; charset=utf-8'),
    'body'      => json_encode($array_with_parameters),
    'method'    => 'POST'
));


但是,它会返回PHP警告


警告:http_build_query():参数1应该是数组或对象。第507行的../wp-includes/Requests/Transport/cURL.php中给出的值不正确


如何取消它?

我已经尝试过只在'body'索引中使用纯数组,但MailChimp返回JSON解析错误。

评论

您是否已将此补丁应用到核心? core.trac.wordpress.org/ticket/37700

有趣。是一个商业插件,则必须在任何WP安装中都可以使用。但是由于似乎是WP错误,对我来说还可以。非常感谢!

#1 楼

尝试像这样在您的请求中设置data_format参数: ,这已经给您带来了问题,因为您已经将主体格式化为字符串。这是query中的相关检查:

$data = wp_remote_post($url, array(
    'headers'     => array('Content-Type' => 'application/json; charset=utf-8'),
    'body'        => json_encode($array_with_parameters),
    'method'      => 'POST',
    'data_format' => 'body',
));


由于您的错误来自http_build_query的第507行,我们可以看到这是wp-includes/class-http.php的根调用:

if (!empty($data)) {
    $data_format = $options['data_format'];

    if ($data_format === 'query') {
        $url = self::format_get($url, $data);
        $data = '';
    }
    elseif (!is_string($data)) {
        $data = http_build_query($data, null, '&');
    }
}