Feb 13, 2015

How to create users with custom field in Appcelerator API using PHP CURL?

If you want to creates a new user with custom field then it's not a big deal. Follows the instructions and use the code below.

Instructions: When creating a user, you must specify either:
    * username
    * email address, first name, and last name

A user can have both an email address and username specified. However, if username is blank then email address, first name and last name are required.

We are creating here a user just by using the username, we are not using email here. So we just need username, password and password_confirmation.

Role: Whatever you want to add. I am taking as 'teacher'. It is not mendatory.
Custom Field: 'pranay' having value equal to '23'. It can be anything. Ex: Age, Gender.

PHP CURL Code: 
$key = 'YourAppceleratorKey';
$Curl_Session = curl_init("https://api.cloud.appcelerator.com/v1/users/create.json?key=" . $key.'&pretty_json=true&role=teacher&custom_fields={"pranay":23}');
 
curl_setopt($Curl_Session, CURLOPT_RETURNTRANSFER, true);    
$post_array = array('username' => "pgtest3", 'password' => "pgtest3" , 'password_confirmation' => "pgtest3");
curl_setopt($Curl_Session, CURLOPT_POSTFIELDS, $post_array);
$resp = curl_exec($Curl_Session);
curl_close($Curl_Session);

Reference Document: http://docs.appcelerator.com/cloud/latest/#!/api/Users-method-create

How to query on user's object fields and custom fields in Appcelerator API using PHP CURL?

Appcelerator Cloud Services provides APIs for querying and searching ACS objects. The query APIs allow you to perform custom database-style searches, while search APIs perform a full text search using the ACS search engine.

1. Query On Custom Field. For Example, My custom field is 'pranay'.
Scenario: Now fetch the user who having custom field value pranay = 23.

PHP CURL Code: 

$key = 'YourAppceleratorKeyHere';
$curl_handle = curl_init("https://api.cloud.appcelerator.com/v1/users/query.json?key=" . $key.'&pretty_json=true&count=true&where={"pranay":{"$eq":23}}');
curl_setopt($Curl_Session, CURLOPT_RETURNTRANSFER, true); $resp = curl_exec($curl_handle);
curl_close($curl_handle);
print_r($resp);

2. Query on predefined field like: username, first_name...etc.

PHP CURL Code: 

$curl_handle = curl_init("https://api.cloud.appcelerator.com/v1/users/query.json?key=" . $key.'&pretty_json=true&count=true&where={"username":"pgtest3"}');
curl_setopt($Curl_Session, CURLOPT_RETURNTRANSFER, true); $resp = curl_exec($curl_handle);
curl_close($curl_handle);
print_r($resp);

Note: In query you can use multiple operators sign like above used "$eq" which tends "equalto". please refer below link for more details.


Reference Document: http://docs.appcelerator.com/cloud/latest/#!/guide/search_query-section-query-on-custom-field-results-in-descending-order