Posting user registration data from external site

Hi, my use-case is that I need to auto-register customers from a WooCommerce site into User Frosting. I can do this by doing a cURL POST to the /account/register endpoint with an array of customer data, eg.

    array( 
			'first_name'=>'first',
			'last_name'=>'last',
			'email'=>'[email protected]',
			'locale'=>'en_US',
			'password'=>'dummypasshere',
			'passwordc'=>'dummypasshere',
			'user_name'=>'dummy.user',
			'spiderbro'=>'http://',
		);

This sorta works but if something goes wrong (eg the data doesn’t validate) I can’t handle the errors and nothing gets returned.

Is there a better way to handle this?

The errors are sent to the message stream by default :

You would need to catch for Json request returning 4xx code and then query the alert stream. But I agree we might need a better solution for future versions, especially if we want to use other frontend framework like Vue.js

Hey there. After the cURL POST to the /account/register endpoint I then check the response code. If it’s different to 200 I check the alert stream by doing a GET to /alerts. This is always empty unfortunately.

	private function curl_post(  ){

	if( !isset( $this->data ) || !isset( $this->api_url ) ){

		throw new Exception("Data and API URL must be set");
	}

	error_log(print_R('POSTING to API',TRUE));

	/*$this->data = array( 
		'first_name'=>'anth2onsd',
		'last_name'=>'blursdf',
		'email'=>'[email protected]',
		'locale'=>'en_US',
		'password'=>'123123123123',
		'passwordc'=>'123123123123',
		'user_name'=>'anthony.blauez727z1',
		'spiderbro'=>'http://',
	);*/
	
	error_log(print_R($this->data,TRUE));
	error_log(print_R($this->api_url,TRUE));

	$curl = curl_init();
	curl_setopt_array($curl, array(
	  CURLOPT_URL => $this->api_url,
	  CURLOPT_RETURNTRANSFER => true,
	  CURLOPT_ENCODING => '',
	  CURLOPT_MAXREDIRS => 10,
	  CURLOPT_TIMEOUT => 0,
	  CURLOPT_FOLLOWLOCATION => true,
	  CURLOPT_CUSTOMREQUEST => 'POST',
	  CURLOPT_POSTFIELDS => $this->data,
	  CURLOPT_VERBOSE => true,
	  CURLOPT_SSL_VERIFYPEER => false,
	  CURLOPT_SSL_VERIFYHOST => false
	));
		
	$response = curl_exec($curl);
	$code = curl_getinfo($curl,CURLINFO_RESPONSE_CODE);
	error_log(print_R($response,TRUE));
	curl_close($curl);
	error_log(print_R('Response code: '.$code,TRUE));

	//check the alerts stream
	if( $code != 200 ){
		$response = $this->curl_get_alerts(  );
	}

	return $response;

}

public function curl_get_alerts(  ){	

	$url = $this->get_url().$this->get_alert_path();
	error_log(print_R('Querying alert stream at '.$url,TRUE));	

	$curl = curl_init();
	curl_setopt_array($curl, array(
	  CURLOPT_URL => $url,
	  CURLOPT_FOLLOWLOCATION => true,
	  CURLOPT_CUSTOMREQUEST => 'GET',
	  CURLOPT_RETURNTRANSFER => true,
	  CURLOPT_ENCODING => '',
	  CURLOPT_MAXREDIRS => 10,
	  CURLOPT_TIMEOUT => 0,
	  CURLOPT_FOLLOWLOCATION => true
	));

	$response = curl_exec($curl);
	curl_close($curl);
	error_log(print_R('Response from ALERTS',TRUE));
	error_log(print_R($response,TRUE));
	return $response;
}

When I’m logging the response in curl_post and there’s an error, eg the username is already taken, I see that in the response

<!DOCTYPE html><!--\n\n\nUserFrosting\\Support\\Exception\\HttpException: Email is already in use.

But querying the alert stream doesn’t return anything

Querying alert stream at https://userfrosting.test/alerts
Response from ALERTS
(empty)