【问题标题】:CURL - cookie not enabled/session expiredCURL - cookie 未启用/会话过期
【发布时间】:2011-12-12 01:16:45
【问题描述】:

我正在尝试使用 PHP CURL 登录网站。在不需要 cookie 和会话的网站上一切正常,但它似乎不适用于需要你的网站,所以这是我的代码,我在 here 上找到了这段代码 对此有任何帮助,谢谢
代码

<?php

// 1-获取第一个登录页面http://signin.ebay.com/aw-cgi/eBayISAPI.dll?SignIn

$ebay_user_id = "username"; // Please set your Ebay ID
$ebay_user_password = "password"; // Please set your Ebay Password
$cookie_file_path = "cookie.txt"; // Please set your Cookie File path

$LOGINURL = "__";
$agent = "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.4) Gecko/20030624 Netscape/7.1 (ax)";
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL,$LOGINURL);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
$result = curl_exec ($ch);
//    curl_close ($ch);

// 2- Post Login Data to Page http://signin.ebay.com/aw-cgi/eBayISAPI.dll

$LOGINURL = "url";
$POSTFIELDS = 'postfiends';
$reffer = "url";

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL,$LOGINURL);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_POSTFIELDS,$POSTFIELDS); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_REFERER, $reffer);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file_path);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file_path);
$result = curl_exec ($ch);
//    curl_close ($ch); 
print   $result;    

 ?>

【问题讨论】:

  • 您的 cookie.txt 是否包含您的第一个 cURL 处理后的数据..?
  • 是的,一些数据被写入 cookie.txt 文件
  • 它实际上显示了什么错误..?
  • 没有错误,它只是说您需要启用 cookie 或会话已过期
  • @tushar-chutani 尝试我的回答中的建议。当然有帮助...祝你好运...

标签: php curl


【解决方案1】:

也许您需要更多选择:

// define some HTTP headers 
$headers[] = "Accept: */*";
$headers[] = "Connection: Keep-Alive";
$headers[] = "Content-type: application/x-www-form-urlencoded;charset=UTF-8";

// to GET add
curl_setopt($ch, CURLOPT_HTTPHEADER,  $headers);
curl_setopt($ch, CURLOPT_HEADER,  0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);        

// to POST add
curl_setopt($ch, CURLOPT_HTTPHEADER,  $headers);
curl_setopt($ch, CURLOPT_HEADER,  1);

// check for errors before close
$result = curl_exec($ch);
if ($result === false)
{
    echo curl_error($ch);
}
curl_close($ch);

确保您的 $cookie_file_path 文件是可写的(如果是 Linux)。与CURLOPT_SSL_VERIFYHOSTCURLOPT_SSL_VERIFYPEER 一起玩。

【讨论】:

  • NomikOS 是正确的;这个答案在网上很难找到。谢谢
【解决方案2】:

http://signin.ebay.com/aw-cgi/eBayISAPI.dll 不仅需要用户名和密码作为发布数据,还需要其他参数,也许你没有考虑到这一点。使用 firebug 上的 Net 选项卡查看传递的参数,并尝试复制它。

【讨论】:

  • 嘿,我已经确定我已经输入了我使用 httpliveheaders 的所有帖子参数
  • @tushar-chutani 我使用 curl 做了很多工作,cookie 问题很常见。 @ 987654322@,请解释传递所有帖子字段将如何解决您在问题中提到的一个完全不同的问题:@ 987654323@。 THX.-
猜你喜欢
  • 2015-03-31
  • 2012-08-23
  • 2013-03-06
  • 1970-01-01
  • 1970-01-01
  • 2020-10-18
  • 1970-01-01
  • 2013-06-13
  • 2011-01-24
相关资源
最近更新 更多