admin管理员组文章数量:1130349
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.
1 Answer
Reset to default 8You’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);
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.
-
Looking at the options that
wp_remote_post()accepts, I don't seeapiorgw. What doesvar_dump( $api_response );give you in your WordPress code? – Pat J Commented Nov 14, 2018 at 3:45
1 Answer
Reset to default 8You’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);
本文标签: wp remote postWpremotepost not posting data
版权声明:本文标题:wp remote post - Wp_remote_post not posting data 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749183677a2329032.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


wp_remote_post()accepts, I don't seeapiorgw. What doesvar_dump( $api_response );give you in your WordPress code? – Pat J Commented Nov 14, 2018 at 3:45