Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save PeterJuel/2f694f71ed19f3481ac0 to your computer and use it in GitHub Desktop.
Save PeterJuel/2f694f71ed19f3481ac0 to your computer and use it in GitHub Desktop.
<?php
$username = "_USERNAME_";
$password = "_PASSWORD_";
$sitename = ""; // blank for default
$URL = "_URL_";
////////////////
// Find Token (/api/2.0/auth/signin)
////////////////
$token = curlTableauToken($username, $password, $URL);
////////////////
// Query Site ID (/api/2.0/sites)
////////////////
$xml = curlTableauXML($URL."/api/2.0/sites", $token);
foreach($xml->sites->site as $site){
if($site->attributes()->contentUrl == $sitename){
$siteID = $site->attributes()->id;
}
}
////////////////
// Query User ID
/////////////////
$xml = curlTableauXML($URL."/api/2.0/sites/$siteID/users", $token);
foreach($xml->users->user as $user){
if($user->attributes()->name == $username){
$userID = $user->attributes()->id;
}
}
////////////////
// Query Workbooks on the site, from the user
/////////////////
$xml = curlTableauXML($URL."/api/2.0/sites/$siteID/users/$userID/workbooks", $token);
foreach($xml->workbooks->workbook as $workbook){
$id = $workbook->attributes()->id->__toString();
$books[$id]["name"] = $workbook->attributes()->name;
$books[$id]["contentUrl"] = $workbook->attributes()->contentUrl;
foreach($workbook->tags->tag as $tag){
$name = $tag->attributes()->label->__toString();
$tags[$name][] = $id;
}
}
////////////////
// Query First view in every workbook on the site, from the user
/////////////////
foreach($books as $id=>$book){
$xml = curlTableauXML($URL."/api/2.0/sites/$siteID/workbooks/$id/views", $token);
$books[$id]["view"] = $xml->views->view[0]->attributes()->contentUrl;
}
////////////////
// Layout
/////////////////
foreach($tags as $tag=>$wb){
echo "<h2>".$tag."</h2>";
foreach($wb as $id){
$url = "$URL/".(($sitename == "") ? "" : "site/".$sitename)."views/".str_replace("sheets/","",$books[$id]["view"]);
echo "<h4><a href='$url'>".$books[$id]["name"]."</a></h4>";
}
}
////////////////
// Functions
/////////////////
function curlTableauXML($url, $token = ""){
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("X-tableau-auth: $token"));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
$xml = simplexml_load_string($output);
if($xml->error->detail != null){
die("Error: ".$xml->error->detail);
}
return $xml;
}
function curlTableauToken($username,$password,$URL){
$payload = '
<tsRequest>
<credentials name="'.$username.'" password="'.$password.'" >
<site contentUrl="" />
</credentials>
</tsRequest>
';
$ch = curl_init($URL."/api/2.0/auth/signin");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);
$xml = simplexml_load_string($output);
$token = $xml->credentials->attributes()->token;
return $token;
}
@khannajam
Copy link

Please tell how to use it for server with no sites?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment