I generally use Wordpress when building my internally facing dashboards with Tableau, but that will not work in this case so I am starting from scratch. I'm a novice with PHP and slightly above novice with jQuery, but I know my way around HTML. I need to pass an Okta javascript parameter into a Tableau embed. Below is my current code:
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
var settings = {
url: "https://harmelin.okta.com/api/v1/users/me",
type: 'GET',
dataType: 'json',
contentType: 'application/json',
xhrFields: {
withCredentials: true
},
success: function (data) {
// alert(JSON.stringify(data));
},
error: function(err){
// alert(JSON.stringify(err));
}
}
jQuery.ajax(settings).done(function (success) {
console.log(success);
var raw = success.profile.login;
var email = raw.toLowerCase();
var $login = email.replace(/#[^#]+$/, '');
jQuery("#write-data").append($login);
});
</script>
</head>
<body>
<?php
// Tableau-provided functions for doing trusted authentication
require_once 'tableau_trusted.php';
?>
<div id="write-data"></div>
<?php
$user = 'jfedorowicz';
$server = 'dashboard1.harmelin.com';
$view = 'JoesPlayground/views/PTOStuff/Dashboard1?LCUsername=';
$theLogin = $login;
echo '<iframe src="';
echo get_trusted_url( $user,$server,$view$theLogin );
echo '" width="400" height="400"> </iframe>';
?>
</body>
Two problems:
1) What I am looking to do is pass $login (javascript variable) into $view. I 100% know that I did not do this right, but I cannot test because problem two.
2) I'm returning a 500 error: "Failed to load resource: the server responded with a status of 500 (Internal Server Error)" which I assume is a Tableau error but I cannot figure it out.
Any ideas? Thanks.
To generate authorization token you can use given code.
This code is working fine with all version upto 2018.3
/* PHP code to generate auth token */
$url = $dashboardURL . '/trusted';
$fields_string = "trusted_site=&username=" . $username;
$ch = curl_init();
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120);
curl_setopt($ch, CURLOPT_TIMEOUT, 120);
//set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
$token = curl_exec($ch);
Now just pass this token with dashboard url in your view/html file.
Related
I am using below javascript and html to get the json data. But i can't make it work with this api.It just return blank result. I have checked the api link is working so i don't think is the server problem. Please help!
html
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="balance"></div>
javascript
$(function() {
$.ajax({
type: "GET",
url:"https://xmg.minerclaim.net/index.php?page=api&action=public",
dataType: "json",
success: function(data) {
console.log(typeof data); // -- Object
var json = data;
$('#balance').html(json.hashrate);
}
});
});
json data
{"pool_name":"minerclaim.net","hashrate":60185.64096,"workers":1056,"shares_this_round":168700,"last_block":1531882,"network_hashrate":61752985,"fee":1,"payout":"prop"}
As I mentioned above, you are getting bool(true) in your PHP output, because the usage of var_dump().
An example of a PHP file you could use:
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://xmg.minerclaim.net/index.php?page=api&action=public");
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 0);
$headers = array();
$headers[] = "Content-Type: application/json; charset=utf-8";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Some error:' . curl_error($ch);
die();
}
// You want your browser to think it's JSON, as we return as JSON
header("Content-Type: application/json; charset=utf-8");
echo($result);
curl_close($ch);
?>
You can copy + paste this into a new PHP file, point the $.ajax-url to that new file and see the magic happen. The example above works for me as I'm getting results back.
As for your javascript, you could also try to add contentType: "application/json", after `dataType: "json",", as this will make sure the content-type of the headers that are returned are set.
So my problem is that I need to update some data from other site, and for calling that data I have php function where is the URL as parameter. ..So in JS I create a function that is in cycle with setInterval where I call that php function with URL parameter where are data stored, but it always return the same data..(data is actually playing track on stream, so data changed every +- 3 minutes) Data changes only on refresh page (f5) ..but I need update that data in background ..
this is the PHP function
function get_content($URL){
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $URL);
$data = curl_exec($ch);
curl_close($ch);
$data = str_replace(",,","},",$data);
$data = str_replace("}}]}}","}]}}",$data);
$data = str_replace("]}}","}]}}",$data);
$data = str_replace(",}}","}}}",$data);
$data = str_replace("}}]}}","}]}}",$data);
return $data;
In js I call in setInterval cycle only console.log to show result of php function..
console.log(<?php echo (get_content("http://server1.internetoveradio.sk:8809/status-json.xsl"));?>["icestats"]["source"])
Well, yeah. The PHP only gets called once in this case, the one time you echo out the contents of get_content();
If you want to get the content over and over again, use XmlHTTPRequest to call a PHP file which then returns the the result of get_content();
jQuery implements ajax ( XmlHTTPRequest ) to do exactly that.
jQuery.ajax({
url: "http://path.to/your_script.php",
method: "get",
complete: function( response ){
console.log(response);
}
});
edit:
Create a new .php file and paste this:
<?php
function get_content($URL){
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $URL);
$data = curl_exec($ch);
curl_close($ch);
$data = str_replace(",,","},",$data);
$data = str_replace("}}]}}","}]}}",$data);
$data = str_replace("]}}","}]}}",$data);
$data = str_replace(",}}","}}}",$data);
$data = str_replace("}}]}}","}]}}",$data);
return $data;
}
echo get_content("http://server1.internetoveradio.sk:8809/status-json.xsl");
In your html, add this:
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script>
jQuery(document).ready(function(){
jQuery.ajax({
url: "http://path.to/your_script.php",
method: "get",
complete: function( response ){
console.log(response);
}
});
});
</script>
This is the most rudimentory version, but it should point you in the right direction hopefully.
I am using cURL to try to login in this website https://myportal.vtc.edu.hk/wps/portal/ , but i found that there is a javascript function
<"onclick="GoLogin(); return false;">
to control the login access.
so i can't connect to this website, this is my code below,
<?php
$username = 'userid';
$password = 'password';
$loginUrl = 'https://myportal.vtc.edu.hk/wps/portal';`
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $loginUrl);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'userid='.$username.'&password='.$password );
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$store = curl_exec($ch);
curl_setopt($ch, CURLOPT_URL, 'https://myportal.vtc.edu.hk/wps/myportal/!ut/p/c4/04_SB8K8xLLM9MSSzPy8xBz9CP0os3hTC39DAxNz98CwMEtTA08f50DjQEdzIwMDI_2CbEdFABEW-jM!/');
$content = curl_exec($ch);
curl_close($ch);
?>
This is the GoLogin() function :
$.ajax({
url: loginForm.attr('action'),
cache: false,
async: false,
data: params,
type: "POST",
complete: function(jqXHR, textStatus) {
if (jqXHR.status == 200) {
// login successfull
window.location = '/wps/myportal';
} else if (jqXHR.status == 399) {
// login successfull
window.location = jqXHR.responseText;
}
how can i login to this website, please help me.
This way you will not able to connect as via curl you can not execute the javascript.
For above requirement you should use the http://phantomjs.org/ .
Its php library is also available.
https://github.com/jonnnnyw/php-phantomjs
Or you can look at http://slimerjs.org/.
I am trying to load JS file through curl request and load it in my app.
I have something like
loadJS.php
$c = curl_init('http://partnerSite.com/test.js');
curl_setopt($c, CURLOPT_VERBOSE, 1);
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
$jsFile = curl_exec($c);
//I have tried this, give me error
echo $jsFile;
//I have tried this and give me error too.
header('Content-Type: application/json');
echo json_encode($jsFile);
curl_close($c);
In my JS
var url = '/loadoutSideJS'; redirect to loadJS.php
$http({
url:url,
method:'GET',
dataType:'json'
}).success(function(data) {
$.getScript(data);
})
I am getting 414 request-url too large error in the console
For some reason, the response is treated as plain texts instead of js file. Is there anyway I can make the response treated as JS to load the external JS file? Thanks a lot!
Updated
loadJS.php
$c = curl_init('http://partnerSite.com/test.js');
curl_setopt($c, CURLOPT_VERBOSE, 1);
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
$jsFile = curl_exec($c);
//I have tried this and give me error too.
header('Content-Type: application/javascript');
echo $jsFile;
curl_close($c);
In my JS
var url = '/loadoutSideJS'; redirect to loadJS.php
$http({
url:url,
method:'GET'
}).success(function(data) {
$.getScript(data);
})
Why not
<?php
header('Content-Type: application/javascript');
echo file_get_contents('http://partnerSite.com/test.js');
I'm trying to build a domain availability checker.
My PHP code is the following:
<?php
$domain = $_GET["domname"];
function get_data($url) {
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$returned_content = get_data('http://freedomainapi.com/?key=XXXXXXX&domain=' . $domain);
echo $returned_content;
?>
When called as such: /domchek.php?domname=google.com
The JSON output is:
{"status":"success","domain":"google.com","available":true}
The JQuery im using to call the script is as follows:
$.get("domchek.php?domname=google.com", {data: "available"}, function(json) {
$("html").html(json);
});
I just want to return the availability not the entire JSON output. I have tried json.availability and a number of other things but can't figure it out. Also if there is a better method for this than .get() please suggest it.
UPDATE:
$.getJSON("domchek.php?domname=google.com", function(json) {
$("html").html(json.status);
});
The above works as does returning json.domain but trying to return json.available - which I require returns nothing..
Change this line:
$("html").html(json);
to:
$("html").html(json.status);
UPDATE: to force the GET request data type to JSON, change to:
$.get("domchek.php?domname=google.com", {data: "available"}, function(json) {
$("html").html(json);
}, 'json');