使用不带tempnam的cURL cookie(PHP)

时间:2014-07-09 22:51:07

标签: php cookies curl fopen

感谢您查看我的问题!我试图通过cURL从服务器上加载一个已删除的cookie。

<?php
$udid = $_GET['string'];

    $ourFileName = md5($string . "salt here");

    $ourFileHandle = fopen($ourFileName, 'w') or die("can't open file");
fclose($ourFileHandle);

    $ckfile = "/public_html/delete/" . $ourFileName; //Note: This code is in the directory delete.


    $ch = curl_init ("http://example.com?string=" . $string);
    curl_setopt ($ch, CURLOPT_COOKIEJAR, $ckfile);
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Linux; Android 4.1.1; Nexus 7 Build/JRO03D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Safari/535.19');
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
    $output = curl_exec ($ch);
?>

每当我去查看它创建的文件时,它都是空的。感谢任何帮助,谢谢!

1 个答案:

答案 0 :(得分:2)

您需要确保运行脚本的进程具有对cookie文件的写访问权,但我相信您还需要设置cookie文件。下面是我使用的脚本,它对我来说很好。

$longUrl 'http://www.example.com';
$useragent="Fake Mozilla 5.0 ";
$cookie_jar = tempnam ("/tmp", "example"); // This can be your statically assigned cookie file as well. 
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $longUrl);
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
curl_setopt($ch, CURLOPT_HEADER,0); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_jar);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_jar); //You are missing this one!!!
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$response = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);

希望有所帮助!

嗨格兰特 - 我读了你的评论 - 为了理智,请尝试以下代码,看看你的cookie文件是否确实写了内容。

$teststring = "This is a test\n";   
$handle = fopen($ckfile, "w");
fwrite($handle,$teststring);
fclose($handle);

让我知道结果!