admin管理员组文章数量:1415484
I have an endpoint on my Flask
app that I can hit via Postman and it works fine. However, when I write the POST request in my functions.php file and trigger it via button press, my server returns a 400
error. I've tried a ton of different approaches, using wp_remote_post
, file_get_contents
, and cURL
.
function send_forgot_password_email ( $email ) {
// ATTEMPT (1): file_get_contents
$url = '';
$data = array('email' => '[email protected]');
// use key 'http' even if you send the request to https://...
$options = array(
'http' => array(
'header' => "Content-Type: application/json",
'method' => 'POST',
'content' => http_build_query($data)
)
);
$context = stream_context_create($options);
$response = file_get_contents($url, false, $context);
if ($response === FALSE) { /* Handle error */ }
var_dump($response);
// ATTEMPT (2): wp_remote_post
// $response = wp_remote_post(
// $url,
// array(
// 'headers' => array("Content-Type" => "application/json"),
// 'body' => array(
// 'email' => "[email protected]"
// )
// )
// );
// ATTEMPT (3): cURL
// $json = json_encode(array("email"=>"[email protected]"));
// $curl = curl_init("");
// curl_setopt( $curl, CURLOPT_POST, true );
// curl_setopt( $curl, CURLOPT_POSTFIELDS,$json);
// curl_setopt($ch, CURLOPT_HTTPHEADER,array('Content-Type:application/json'));
// curl_exec( $curl );
// curl_close( $curl );
// ATTEMPT (4): wp_remote_post (again)
// $args = array(
// 'method' => 'POST',
// 'headers' => array(
// 'Content-type: application/json'
// ),
// 'sslverify' => true,
// 'body' => array(
// 'email' => "[email protected]",
// )
// );
// $response = wp_remote_post($url, $args);
}
本文标签: Send POST request to Flask app from functionsphp file
版权声明:本文标题:Send POST request to Flask app from functions.php file 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745233808a2648939.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论