简体   繁体   English

Tableau使用PHP获取门票

[英]Tableau get ticket with PHP

please can you assist. 请你能帮忙。 I'm trying to get a ticket from the tableau server via PHP. 我正在尝试通过PHP从Tableau Server获得票证。 Currently my ISP and my Server IP address is listed as trusted IPs on the tableau servers. 当前,我的ISP和服务器IP地址在tableau服务器上被列为受信任的IP。 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. 如果我在远程服务器上使用javascript,则会获得票证,但由于某种原因,我无法使用PHP取得任何结果,并且尝试了一系列php代码段。 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. 注意:在javascript版本中,我需要输入与用户名相同的target_site,否则不会得到结果。 Also note the :8000 port on the end of the url. 另请注意网址末尾的:8000端口。

This is the working html/javascript version (returns a valid ticket, eg 128018285): 这是有效的html / javascript版本(返回有效票证,例如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 这是我的代码,使用file_get_contents的代码段1

$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 另一个使用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 使用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... 在此先感谢...

Tableau has sample PHP which works (it's running on my machine as I type). Tableau的示例PHP有效(当我键入时,它在我的计算机上运行)。 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: 这篇文章实际上引用了相同的示例代码,并对其进行了扩展,因此可以通过JS进行调用:

Generate tableau trusted ticket using AJAX 使用AJAX生成Tableau可信票证

The sample code in question can be found in: 有问题的示例代码可以在以下位置找到:

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

It looks fairly similar to yours, but POSTs using http_post_fields() 它看起来与您的非常相似,但是使用http_post_fields()的POST

    <?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;
}

?>

Sorry, seems that my web host was blocking port 8000, which is why the code was not working. 抱歉,似乎我的网络主机阻止了8000端口,这就是代码无法正常工作的原因。

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 作为参考,这是实现该技巧的最简洁的代码,您可以自己更改变量,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;

With your suggestion, I used cURL request and I have solved your problem with cURL request as following: 根据您的建议,我使用了cURL请求,并通过以下方法解决了您的cURL请求问题:

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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM