How to modify user values in custom business logic with remote API

In my application, once a user have registered, I need to query a remote, external API using some of the data she provided.
After receiving an answer from the remote API, I need to store the returned data as part of the user’s profile.

I’m using the PHP SDK and have 2 questions related to the above:

  1. in the context of the beforeRegister / afterRegister functions, how can I change the $user_values array so that the changes I do are persisted?
  2. How can I perform an HTTP call to a remote REST API from one of the beforeRegister / afterRegister functions?

Thanks,
Gilad

1) in the context of the beforeRegister / afterRegister functions, how can I change the $user_values array so that the changes I do are persisted?

You would do it exactly the same way as you modify any other object in PHP. There is absolutely nothing special $user_values, it is just a PHP object array…

2) How can I perform an HTTP call to a remote REST API from one of the beforeRegister / afterRegister functions?

Once again, you would do it the same way you make an HTTP call to a rest API in PHP

Mark

Mark,
re. 1: The problem is that if I modify the $user_values array, my changes don’t persist.

re. 2: I usually use curl to do HTTP calls, but it seems curl isn’t available to the custom code.

I understand that these tasks should be simple to implement - I might be missing something…

Here’s the code for my test GenericUserEventHandler.php:

class GenericUserEventHandler extends BaseUserEventHandler 
{
 public function beforeRegister( $context, $user_values ) {
 	$user_values['balance'] = 200;
 }
 
 public function afterRegister( $context, $user_values, $execution_result ) {
 	if (function_exists('curl_init')) {
 		echo "Haz cURL!\n";
 	}
 	var_dump($user_values);
 }
}

And here’s the output for a user registration event using the above code:

...
[INFO] Model successfully deployed...
[INFO] Waiting for events...
array(3) {
 ["password"]=>
 string(60) "$2a$10$dRdtMIJ0jrvSwQgQXqPOie5y1rA7oOF7rBKOwCLPhCgyzbUftJHNq"
 ["email"]=>
 string(11) "goo@bag.com"
 ["lol_info"]=>
 string(33) "{"summoner":"bob","region":"euw"}"
}

Thanks,
Gilad

Gilad,

We will look into (1).

As for (2), curl is not the only way to make http requests. Here’s another alternative: http://stackoverflow.com/questions/959063/how-to-send-a-get-request-from-php

Mark

Hi Gilad,

Here’s a temporary workaround for (1):

Change the declaration of $user_values to look like this (with the & character before $):

public function beforeRegister( $context, &$user_values )
{
$user_values[‘balance’] = 200;
}

Regards,
Mark