代码之家  ›  专栏  ›  技术社区  ›  sortas

如果不需要回复,如何使用Wordpress wp_remote_post?

  •  0
  • sortas  · 技术社区  · 8 年前

    wp_remote_post 发送表单数据( Contact Form 7 )到外部API(CRM)。API很重(检查电子邮件、确认信等),所以我不希望PHP阻塞任何进程,同时等待响应(我根本不需要响应,只需要发送)。

    'blocking' => false 如果我在外部API上激活确认邮件,Wordpress用户需要等待几秒钟才能处理表单。

    我做错什么了?:)代码:

    // 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 回复  |  直到 8 年前
        1
  •  4
  •   Bijaya Kumar Oli    8 年前

    我认为这里缺少方法,添加方法并尝试

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

    并检查响应:

    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>';
    }