【问题标题】:how can i set cookie in curl我如何在 curl 中设置 cookie
【发布时间】:2010-04-01 11:48:40
【问题描述】:

我正在获取某个站点页面..

但它什么也不显示 和url地址变化。

我输入的示例

http://localhost/sushant/EXAMPLE_ROUGH/curl.php

在 curl 页面中我的编码是=

$fp = fopen("cookie.txt", "w");

fclose($fp);

$agent= 'Mozilla/5.0 (Windows; U; Windows NT 5.1; pl; rv:1.9) Gecko/2008052906 Firefox/3.0';

$ch = curl_init();

curl_setopt($ch, CURLOPT_USERAGENT, $agent);
 // 2. set the options, including the url  

curl_setopt($ch, CURLOPT_URL, "http://www.fnacspectacles.com/place-spectacle/manifestation/Grand-spectacle-LE-ROI-LION-ROI4.htm");  

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  

curl_setopt($ch, CURLOPT_HEADER, 0);  

curl_setopt ($ch, CURLOPT_COOKIEJAR, 'cookie.txt');

curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt");
// 3. execute and fetch the resulting HTML output  

if(curl_exec($ch) === false)
{
  echo 'Curl error: ' . curl_error($ch);
}
else
  echo $output = curl_exec($ch);  

 // 4. free up the curl handle  
curl_close($ch); 

但它像这样访问网址..

http://localhost/aide.do?sht=_aide_cookies_

找不到对象。

如何解决这些问题对我有帮助

【问题讨论】:

  • 我不明白“cookie.txt”的作用。显示错误:error_reporting(E_ALL);
  • 您的问题到底是什么?什么不起作用?
  • 不太清楚为什么 VolkerK 恢复了我的编辑...

标签: php curl


【解决方案1】:

看起来你们都在尝试将 cookie 保存到 cookies.txt,并从那里读取它们。您通常会做的是,您访问的第一个 url,您让 curl 将 cookie 保存到文件中。然后,对于后续请求,您提供该文件。

我不确定 php 方面,但从 curl 方面来看,您似乎正在尝试读取尚不存在的 cookie 文件。

编辑:哦,如果你只做一个请求,你甚至不应该需要 cookie。

【讨论】:

    【解决方案2】:

    似乎输出中有 javascript,这导致了重定向。

    所以为了测试目的,而不是使用:

    echo $output = curl_exec($ch);
    

    用途:

    $output = curl_exec($ch);
    echo strip_tags($output);
    

    更新:

    下面的代码会将内容放入contents.htm .. 解析所需的所有内容都应该在其中和输出变量中。

    if(curl_exec($ch) === false)
    {
      echo 'Curl error: ' . curl_error($ch);
    }
    else{
      $output = curl_exec($ch);  
      $fp2 = fopen("content.htm" , "w");
      fwrite($fp2 , $output);
    fclose($fp2);
    }
    

    【讨论】:

      猜你喜欢
      • 2012-06-22
      • 2015-03-13
      • 2013-02-15
      • 2014-10-27
      • 1970-01-01
      • 2013-06-12
      • 1970-01-01
      • 2015-03-22
      • 2011-08-25
      相关资源
      最近更新 更多