WP_REMOTE_POST未过帐数据

时间:2018-11-14 作者:LubWn

这在php中有效:

$postdata = http_build_query(
        array(
            \'api\' => get_option(\'API_key\'),
            \'gw\' => \'1\'
        )
    );

    $opts = array(\'http\' =>
        array(
            \'method\'  => \'POST\',
            \'header\'  => \'Content-type: application/x-www-form-urlencoded\',
            \'content\' => $postdata
        )
    );

    $context  = stream_context_create($opts);

    $api_response = file_get_contents(\'https://myurl.com/api\', false, $context);
但是,这在Wordpress中不起作用:

$args = array(
        \'method\' => \'POST\',
        \'headers\'  => \'Content-type: application/x-www-form-urlencoded\',
        \'sslverify\' => false,
        \'api\' => get_option(\'API_key\'),
        \'gw\' => \'1\'
    );

    $api_response = wp_remote_post(\'https://myurl.com/api\', $args);
它基本上也应该这样做,但wordpress无法发送POST数据。我想将数据发送到服务器并获得HTML响应,如下所示$api_response.

1 个回复
最合适的回答,由SO网友:Krzysiek Dróżdż 整理而成

您错误地传递了请求参数。

看看Codex page. 你可以在那里找到这样的例子:

$response = wp_remote_post( $url, array(
  \'method\' => \'POST\',
  \'timeout\' => 45,
  \'redirection\' => 5,
  \'httpversion\' => \'1.0\',
  \'blocking\' => true,
  \'headers\' => array(),
  \'body\' => array( \'username\' => \'bob\', \'password\' => \'1234xyz\' ),
  \'cookies\' => array()
   )
);
因此,在您的情况下,应该如下所示:

$args = array(
    \'method\' => \'POST\',
    \'headers\'  => array(
        \'Content-type: application/x-www-form-urlencoded\'
    ),
    \'sslverify\' => false,
    \'body\' => array(
        \'api\' => get_option(\'API_key\'),
        \'gw\' => \'1\'
    )
);

$api_response = wp_remote_post(\'https://myurl.com/api\', $args);

结束

相关推荐