最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Send POST request to Flask app from functions.php file

programmeradmin2浏览0评论

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);
}
发布评论

评论列表(0)

  1. 暂无评论