1

How to send user data in json format to another server when user register on wordpress site in PHP. I have a server url is like (http://1.2.3.4:49005/SingleMethodName=DomainManagementStripeService___DomainRegistration)

when user register on wordpress site i want to send their name, email, custom fields data to my server in json format. Thanks

Edited----- I am using mu-plugins for this purpose and bellow is the code i am using it for

<?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

2 Answers 2

2

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":"[email protected]","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":"[email protected]","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);
1
  • this is actually unlikely to work as it misses the critical part of doing it only after successful registration which with email validation will probably happen away from the registration page Mar 9, 2017 at 14:37
2

The trivial way to do this is to use the register_user action hook that fires immediately after a user is added to the database. It passes one variable to the callbacks, $user_id, that can be used to 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' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.