通过 cURL 进行 Flurry 身份验证和 CSV 下载

Flurry Authentication & CSV downloads via cURL

我正在尝试通过 php 和 cURL 验证和下载 Flurry 事件日志。 silkfire 的 帮助我完成了部分工作,但我仍然无法下载。我相信我已经通过身份验证,但下载似乎 return 错误页面而不是文件。这是我正在尝试的最新代码:

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

如有任何帮助,我们将不胜感激。

更新:$data 回复的主要内容如下:

    <div id="main">
    <!-- center -->
    <div id="centerColumn">
      <h1>&nbsp;</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:support@flurry.com">support@flurry.com</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>

将此行代码添加到您的第一个 curl 调用中:

curl_setopt($ch, CURLOPT_COOKIEJAR, "/tmp/cookieFileName");

然后在您存储 cookie 数据后,在您的第二次 curl 调用中使用它:

curl_setopt($ch, CURLOPT_COOKIEFILE, "/tmp/cookieFileName");

并确保已存储 cookie,并且您在 /tmp/cookieFileName 路径或您想要的任何路径上没有任何权限问题。

我编辑了你的代码;

<?php

$post = array(
    'loginEmail' => 'mertizci@xxxxx.com',
    '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;
?>