请帮忙。我正试图通过PHP从tableau服务器获得一张票。目前,我的ISP和我的服务器IP地址在tableau服务器上列为可信IP。如果我在我的远程服务器上使用javascript然后我得到了票但由于某种原因我无法获得PHP的任何结果并尝试了一系列的PHP代码片段。如果我能让他们中的任何一个人工作,我会高兴地分手。
注意:在javascript版本中,我需要输入与用户名相同的target_site,否则我不会得到结果。另请注意:url末尾的8000端口。
这是工作的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>
这是我的代码,使用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;
使用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);
另一个使用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;
许多人提前感谢...
答案 0 :(得分:1)
Tableau有一个可用的示例PHP(它在我键入的机器上运行)。你试过吗?
这篇文章实际上是指相同的示例代码并只是触摸它,因此它可以通过JS调用:
Generate tableau trusted ticket using AJAX
有问题的示例代码可在以下网址找到:
C:\ Program Files(x86)\ Tableau \ Tableau Server \ 8.0 \ extras \ embedding \ php
它看起来与你的相似,但使用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;
}
?>
答案 1 :(得分:0)
很抱歉,我的网络主机似乎阻止了端口8000,这就是代码无效的原因。
作为参考,这是完成这一操作的最简洁的代码片段,更改您自己的变量,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;
答案 2 :(得分:0)
根据您的建议,我使用了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);
}