Created
May 1, 2012 01:05
-
-
Save jeremysuriel/2564090 to your computer and use it in GitHub Desktop.
Desk.com Single Access Token API - PHP Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// | |
// Desk.com Single Access Token API Example | |
// | |
// 1) Copy this file to the root of your webserver and change the $my_desk_url variable to point to your desk.com hostname | |
// 2) Go to https://[yoursite].desk.com/admin/settings/api-applications and create an API Application | |
// 3) Copy the consumer key & secret on the listing page after you create your API Application | |
// 4) Click the "Your Access Token" link to get your single access token & secret | |
// 5) Copy your single access token & secret and paste it into the appropriate variable in this file | |
// | |
// Note: This was tested with PHP 5.4 with OAuth Extension installed | |
// | |
$my_desk_url = 'https://[your site].desk.com'; | |
//Application key and secret found here: | |
// https://[yoursite].desk.com/admin/settings/api-applications | |
$consumer_key = '[your consumer key]'; | |
$consumer_secret = '[your consumer secret]'; | |
//Access token & secret (Click [Your Access Token] on App Listing) | |
// https://[yoursite].desk.com/admin/settings/api-applications) | |
$access_token = '[your access token]'; | |
$access_secret = '[your access secret]'; | |
try { | |
//Create a new Oauth request. | |
$oauth = new OAuth($consumer_key, $consumer_secret); | |
$oauth->enableDebug(); | |
$oauth->setToken($access_token,$access_secret); | |
$action = 'list'; | |
if(isset($_GET['action'])) $action = $_GET['action']; | |
switch($action) { | |
case 'resolve': | |
$id = $_GET['id']; | |
//Example of a PUT API action - resolve a case | |
$oauth->fetch($my_desk_url."/api/v1/cases/".$id.".json",array('case_status_type_id'=>70),OAUTH_HTTP_METHOD_PUT); | |
header('Location: /desk-oauth-example-single.php'); | |
break; | |
case 'show': | |
$id = $_GET['id']; | |
//Example of GET Show action | |
$oauth->fetch($my_desk_url."/api/v1/cases/".$id.".json",array(),OAUTH_HTTP_METHOD_GET); | |
//Get Response | |
$json = json_decode($oauth->getLastResponse()); | |
echo "<a href='/desk-oauth-example-single.php'>BACK</a>"; | |
print_obj($json->case); | |
break; | |
default: | |
//Example of a List action | |
//Parse query parameters | |
$query = ''; | |
if(isset($_GET['email'])) { | |
$query = '?email='.$_GET['email']; | |
} | |
//Sample GET Request | |
$oauth->fetch($my_desk_url."/api/v1/cases.json".$query,array(),OAUTH_HTTP_METHOD_GET); | |
//Get Response | |
$json = json_decode($oauth->getLastResponse()); | |
echo "<h3>Total Cases: ".($json->total)."</h3>"; | |
echo "<ul>"; | |
foreach($json->results as $key => $value) { | |
$case = $value->case; | |
echo "<li>"; | |
echo "<b>Case #".($case->id).": ".($case->subject)."</b>"; | |
if (!in_array($case->case_status_type, array("resolved", "closed"))) { | |
echo " (<a href='/desk-oauth-example-single.php?action=resolve&id=".($case->id)."'>resolve</a>)"; | |
} else { | |
echo " (".$case->case_status_type.")"; | |
} | |
echo " (<a href='/desk-oauth-example-single.php?action=show&id=".($case->id)."'>details</a>)"; | |
//echo "<br/>".($case->preview); | |
echo "</li>"; | |
} | |
echo "</ul>"; | |
//print_r($json); | |
break; | |
} | |
} catch(OAuthException $E) { | |
print_r($E); | |
} | |
function print_obj($obj){ | |
echo "<ul>"; | |
foreach($obj as $key => $value) { | |
echo "<li>"; | |
echo "<b>".($key)."</b> "; | |
if (is_object($value)) { | |
print_obj($value); | |
} else if (is_array($value)) { | |
echo implode(",", $value); | |
} else { | |
echo $value; | |
} | |
echo "</li>"; | |
} | |
echo "</ul>"; | |
} | |
?> |
Hi jsiesquen,
Did you manage to make this work with V2? I want to make that work with V2 too, but I have the same issues as you.
Regards,
tehknox
Hi
I need your help this code is not working for me.Please help me i need to implement Desk.com for multiple users
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi Jeremy,
i'm working in getting data but from v.2. of Desk API.
I upgrade your code but shown me some errors at run the script without GET parameters:
Notice: Undefined property: stdClass::$total in C:\xampp\desk-oauth-example-single.php on line 77
Total Cases:
Notice: Undefined property: stdClass::$results in C:\xampp1\desk-oauth-example-single.php on line 79
Warning: Invalid argument supplied for foreach() in C:\xampp\desk-oauth-example-single.php on line 79
Using GET parameters as: desk-oauth-example-single.php?action=show&id=17517; no have errors if change
print_obj($json->case); by print_obj($json);
I using: PHP 5.4.16
Update the all reference of v1 to v2, please can support me? Thanks very much! Try make my tests....