使用cURL cookie没有tempnam (PHP)


Using cURL cookies without tempnam (PHP)

感谢观看我的问题!我正试图通过cURL从一个抓取的站点加载到服务器上。

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

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

您需要确保运行脚本的进程对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);

希望有帮助!

你好Grant -我读了你的评论-为了理智起见,请尝试下面的代码,看看你的cookie文件是否确实得到内容写入。

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

让我知道结果!