示例#1
0
/**
 * Register with a remote application and create a new connection.
 *
 * One should generally identify an application using the app_guid.
 * However, if you need to test a new/experimental application, then
 * disable CIVICRM_CXN_CA and specify app_meta_url.
 *
 * @param array $params
 *   Array with keys:
 *   - app_guid: The unique identifer of the target application.
 *   - app_meta_url: The URL for the application's metadata.
 * @return array
 * @throws Exception
 */
function civicrm_api3_cxn_register($params)
{
    if (!empty($params['app_meta_url'])) {
        list($status, $json) = CRM_Utils_HttpClient::singleton()->get($params['app_meta_url']);
        if (CRM_Utils_HttpClient::STATUS_OK != $status) {
            throw new API_Exception("Failed to download appMeta. (Bad HTTP response)");
        }
        $appMeta = json_decode($json, TRUE);
        if (empty($appMeta)) {
            throw new API_Exception("Failed to download appMeta. (Malformed)");
        }
    } elseif (!empty($params['app_guid'])) {
        $appMeta = civicrm_api3('CxnApp', 'getsingle', array('appId' => $params['app_guid']));
    }
    if (empty($appMeta) || !is_array($appMeta)) {
        throw new API_Exception("Missing expected parameter: app_guid");
    }
    \Civi\Cxn\Rpc\AppMeta::validate($appMeta);
    try {
        /** @var \Civi\Cxn\Rpc\RegistrationClient $client */
        $client = \Civi::service('cxn_reg_client');
        list($cxnId, $result) = $client->register($appMeta);
        CRM_Cxn_BAO_Cxn::updateAppMeta($appMeta);
    } catch (Exception $e) {
        CRM_Cxn_BAO_Cxn::updateAppMeta($appMeta);
        throw $e;
    }
    return $result;
}