Hi community,
I registered for a special request, I have gotten and I struggle in resolving it. Let me explain the need:
I should create an Intranetpage, which should show a value taken from our company salesforce. There is a dashboard in salesforce, which I’d need to access and parse off some values there. Dashboards in salesfoce do have a static URL, like “https://companyname.my.salesforce.com/01Z60000000ipt3”. When I download the website locally, e.g. to http://localhost/simulation/index.html, I can use my script perfectly:
[php]<?php
//— Varibales you may use after including this php —//
/*
$taccasecount Displays the number of Backup Cases in Tac EMEA
*/
//— Fetching website and searching for the string above —//
try {
$content = file_get_contents(“http://localhost/simulation/index.html”); // get HTML code from Website
$search = ‘#Backup:(.)([0-9])#’; // search string
//--- Error handling ---//
if (!$content) {
echo "Error: Unable to fetch website." . PHP_EOL;
echo "Debugging errno: " . file_get_contents_errno() . PHP_EOL;
echo "Debugging error: " . file_get_contents_error() . PHP_EOL;
} else {
//--- If no error: run the function ---/
if (!preg_match($search, $content, $casecountmatch)) {
echo "Error: Unable to find search string." . PHP_EOL;
} else {
preg_match($search, $content, $casecountmatch);
$taccasecount = $casecountmatch[1];
}
}
print_r("<p>Backup Cases: $taccasecount</p>"); // test output
//— If boolean <> false, but return is still bad —//
} catch (Exception $e) {
echo “Error: Unknown Exception” . PHP_EOL;
}
?>[/php]
I may even use file_get_contents function from PHP. However, when I am trying to directly access the webpage, I am getting stuck at a redirect. I assume it is javascript. The plaintext output from file_get_contents for https://companyname.my.salesforce.com/01Z60000000ipt3 is:
[code]
[/code]I found that file_get_contents is probably not the rigth way to grab the html, as it does not follow redirects. So i tried working with cURL. No matter, what I did, I am still getting the same output. I know that the website is https and is using an authentication. So i tried both: cURL w/ and w/o post of credentials. However, as long as I work with a browser, which has the salesforce cookies still, this should work w/o auth - they do, when I link to pictures in the same dashboard from my intranet page.
I really tried many things, which can be found with google, so I do not have a list, with stuff i tried. Also it’S quite fun that i need to set header(‘Content-type: text/plain’); before my content output to even see the the above - otherwise I am getting redirected to the correct site.
my last attempt was installing guzzle and using this snippet:
[php]<?php
require ‘…/…/…/vendor/autoload.php’;
use GuzzleHttp\Client;
$client = new Client([
//Base URI is used with relative requests
//MUST BE a qualified url, ex. http://
‘base_uri’ => ‘https://companyname.my.salesforce.com’,
//optionally set a timeout
// You can set any number of default request options.
‘timeout’ => 2.0,
]);
$response = $client->request(‘GET’, ‘https://companyname.my.salesforce.com/01Z60000000ipt3’);
header(‘Content-type: text/plain’);
//returns a GuzzleHttp\Psr7\Stream
$body = $response->getBody();
// Implicitly cast the body to a string and echo it
echo $body;
echo $response->getStatusCode();
?>[/php]
Another attempt was this one: http://at1.php.net/manual/en/ref.curl.php#93163
Everytime the same: The script gots stuck at the redirect. Does anyone have an idea, how I could get the data from the page? I am also open to completely different solutions. The only thing I cannot do at this time is to get permissions to use any API from salesforce (REST or something) to work with. I can only use my personal login and access to the data, which is already there.
Cheers
RamsesV