diff --git a/ntp-integration/php/ntp/config.ini b/ntp-integration/php/ntp/config.ini new file mode 100644 index 0000000..89d16fa --- /dev/null +++ b/ntp-integration/php/ntp/config.ini @@ -0,0 +1,18 @@ +[API] +api_base_url = 'https://dev.open-sunbird.org/api/' +api_org_create = 'org/v1/create' +api_org_update = 'org/v1/update' +api_user_create = 'user/v1/create' +api_user_update = 'user/v1/update' +api_user_associate = 'org/v1/member/add' +api_user_disassociate = 'org/v1/member/remove' + +header_content_type = 'application/json' +header_authorization = '' +provider = '' + +[CURL] +curl_header_out = true +curl_returntransfer = true +curl_post = true +curl_verify_ssl = true \ No newline at end of file diff --git a/ntp-integration/php/ntp/ntp.php b/ntp-integration/php/ntp/ntp.php new file mode 100644 index 0000000..1ec7aa0 --- /dev/null +++ b/ntp-integration/php/ntp/ntp.php @@ -0,0 +1,207 @@ + + * @see https://github.com/project-sunbird/sunbird-commons/wiki/Partner-Organization-and-User-APIs + */ +class Ntp +{ + private $_config; + + public function __construct() + { + $this->_config = parse_ini_file('config.ini'); + } + + /** + * This function helps create an organization with NTP for a partner + * + * @param Array $data Array of values needed by the API + * + * @return Array Status and response (optional) + */ + public function createOrganization($data) + { + $apiUrl = $this->_config['api_base_url'] . $this->_config['api_org_create']; + $apiBody = '{ + "request":{ + "orgName": "' . $data['name'] . '", + "description": "' . $data['description'] . '", + "externalId":' . $data['id'] . ', + "source":"' . $this->_config['provider'] . '" + } + }'; + + return $this->exec($apiUrl, $apiBody); + } + + /** + * This function helps update an organization with NTP for a partner + * + * @param Array $data Array of values needed by the API + * + * @return Array Status and response (optional) + */ + public function updateOrganization($data) + { + $apiUrl = $this->_config['api_base_url'] . $this->_config['api_org_update']; + $apiBody = '{ + "request":{ + "orgName": "' . $data['name'] . '", + "description": "' . $data['description'] . '", + "externalId":' . $data['id'] . ', + "source":"' . $this->_config['provider'] . '" + } + }'; + + return $this->exec($apiUrl, $apiBody); + } + + /** + * This function helps create a user with NTP for a partner + * + * @param Array $data Array of values needed by the API + * + * @return Array Status and response (optional) + */ + public function createUser($data) + { + $apiUrl = $this->_config['api_base_url'] . $this->_config['api_user_create']; + $apiBody = '{ + "request":{ + "firstName": "' . $data['fname'] . '", + "lastName": "' . $data['lname'] . '", + "provider":"' . $this->_config['provider'] . '", + "email": "' . $data['email'] . '", + "emailVerified":' . $data['emailVerified'] . ', + "userName":"' . $data['userName'] . '", + "phone": "' . $data['phone'] . '", + "phoneNumberVerified":' . $data['phoneVerified'] . ' + } + }'; + + return $this->exec($apiUrl, $apiBody); + } + + /** + * This function helps update a user with NTP for a partner + * + * @param Array $data Array of values needed by the API + * + * @return Array Status and response (optional) + */ + public function updateUser($data) + { + $apiUrl = $this->_config['api_base_url'] . $this->_config['api_user_update']; + $apiBody = '{ + "request":{ + "firstName": "' . $data['fname'] . '", + "lastName": "' . $data['lname'] . '", + "provider":"' . $this->_config['provider'] . '", + "email": "' . $data['email'] . '", + "emailVerified":' . $data['emailVerified'] . ', + "userName":"' . $data['userName'] . '", + "phone": "' . $data['phone'] . '", + "phoneNumberVerified":' . $data['phoneVerified'] . ' + } + }'; + + return $this->exec($apiUrl, $apiBody); + } + + /** + * This function helps associate a user and Organization with NTP for a partner + * + * @param Array $data Array of values needed by the API + * + * @return Array Status and response (optional) + */ + public function addUserToOrganization($data) + { + $apiUrl = $this->_config['api_base_url'] . $this->_config['api_user_associate']; + $apiBody = '{ + "request":{ + "externalId": "' . $data['orgId'] . '", + "source":"' . $this->_config['provider'] . '", + "userName": "' . $data['userName'] . '", + "role":' . $data['userRole'] . ' + } + }'; + + return $this->exec($apiUrl, $apiBody); + } + + /** + * This function helps dis-associate a user and Organization with NTP for a partner + * + * @param Array $data Array of values needed by the API + * + * @return Array Status and response (optional) + */ + public function removeUserFromOrganization($data) + { + $apiUrl = $this->_config['api_base_url'] . $this->_config['api_user_disassociate']; + $apiBody = '{ + "request":{ + "externalId": "' . $data['orgId'] . '", + "source":"' . $this->_config['provider'] . '", + "userName": "' . $data['userName'] . '" + } + }'; + + return $this->exec($apiUrl, $apiBody); + } + + /** + * Execute the CURL call to the NTP API + * + * @param String $url API Endpoint + * @param Array $data Array of data required by API + * @param Array $headers Array of header. If passed, default headers will be REPLACED by this. + * + * @return Array Status of CURL, response from NTP (if Status is success) + */ + protected function exec($url, $data = '', $headers = '') + { + // Prepare headers + $chHeaders = ""; + if ($headers) { + $chHeaders = $headers; + } else { + $chHeaders = array("Content-Type: " . $this->_config['header_content_type'], + "Authorization: " . $this->_config['header_authorization']); + } + + // Prepare CURL options + $chOptions = array(CURLOPT_URL => $url, + CURLINFO_HEADER_OUT => $this->_config['curl_header_out'], + CURLOPT_RETURNTRANSFER => $this->_config['curl_returntransfer'], + CURLOPT_POST => $this->_config['curl_post'], + CURLOPT_SSL_VERIFYPEER => $this->_config['curl_verify_ssl']); + + if ($chHeaders) { + $chOptions[CURLOPT_HTTPHEADER] = $chHeaders; + } + + if ($data) { + $chOptions[CURLOPT_POSTFIELDS] = $data; + } + + // Make a call + $ch = curl_init(); + curl_setopt_array($ch, $chOptions); + $response = curl_exec($ch); + + // Return + if (curl_error($ch)) { + return array('status' => 'error');; + } else { + return array('status' => 'success', + 'response' => $response);; + } + + curl_close($ch); + } +} diff --git a/ntp-integration/php/sample.php b/ntp-integration/php/sample.php new file mode 100644 index 0000000..da3f288 --- /dev/null +++ b/ntp-integration/php/sample.php @@ -0,0 +1,38 @@ + 'A V S S SUBBA', + 'email' => 'gsbajaj@apschooledu.in', + 'emailVerified' => 'true', + 'userName' => '9341229258', + 'phone' => '9341229258', + 'phoneVerified' => 'true', + ); + +$ntp = new Ntp(); +$resp = $ntp->createUser($data); + +echo "
";
+var_dump($resp);
+echo "
"; + + +/* +ERROR RESPONSE +array(2) { + ["status"]=> + string(7) "success" + ["response"]=> + string(327) "{"id":"api.user.create","ver":"v1","ts":"2017-07-31 04:42:15:103+0000","params":{"resmsgid":null,"msgid":"13cb6a7e-16fa-44ec-9634-6a1d9abc112f","err":"USERNAME_EMAIL_IN_USE","status":"USERNAME_EMAIL_IN_USE","errmsg":"Username is already in use. Please try with a different username."},"responseCode":"CLIENT_ERROR","result":{}}" +} + +SUCCESS RESPONSE +array(2) { + ["status"]=> + string(7) "success" + ["response"]=> + string(255) "{"id":"api.user.create","ver":"v1","ts":"2017-07-31 04:44:08:598+0000","params":{"resmsgid":null,"msgid":null,"err":null,"status":"success","errmsg":null},"responseCode":"OK","result":{"response":"SUCCESS","userId":"44291d87-e964-40d7-84a1-8498f92f0ec2"}}" +} +*/ \ No newline at end of file