$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>wp remote post - Wp_remote_post not posting data|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

wp remote post - Wp_remote_post not posting data

matteradmin8PV0评论

This works in 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('', false, $context);

However, this does not work in 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('', $args);

It basicly should do the same, but wordpress somehow fails to send the POST data. I want to send the data to server and get the HTML response as $api_response.

This works in 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/api', false, $context);

However, this does not work in 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/api', $args);

It basicly should do the same, but wordpress somehow fails to send the POST data. I want to send the data to server and get the HTML response as $api_response.

Share Improve this question asked Nov 14, 2018 at 3:12 LubWnLubWn 751 gold badge1 silver badge6 bronze badges 1
  • Looking at the options that wp_remote_post() accepts, I don't see api or gw. What does var_dump( $api_response ); give you in your WordPress code? – Pat J Commented Nov 14, 2018 at 3:45
Add a comment  | 

1 Answer 1

Reset to default 8

You’re passing request params incorrectly.

Take a look at Codex page. You can find such example in there:

$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()
   )
);

So in your case it should look something like this:

$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/api', $args);

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far