I'm trying to authenticate and download the Flurry event log via php & cURL. The post by silkfire got me part of the way, but I'm still having trouble getting the download. I believe I've authenticated, but the download seems to return an error page instead of the file. Here's the latest code I'm trying:
$post = array(
'loginEmail' => 'email',
'loginPassword' => 'pw',
//'rememberMeCheckbox' => 'true',
//'__checkbox_rememberMeCheckbox' => 'true',
'struts.token.name' => 'token',
);
//$ckfile = tempnam("/tmp", "FLURRYCOOKIE");
$ch = curl_init('https://dev.flurry.com/secure/login.do');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_COOKIEFILE, null);
//curl_setopt ($ch, CURLOPT_COOKIEJAR, $ckfile);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
libxml_use_internal_errors(true);
$dom = new DOMDocument('1.0', 'UTF-8');
$dom->loadHTML(curl_exec($ch));
$xpath = new DOMXPath($dom);
$post['token'] = $xpath->query('//input[@name="token"]')->item(0)->getAttribute('value');
curl_setopt($ch, CURLOPT_URL, 'https://dev.flurry.com/secure/loginAction.do');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));
//curl_setopt( $ch, CURLOPT_COOKIEFILE, $ckfile );
//curl_setopt($ch, CURLOPT_COOKIESESSION, true );
//curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
$login = curl_exec($ch);
$log_url = 'https://dev.flurry.com/eventsLogCsv.do?projectID=12345&versionCut=versionsAll&intervalCut=30Days&childProjectId=0&stream=true&direction=1&offset=0';
$ch = curl_init();
//curl_setopt( $ch, CURLOPT_COOKIEFILE, $ckfile );
curl_setopt($ch, CURLOPT_URL, $log_url);
curl_setopt($ch, CURLOPT_POST, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, "");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
$data = curl_exec($ch);
//print_r($data);
$error = curl_error($ch);
curl_close ($ch);
$destination = "/tmp/flurry_event_log.csv";
$file = fopen($destination, "w");
fputs($file, $data);
fclose($file);
Any help would be greatly appreciated.
Update: Here's the main content of the response for $data:
<div id="main">
<!-- center -->
<div id="centerColumn">
<h1> </h1>
<div class="fontSize" style="margin:50px;">
<h2>Oops, an unexpected error has occurred.</h2>
<br/><br/><br/>
Please <a class="colorMediumBlue bold hover fontSize" href="/">go home</a> and try again.
<br/><br/><br/>
If you receive this same message more than once,
please email <a class="colorMediumBlue bold hover fontSize" href="mailto:[email protected]">[email protected]</a> with details on how you arrived on this error page.
<br/><br/>
We apologize for this inconvenience and we will find a solution to this issue as soon as possible.
<br/><br/>
Sincerely,<br/>
The Flurry Team
</div>
</div>
</div>
Add this line code to your first curl call:
curl_setopt($ch, CURLOPT_COOKIEJAR, "/tmp/cookieFileName");
Then after you stored cookie data, use it on your second curl call with this:
curl_setopt($ch, CURLOPT_COOKIEFILE, "/tmp/cookieFileName");
And be sure cookies stored and you dont have any permission problem on /tmp/cookieFileName path or any path you desire.
I edited your code;
<?php
$post = array(
'loginEmail' => '[email protected]',
'loginPassword' => 'xxxxxxx',
//'rememberMeCheckbox' => 'true',
//'__checkbox_rememberMeCheckbox' => 'true',
'struts.token.name' => 'token',
);
$ch = curl_init('https://dev.flurry.com/secure/login.do');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_COOKIEFILE, null);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
libxml_use_internal_errors(true);
$dom = new DOMDocument('1.0', 'UTF-8');
$dom->loadHTML(curl_exec($ch));
$xpath = new DOMXPath($dom);
$post['token'] = $xpath->query('//input[@name="token"]')->item(0)->getAttribute('value');
curl_setopt($ch, CURLOPT_URL, 'https://dev.flurry.com/secure/loginAction.do');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));
curl_exec($ch);
$log_url = 'https://dev.flurry.com/eventsLogCsv.do?projectID=12345&versionCut=versionsAll&intervalCut=30Days&childProjectId=0&stream=true&direction=1&offset=0';
curl_setopt($ch, CURLOPT_URL, $log_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
$data = curl_exec($ch);
//print_r($data);
$error = curl_error($ch);
curl_close($ch);
echo $data;
?>