使用wp_login上的wp_emote_post将数据发送到第三方API

时间:2018-03-31 作者:Yale Newman

是否可以使用wp\\u remote\\u post向第三方api发送http post请求?我无法成功地将用户对象保存为javascript变量,因此我希望可以使用php发出http请求,并在我的node express应用程序中处理javascript操作。

当前尝试次数:

function identify_user() {
    echo "made it into identify user";
    if( is_user_logged_in()):
        $current_user = wp_get_current_user();
        $user = [];
        $user[\'id\'] =  $current_user->ID;
        $user[\'user_login\'] = $current_user->user_login;
        $user[\'user_email\'] = $current_user->user_email;
        $user[\'user_firstname\'] = $current_user->user_firstname;
        $user[\'user_lastname\'] = $current_user->user_lastname;
        $user[\'display_name\'] = $current_user->display_name;
        $response = wp_remote_post( \'myapp.com/endpoint\', array(
           \'method\' => \'POST\',
           \'headers\' => array(\'Content-Type\' => \'application/json; charset=utf-8\'),
           \'body\' => json_encode($user)
        )
    );

    if ( is_wp_error( $response ) ) {
       $error_message = $response->get_error_message();
       echo "Something went wrong: $error_message";
    } else {
       print_r( $response );
    }
    endif;
}

add_action( \'wp_login\', \'identify_user\' );
我很难排除此代码的故障,因为我的echo调用都没有登录到控制台。我看到您可以运行error\\u log(something),但也无法使其正常工作。

3 个回复
最合适的回答,由SO网友:patman 整理而成

“body”需要是一个数组,不包括“json\\u encode($user)”部分。

$response = wp_remote_post( \'myapp.com/endpoint\', array(
  \'method\' => \'POST\',
  \'headers\' => array(\'Content-Type\' => \'application/json; charset=utf-8\'),
  \'body\' => $user
  )
);
我的职能中有这一点,因为我也有身体作为物体的问题:

if (is_object($user) && !is_array($user))
  $user = json_decode(json_encode($user), true);
$body = $user;
关于“运行错误日志(某物)…”你有问题,试试看#David Lee\'s wrapper function

基本上你可以打电话

write_log(\'THIS IS THE START OF MY CUSTOM DEBUG\');
  // or log data like objects
write_log($object_you_want_to_log);
我不知道没有它我该怎么办。

SO网友:Somin

请尝试以下代码可能对您有所帮助。

function identify_user() { 
if( is_user_logged_in()): 
$current_user = wp_get_current_user(); 
$_id = $current_user->ID; 
$_email = $current_user->user_email; 
$user = json_encode(array("user_id"=>$_id,"user_email"=>$_email)); 
$curl = curl_init("myeNDPOINT"); 
curl_setopt( $curl, CURLOPT_POST, true ); 
curl_setopt( $curl, CURLOPT_POSTFIELDS,$user); 
curl_setopt($ch, CURLOPT_HTTPHEADER,array(\'Content-Type:application/json\')); 
curl_exec( $curl ); 
curl_close( $curl ); 
endif; 
} 

add_action( \'wp_enqueue_scripts\', \'identify_user\');

SO网友:Somin

您可以尝试以下wp\\u remote\\u post代码。可能您缺少远程post url参数。

我还注意到,您将用户变量转换为json格式2次,这是错误的。

$response = wp_remote_post($url, array(
        \'method\' => \'POST\',
        \'headers\' => array(\'Content-Type\' => \'application/json; charset=utf-8\'),
        \'httpversion\' => \'1.0\',
        \'sslverify\' => false,
        \'body\' => json_encode($user)
    );

结束

相关推荐

通过WP REST API显示帖子特色图像和类别

我有一个自定义函数,可以从另一个Wordpress博客中提取(3)篇文章,它通过一个快捷码从我指定的特定类别中提取它们。现在,尽管如此,我无法让它运行并显示实际图像,因为我只能让它显示特色图像(媒体附件)的ID本身。这是我的功能代码,除了不显示图像外,它可以工作,而是图像ID(数值)function feedpuller_func($atts) { $url = $atts[\'url\']; $count = ($atts[\'count\'])?$at