我正在尝试通过php& s进行身份验证并下载Flurry事件日志。卷曲。丝绸的post让我成为了一部分,但我仍然无法下载。我相信我已经过身份验证,但下载似乎返回错误页面而不是文件。这是我正在尝试的最新代码:
if let linkString = posts.objectAtIndex(indexPath.row).valueForKey("link") as? String, url = NSURL(string: linkString) {
...do rest of your code here
}
非常感谢任何帮助。
更新:以下是$ data响应的主要内容:
$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);
答案 0 :(得分:0)
将此行代码添加到您的第一个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;
?>