如何在PHP中注册WordPress站点时将用户数据以JSON格式发送到另一台服务器

时间:2017-03-09 作者:khan

当用户在PHP中的wordpress站点注册时,如何将json格式的用户数据发送到另一台服务器。我有一个服务器url(http://1.2.3.4:49005/SingleMethodName=DomainManagementStripeService___DomainRegistration)

当用户在wordpress站点注册时,我想以json格式将他们的姓名、电子邮件、自定义字段数据发送到我的服务器。谢谢

编辑------我正在为此使用mu插件,下面是我使用它的代码

<?php
add_action(\'init\', \'s2_payment_notification\');
function s2_payment_notification()
{

if( isset($_POST[\'user_id\'], $_POST[\'subscr_id\'], $_POST[\'user_ip\'],$_POST[\'user_piva\']))
    {
        $user_id             = (integer)$_POST[\'user_id\'];
        $subscr_id           = (string)$_POST[\'subscr_id\'];
        $user_ip             = (string)$_POST[\'user_ip\'];
        $user_piva           = (integer)$_POST[\'user_piva\'];

        $s2member_subscr_id  = get_user_option(\'s2member_subscr_id\', $user_id);
        $s2member_registration_ip = get_user_option(\'s2member_registration_ip\', $user_id);
        $s2member_p_iva_fisc = get_user_option(\'p_iva_fisc\', $user_id);

        $user_piva = wp_json_encode($s2member_p_iva_fisc);
        $user_ip = wp_json_encode($s2member_registration_ip);
        $subscr_id = wp_json_encode($s2member_subscr_id);

        wp_remote_post(\'http://1.2.3.123:49005/SingleMethodName=DomainManagementStripeService___DomainRegistration\', [
            \'headers\' => [\'content-type\' => \'application/json\'],
            \'body\' => array(
                \'User_Id\' => \'$user_id\', 
                \'subscr_id\' => \'$subscr_id\', 
                \'user_ip\' => \'$user_ip\',
                \'user_piva\' => \'$user_piva\'
                ),
        ]);
    }
    else {
        wp_remote_post(\'http://1.2.3.123:49005/SingleMethodName=DomainManagementStripeService___DomainRegistration\', [
            \'headers\' => [\'content-type\' => \'application/json\'],
            \'body\' => \'Everything is empty\',
        ]);
    }

}

2 个回复
SO网友:Abhishek Pandey

Sending and receiving data in JSON format using POST method

xhr = new XMLHttpRequest();
var url = "url";
xhr.open("POST", url, true);
xhr.setRequestHeader("Content-type", "application/json");
xhr.onreadystatechange = function () { 
    if (xhr.readyState == 4 && xhr.status == 200) {
        var json = JSON.parse(xhr.responseText);
        console.log(json.email + ", " + json.password)
    }
}
var data = JSON.stringify({"email":"hey@mail.com","password":"101010"});
xhr.send(data);

Sending a receiving data in JSON format using GET method

xhr = new XMLHttpRequest();
var url = "url?data=" + encodeURIComponent(JSON.stringify({"email":"hey@mail.com","password":"101010"}));
xhr.open("GET", url, true);
xhr.setRequestHeader("Content-type", "application/json");
xhr.onreadystatechange = function () { 
    if (xhr.readyState == 4 && xhr.status == 200) {
        var json = JSON.parse(xhr.responseText);
        console.log(json.email + ", " + json.password)
    }
}
xhr.send();

Handling data in JSON format on the server-side using PHP

// Handling data in JSON format on the server-side using PHP
header("Content-Type: application/json");
// build a PHP variable from JSON sent using POST method
$v = json_decode(stripslashes(file_get_contents("php://input")));
// build a PHP variable from JSON sent using GET method
$v = json_decode(stripslashes($_GET["data"]));
// encode the PHP variable to JSON and send it back on client-side
echo json_encode($v);
SO网友:Nathan Johnson

实现这一点的简单方法是使用register_user 将用户添加到数据库后立即激发的操作挂钩。它将一个变量传递给回调,$user_id, 可以用来wp_safe_remote_post().

namespace StackExchange\\WordPress;

function register_user( $user_id ) {
  $url = \'https://example.com\';
  $args = [
    \'user_id\' => $user_id,
  ];
  \\wp_safe_remote_post( $url, $args );
}
\\add_action( \'register_user\', __NAMESPACE__ . \'\\register_user\' );

相关推荐

无法在模板函数.php中使用IS_HOME

我试图在标题中加载一个滑块,但只在主页上加载。如果有帮助的话,我正在使用Ultralight模板。我正在尝试(在template functions.php中)执行以下操作:<?php if ( is_page( \'home\' ) ) : ?> dynamic_sidebar( \'Homepage Widget\' ); <?php endif; ?> 但这行不通。现在,通过快速的google,我似乎需要将请