How to use Wordpress wp_remote_post if I don't need to get a response?

邮差的信 提交于 2020-06-17 02:01:11

问题


Using wp_remote_post to send form data (Contact Form 7) to external API (CRM). API is heavy (checking emails, confirmation letters, etc), so I don't want PHP to block any processes, while waiting for a response (I don't need response at all, just send).

Still, even with 'blocking' => false it does — if I activate confirmation emails on external API, Wordpress users need to wait several seconds before form is processed.

What am I doing wrong? :) Code:

// POST-request to API
wp_remote_post('http://crm.site.com/get_record', array(
    'timeout' => 5,
    'redirection' => 5,
    'httpversion' => '1.0',
    'blocking' => false,
    'headers' => array() ,
    'body' => $send_data,
    'cookies' => array()
));

回答1:


I think method is missing here, add method and try

   $response = wp_remote_post('http://crm.site.com/get_record', array(
    'method' => 'POST',
    'timeout' => 5,
    'redirection' => 5,
    'httpversion' => '1.0',
    'blocking' => false,
    'headers' => array() ,
    'body' => $send_data,
    'cookies' => array()
   ));

And check response:

if ( is_wp_error( $response ) ) {
   $error_message = $response->get_error_message();
   echo "Something went wrong: $error_message";
} else {
   echo 'Response:<pre>';
   print_r( $response );
   echo '</pre>';
}


来源:https://stackoverflow.com/questions/52041684/how-to-use-wordpress-wp-remote-post-if-i-dont-need-to-get-a-response

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!