2016-11-18 7 views
2

私はカール経由でサイトにログインできましたが、今はダウンロードするファイルにリダイレクトしたいと思います。このファイルをダウンロードするにはログインクッキーが必要です。以前に保存したCookieをリダイレクトして使用するにはどうすればよいですか?ログインが成功した後、私は下のURLを設定しようとしましたが、うまくいきませんでした。サイトにログインし、ファイルのダウンロードにリダイレクト

<?php 
$username = "user"; 
$password = "pass"; 
$url = "https://www.example.com/login"; 
$urldl = "https://www.example.com/get-download/abc.xyz"; 
$cookie = "cookie.txt"; 

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie); 
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
$response = curl_exec($ch); 

if (curl_errno($ch)) die(curl_error($ch)); 

$doc = new DOMDocument(); 
libxml_use_internal_errors(true); 
$doc->loadHTML($response); 
$token = $doc->getElementById("csrf_token")->attributes->getNamedItem("value")->value; 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false); 
curl_setopt($ch, CURLOPT_POST, true); 
$params = array(
    'username' => $username, 
    'password' => $password, 
    'csrf_token' => $token 
); 
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params)); 
curl_exec($ch); 

if (curl_errno($ch)) print curl_error($ch); 


curl_setopt($ch, CURLOPT_URL, $urldl); 
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_exec($ch); 


curl_close($ch); 

答えて

0

あなたはCURLOPT_RETURNTRANSFERが有効に持っている、しかし、あなたが実際にあなたが値、set the header appropriatelyにそれを割り当てる必要があり、その後、応答を印刷curl_exec

// I want the result returned from exec 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
// but I'm not gonna do anything with it... 
curl_exec($ch); 

から値を取得していません。

$result = curl_exec($ch); 
header('Content-Type: application/octet-stream'); 
header('Content-Disposition: attachment; filename="foo.bar"'); 
echo $result; 
関連する問題