dongxun7962 2010-04-01 11:48
浏览 163

我怎样才能在curl中设置cookie

i am fetching somesite page..

but it display nothing and url address change.

example i have typed

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

in curl page my coding is=

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

but it canege url like this..

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

object not found.

how can solve these problem help me

  • 写回答

2条回答 默认 最新

  • dongshengyin0147 2010-04-01 11:58
    关注

    It looks like you're both trying to save cookies to cookies.txt, and read them from there. What you would normally do is that the first url you visit, you have curl save the cookies to a file. Then, for subseqent requests, you supply that file.

    I'm not sure of the php aspects, but from the curl aspects it looks like you're trying to read a cookie file that doesn't exist yet.

    edit: oh, and if you're only doing one request, you shouldn't even need cookies.

    评论

报告相同问题?

悬赏问题

  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计