繁体   English   中英

使用不带tempnam的cURL cookie(PHP)

[英]Using cURL cookies without tempnam (PHP)

感谢您查看我的问题! 我正在尝试从通过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);
?>

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

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

让我知道结果!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM