How do I call CITRIX (LogMeIn) API via PHP to register new GotoWebinar attendee?

陌路散爱 提交于 2019-12-01 01:33:18
<?php
if (isset($_POST['registration-submission'])) {
$base_url = 'https://api.citrixonline.com/G2W/rest';
$org_key = $_POST['organizer_key'];
$web_key = $_POST['webinar_key'];
$access_token = 'xxxxx';

$gtwPost = (object) array(
'firstName' => $POST['fname'],
'lastName'  => $POST['lname'],
'email'=> $POST['email'],
);
$newRegistrantFields = json_encode($gtwPost);
$headers = array(
        'HTTP/1.1',
        'Accept: application/vnd.citrix.g2wapi-v1.1+json',
        'Content-Type: application/json',
        'Authorization: OAuth oauth_token=xxxxx',
         'Content-Length:' . strlen( $newRegistrantFields )
        );


//Set POST URL for GoToWebinar
$gtw_url = $base_url.'/organizers/'.$org_key.'/webinars/'.$web_key.'/registrants?resendConfirmation=false';

//Start GoToWebinar submission

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $gtw_url);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt( $curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0 );
curl_setopt( $curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt( $curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt( $curl, CURLOPT_POST, 1);
curl_setopt( $curl, CURLOPT_POSTFIELDS,$newRegistrantFields );
$newRegistrants = curl_exec($curl);
curl_close($curl);
$newRegistrantsArray = json_decode($newRegistrants,true);


 if( array_key_exists( 'registrantKey', $newRegistrantsArray ) && array_key_exists( 'joinUrl', $newRegistrantsArray ) ) {
    $form_data[ 'status' ] = true;
    $form_data[ 'code'   ] = $_POST[ 'Email' ] . ' successfully registered with webinar';
    $form_data[ 'error'  ] = 'E300';

    //echo json_encode( $form_data );
 //exit;
 }
}
?>

the code is not working. Please help me with this.

The question is 3 years old, but considering the present dismal state of the CITRIX (now LogMeIn) API documentation, I'm offering the following snippet as a possible solution:

Obviously, we'll need the Organizer Key and Access Token data for our account...

    $organizer_key= '10000000000XXXXXXX';
    $access_token = 'GwsiiPWaJbHIiaIiocxxxxxxxxxx';

Get the minimum required fields for a webinar (for example from an HTML form) and JSON encode the data...

    $newRegFields = (object) array(
        'firstName' => $_POST[ 'FirstName' ],
        'lastName'  => $_POST[ 'LastName'  ],
        'email'     => $_POST[ 'Email'     ],
    );

    $newRegistrantFields = json_encode( $newRegFields );

    //echo '<br><br>' . $newRegistrantFields;

Get the Webinar...

    $webinarID = preg_replace( "/[^0-9]/", "", $_POST[ "WebinarKey" ] );

Set the URL to the LogMeIn API (the resendConfirmation option is not required)...

    $gtw_url = "https://api.citrixonline.com/G2W/rest/organizers/" . $organizer_key . "/webinars/" . $webinarID . "/registrants?resendConfirmation=false";

Format our POST headers...

    $headers = array(
        "HTTP/1.1",
        "Accept: application/json",
        "Content-Type: application/json",
        "Authorization: OAuth oauth_token=$access_token",
        "Content-Length: " . strlen( $newRegistrantFields )
    );

Set our cURL options, ensuring we specify a POST with CURLOPT_POST, 1 ...

    $curl = curl_init();

    curl_setopt( $curl, CURLOPT_URL, $gtw_url                       );
    curl_setopt( $curl, CURLOPT_HTTPHEADER, $headers                );
    curl_setopt( $curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0 );
    curl_setopt( $curl, CURLOPT_SSL_VERIFYPEER, 0                   );
    curl_setopt( $curl, CURLOPT_RETURNTRANSFER, 1                   );
    curl_setopt( $curl, CURLOPT_POST, 1                             );
    curl_setopt( $curl, CURLOPT_POSTFIELDS, $newRegistrantFields    );

    $newRegistrants = curl_exec( $curl );
    curl_close( $curl );

Our cURL call has returned with JSON encoded data, whether it's a server error message or a confirmation of registration. Now let's turn the reply into a handy associative array...

    $newRegistrantsArray = json_decode( $newRegistrants, true );

    //echo '<br><br>' . $newRegistrants . '<br><br>';
    //echo '<pre>'; print_r( $newRegistrantsArray ); echo '</pre>';

If the errorCode key was returned, then the registration bombed out. All I'm doing here is grabbing the actual error description from the server and loading it up to return to my calling HTML page, but this is totally optional...

    if( array_key_exists( 'errorCode', $newRegistrantsArray )) {
        $form_data[ 'status' ] = false;
        $form_data[ 'code'   ] = $newRegistrantsArray[ 'description' ];
        $form_data[ 'error'  ] = 'E200';
        //echo json_encode( $form_data );
        //exit;
    }

Now, if a registration was successful, the server will return something like...

(
  [registrantKey] => 2.5022062212198E+18
  [joinUrl] => https://global.gotowebinar.com/join/6552167171182613761/103193261
) 

...and so I'm just checking to see if those keys were returned, and if so, I know the registration was good.

    if( array_key_exists( 'registrantKey', $newRegistrantsArray ) && array_key_exists( 'joinUrl', $newRegistrantsArray ) ) {
        $form_data[ 'status' ] = true;
        $form_data[ 'code'   ] = $_POST[ 'Email' ] . ' successfully registered with webinar';
        $form_data[ 'error'  ] = 'E300';
        //echo json_encode( $form_data );
        //exit;
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!