1

please can you assist. I'm trying to get a ticket from the tableau server via PHP. Currently my ISP and my Server IP address is listed as trusted IPs on the tableau servers. If I use javascript on my remote server then I get the ticket but for some reason I cannot get any result with PHP and have tried a range of php code snippets. If I can get any one of them to work I will do the splits with joy.

NB: In the javascript version I need to enter the target_site which is the same as the username otherwise I don't get a result. Also note the :8000 port on the end of the url.

This is the working html/javascript version (returns a valid ticket, eg 128018285):

<script type="text/javascript">
function submitForm(){document.getElementById('form1').action = document.getElementById('server').value + "/trusted";}
</script>                      
<form method="POST" id="form1" onSubmit="submitForm()">
    <table class="style1">
        <tr>
            <td class="style2">
                Username:</td>
            <td>
                <input type="text" name="username" value="" /></td>
        </tr>
        <tr>
            <td class="style2">
                Server: </td>
            <td>
                <input type="text" id="server" name="server" value="http://" /></td>
        </tr>
        <tr>
            <td class="style2">
                Client IP (optional):</td>
            <td>
                 <input type="text" id="client_ip" name="client_ip" value="" /></td>
        </tr>
        <tr>
            <td class="style2">
                Site: (leave blank for Default site, else NameOfSite if using sites)</td>
            <td>
                <input type="text" id="target_site" name="target_site" value="" /></td>
        </tr>
        <tr>
            <td class="style2">
                        <input type="submit" name="submittable" value="Go" /></td>
            <td>
                 </td>
        </tr>
    </table>
</form>

Here is my code, snippet 1 using file_get_contents

$remote_addr = $_SERVER['REMOTE+ADDR'];  
  $params = array(
    'username' => 'myusername',
    'client_ip' => $remote_addr,
    'target_site' => 'myusername'
   );

    $context = stream_context_create($params);
    $ticket = file_get_contents('http://mysite.com:8000/trusted', false, $context);

  if ($ticket > 0) {
     return $ticket;
  }
  else
    return 0;

Another code snippet using curl

$server = 'myserver.com:8000';
$url = 'http://'.$server.'/trusted';
$fields_string ='target_site=myusername&username=myusername';

$ch = curl_init();      
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept-Encoding: gzip'));
curl_setopt($ch,CURLOPT_POST, 1);
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);

return curl_exec($ch);      
curl_close($ch);

Another code snippet using fopen

$url = 'http://myserver.com:8000/trusted';
$data = array ('username' => 'myusername','target_site' => 'myusername', 'format' => 'txt');
$data = http_build_query($data);
$params = array('http' => array(
                        'method' => 'POST',
                        'content' => $data,
                         'header'  => 'Content-type: application/x-www-form-urlencoded' . "\r\n"
              .'Accept-Encoding:' . "\r\n"
                    ));
        if($optional_headers != null)
        {
            $params['http']['header'] = $optional_headers;
        }
        $ctx = stream_context_create($params);
        $fp = @fopen($url, 'rb', false, $ctx);
        if (!$fp)
        {
            throw new Exception("Problem with $url, $php_errormsg");
        }
        $response='';
        while (!feof($fp))
        {
            $response = $response.fgets($fp);
        }
        if ($response === false)
        {
            throw new Exception("Problem reading data from $url, $php_errormsg");
        }

        fclose($fp);
        return $response;

Many Many Thanks in advance...

0

3 Answers 3

1

Tableau has sample PHP which works (it's running on my machine as I type). Have you tried it?

This post actually refers to the same sample code and extends it just a touch so it can be called by way of JS:

Generate tableau trusted ticket using AJAX

The sample code in question can be found in:

C:\Program Files (x86)\Tableau\Tableau Server\8.0\extras\embedding\php

It looks fairly similar to yours, but POSTs using http_post_fields()

    <?php

// Returns a trusted URL for a view on a server for the
// given user.  For example, if the URL of the view is:
//    http://tabserver/views/MyWorkbook/MyView
//
// Then:
//   $server = "tabserver";
//   $view_url = "views/MyWorkbook/MyView";
//
function get_trusted_url($user,$server,$view_url) {
  $params = ':embed=yes&:toolbar=yes';

  $ticket = get_trusted_ticket($server, $user, $_SERVER['REMOTE_ADDR']);
  if($ticket > 0) {
    return "http://$server/trusted/$ticket/$view_url?$params";
  }
  else 
    return 0;
}

// Note that this function requires the pecl_http extension. 
// See: http://pecl.php.net/package/pecl_http

// the client_ip parameter isn't necessary to send in the POST unless you have
// wgserver.extended_trusted_ip_checking enabled (it's disabled by default)
Function get_trusted_ticket($wgserver, $user, $remote_addr) {
  $params = array(
    'username' => $user,
    'client_ip' => $remote_addr
  );

  return http_parse_message(http_post_fields("http://$wgserver/trusted", $params))->body;
}

?>
1
  • Thanks Russell, I gave this code a go without success. Looks like I might need to debug the curl.
    – uknowit2
    Sep 24, 2013 at 12:58
1

Sorry, seems that my web host was blocking port 8000, which is why the code was not working.

For reference, this was the most concise piece of code that did the trick, change the variables for your own, myusername, mytargetsite, http://example.com/trusted

$opts = array('http' =>
array(
    'method'  => 'POST',
    'header'  => 'Content-type: application/x-www-form-urlencoded',
    'content' => 'username=myusername&target_site=mytargetsite'
)
);

$context  = stream_context_create($opts);

$result = file_get_contents('http://example.com/trusted', false, $context);

if ($result === false) {

throw new Exception("Problem reading data from $url, $php_errormsg");
}

else echo $result;
0

With your suggestion, I used cURL request and I have solved your problem with cURL request as following:

function get_trusted_ticket($wgserver, $user, $remote_addr) {
    $server = $wgserver;
    $url = 'http://'.$server.'/trusted';
    $fields_string ='target_site=$remote_addr&username=$user';

    $ch = curl_init($url);
    $data = array('username' => $user, 'client_ip' => $remote_addr);

    curl_setopt($ch, CURLOPT_POST, true); 
    curl_setopt($ch,CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    return curl_exec($ch);      
    curl_close($ch);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.