您好,UncleToo欢迎您!  为了更好的浏览本站,请使用高版本浏览器
RSS  Tag     设为首页 | 加入收藏
 您所在的位置:首页 > PHP框架 > ZendFramework

Zend Framework集成GoToTraining API

作者:UncleToo  来源:翻译  日期:2014-01-02 11:09:22
收藏  评论:( 0 )  阅读:751

本文将给大家分享一个如何在Zend项目中集成GoToTraining API,有需求的同学们可以参考。

整合GoToTraining AP,首先我们需要有一个开发者账号(用来生成API密钥)和 Citrix 产品账号(授权使用OAuth 2.0的API密钥),具体授权过程请参考这里

授权成功后,你会得到以下相关凭证,需要存储后才能使用:

1、访问令牌:所有API调用必须使用此令牌请求。

2、更新令牌:当访问令牌快要过期时,用来更新访问令牌。

3、所有者密钥

4、账户类型

5、账户密钥


下面是几个种方法的集成实例:

1、使用POST方法

                    $uri = "https://api.citrixonline.com/G2T/rest/organizers/<YOUR
orGANIZER KEY>/trainings";
                    $params = array(
                        'name' => <NAME OF THE TRAINING>,
                        'description' => <DESCRIPTION OF THE TRAINING>,
                        'timeZone' => <TRAINING CONDUCTING TIMEZONE>,
                        'times' => <ARRAY OF TIME VALUES>,
                        'registrationSettings' => array(
                            'disableWebRegistration' => <TRUE/FALSE>,
                            'disableConfirmationEmail' => <TRUE/FALSE>
                        )
                    );
                    $postParams = array(
                        'oauth_token' => <YOUR ACCESS TOKEN>
                    );
                    $config = array(
                        'adapter' => 'Zend_Http_Client_Adapter_Curl',
                        'curloptions' => array(
                            CURLOPT_RETURNTRANSFER => false,
                            CURLOPT_HTTPHEADER => array("Accept: application/json",
                                "Content-Type: application/json")
                        )
                    );
                    $client = new Zend_Http_Client($uri, $config);
                    $client->setMethod(Zend_Http_Client::POST);
                    $client->setParameterGet($postParams);
                    $client->setRawData(json_encode($params), 'application/json');
                    try {
                        $response = $client->request();
                        if ($response->getStatus() == 201) {
                                // You will get the Training Key here by parsing the
response object.
                //You can code here for any other functionality after
successful creation of Training.
                        }
                    } catch (Exception $e) {
                        echo "Unable to Schedule the Training. Please try again";
                    }

2、使用PUT方法

                    $postParams = array(
                        'oauth_token' => <YOUR ACCESS TOKEN>
                    );
                    //UPDATING TRAINING NAME & DESCRIPTION
                    $uri = "https://api.citrixonline.com/G2T/rest/organizers/<YOUR
orGANIZER KEY>/trainings/<YOUR STORED TRAINING KEY>/nameDescription";
                    $params = array(
                        'name' => <UpdateD NAME OF THE TRAINING>,
                        'description' => <UpdateD TRAINING DESCRIPTION>
                    );
                    $client = new Zend_Http_Client($uri);
                    $client->setMethod(Zend_Http_Client::PUT);
                    $client->setHeaders(array("Accept: application/json","Content-Type:
application/json"));
                    $client->setParameterGet($postParams);
                    $client->setRawData(json_encode($params), 'application/json');
                    try {
                        $response = $client->request();
                        if ($response->getStatus() == 204) {
                            //Successfully updated the Training Name & Description details
                        }
                    } catch (Exception $e) {
                        echo "Unable to update Training Name & Description details. Please
try again";
                    }
                    $client = "";
                    //Update TRAINING REGISTRATION SETTINGS
                    $uri = "https://api.citrixonline.com/G2T/rest/organizers/<YOUR
orGANIZER KEY>/trainings/<YOUR STORED TRAINING KEY>/registrationSettings";
                    $params = array(
                        'disableWebRegistration' => <TRUE/FALSE>,
                        'disableConfirmationEmail' => <TRUE/FALSE>
                    );
                    $client1 = new Zend_Http_Client($uri);
                    $client1->setMethod(Zend_Http_Client::PUT);
                    $client1->setHeaders(array("Accept: application/json","Content-Type:
application/json"));
                    $client1->setParameterGet($postParams);
                    $client1->setRawData(json_encode($params), 'application/json');
                    try {
                        $response1 = $client1->request();
                        if ($response1->getStatus() == 204) {
                            //Successfully updated the Training registration settings
                        }
                    } catch (Exception $e) {
                        echo "Unable to update Training registration settings. Please try
again";
                    }
                    //Update TRAINING TIMING DETAILS
                    $uri = "https://api.citrixonline.com/G2T/rest/organizers/<YOUR
orGANIZER KEY>/trainings/<YOUR STORED TRAINING KEY>/times";
                    $params = array(
                        'timeZone' => <UpdateD TRAINING TIMEZONE>,
                        'times' => <ARRAY OF UpdateD TIME VALUES>,
                        'notifyRegistrants' => <TRUE/FALSE>,
                        'notifyTrainers' => <TRUE/FALSE>
                    );
                    $client2 = new Zend_Http_Client($uri);
                    $client2->setMethod(Zend_Http_Client::PUT);
                    $client2->setHeaders(array("Accept: application/json","Content-Type:
application/json"));
                    $client2->setParameterGet($postParams);
                    $client2->setRawData(json_encode($params), 'application/json');
                    try {
                        $response2 = $client2->request();
                        if ($response2->getStatus() == 200) {
                            //Successfully updated the Training time details
                        }
                    } catch (Exception $e) {
                        echo "Unable to update Training time details. Please try again";
                    }

3、使用Delete方法

                    $postParams = array(
                        'oauth_token' => <YOUR ACCESS TOKEN>
                    );
                    //Before deleting, checking for any registrants. This step can be
ignored if you don't want
                    $uri = "https://api.citrixonline.com/G2T/rest/organizers/<YOUR
orGANIZER KEY>/trainings/<YOUR STORED TRAINING KEY>/registrants";
                    $config = array(
                        'adapter' => 'Zend_Http_Client_Adapter_Curl',
                        'curloptions' => array(
                            CURLOPT_RETURNTRANSFER => false,
                            CURLOPT_HTTPHEADER => array("Accept: application/json",
                                "Content-Type: application/json")
                        )
                    );
                    $client = new Zend_Http_Client($uri, $config);
                    $client->setMethod(Zend_Http_Client::GET);
                    $client->setParameterGet($postParams);
                    try {
                        $response = $client->request();
                        if ($response->getStatus() == 200) {
                            $arrResponse = json_decode($response->getBody(), true);
                            if(is_array($arrResponse) && count($arrResponse) == 0){
                                //DELETING TRAINING DETAILS
                                $uri =
"https://api.citrixonline.com/G2T/rest/organizers/<YOUR orGANIZER KEY>/trainings/<YOUR
STORED TRAINING KEY>";
                                $client1 = new Zend_Http_Client($uri, $config);
                                $client1->setMethod(Zend_Http_Client::Delete);
                                $client1->setParameterGet($postParams);
                                try {
                                    $response1 = $client1->request();
                                    if ($response1->getStatus() == 204) {
                                        //Successfully deleted the scheduled training
details
                                    }
                                } catch (Exception $e) {
                                    echo "Unable to cancel Training. Please try again";
                                }
                                $client = "";
                            }
                            else{
                                echo "Training cannot be deleted. Registrants exists";
                            }
                        }
                    } catch (Exception $e) {
                        echo "Unable to cancel Training. Please try again";
                    }
                    $client = "";

你也可以按照上面的方法,通过改变URI和参数值来集成其他GoToTraining API方法。


本文由UncleToo翻译整理,转载请注明出处!

原文(英文)地址:http://geekw3.com/2013/07/25/gototraining-api-integration-with-zend-php-framework/




除非特别声明,本站所有PHP教程及其他教程/文章均为原创、翻译或网友投稿,版权均归UncleToo中文网所有, 转载请注明作者及出处。
原文网址:http://www.uncletoo.com/html/zend/767.html
读完这篇文章后,你是否有所收获? 分享是一种生活的信念!
  • 0
  • 0
我来说两句
更多>>网友评论
UncleToo,一档有态度的PHP中文网

图片教程