Cookie を使用して PHP でダウンロードを強制するにはどうすればよいですか? file_get_contents と cURL を使用してみましたが、file_get_contents で Cookie を使用できるかどうかわかりません。このコードを使用しますが、Cookie がありません:
<?php
$extensiones = array("jpg", "jpeg", "png", "gif");
$f = $_POST["direccion"];
$nombre_archivo = $_POST["nombre_archivo"];
if(strpos($f,"/") < 6){
die("No puedes navegar por otros directorios");
}
$ftmp = explode(".",$f);
$fExt = strtolower($ftmp[count($ftmp)-1]);
if(!in_array($fExt,$extensiones)){
die("<b>¡ERROR!</b> No es posible descargar archivos con la extensión $fExt");
}
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"$nombre_archivo\"\n");
$fp=fopen("$f", "r");
fpassthru($fp);
?>
これはcURLとCookieを使用したものですが、ファイルはサーバーに保存されているので、それはしたくありません。他のコード (file_get_contents) のように、このユーザーにダウンロードを強制したいのですが、Cookie を使用します :(
<?php
// Data for the login
$raw_post = "&". http_build_query(array("user" => "xxxxxxxxxxxxxxxxx"));
$raw_post .= "&". http_build_query(array("password" => "xxxxxxxxxxxxxxxxx"));
// Login
$ch = curl_init('https://www.instagram.com/accounts/login/');
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS , $raw_post );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_COOKIEJAR, '/tmp/cookie.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, '/tmp/cookie.txt');
$buffer = curl_exec($ch);
// Download the file
$fp = fopen("hi.jpg", 'w+');
$ch = curl_init("http://www.gravatar.com/avatar/10773ae6687b55736e171c038b4228d2");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_COOKIEJAR, '/tmp/cookie.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, '/tmp/cookie.txt');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_exec($ch);
?>