簡體   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