首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >会话不维护curl php远程登录

会话不维护curl php远程登录
EN

Stack Overflow用户
提问于 2014-08-20 05:52:47
回答 1查看 789关注 0票数 1

我正在尝试使用curl php远程登录。我可以登录到网站,但会话不维护在卷曲响应。我试过session_write_close()作为how to maintain session in cURL in php?,但这是行不通的。我也用曲奇,但什么都没有。

以下是我尝试过的:

代码语言:javascript
复制
  $ch = curl_init();

  $params['ror_csrf_token'] = $hiddenValue;
  $params['n'] = '';
  $params['email'] = 'xxx.xxx@evontech.com';
  $params['password'] = 'xx';
  $params['remember_me'] = 'on';

  $form_action_url = 'http://www.xxxxxxxx.com/go/login';
  $postData = '';
  foreach($params as $k => $v)
  {
     $postData .= $k.'='.$v.'&';
  }
  $postData = rtrim($postData, '&');
  print_r($postData);
  $theaders[] = "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
  $theaders[] = "Content-Type: application/x-www-form-urlencoded";
  $theaders[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7";
  $theaders[] = "Accept-Encoding: gzip,deflate,sdch";
  $theaders[] = "Accept-Language: en-US,en;q=0.8";
  $theaders[] = "Cache-Control: max-age=0";
  //$theaders[] = "Connection: keep-alive";
  //$theaders[] = "Content-Length: 119";
  curl_setopt($ch, CURLOPT_URL,$form_action_url);
  //curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 100);
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);//follow redirection
  curl_setopt($ch, CURLOPT_AUTOREFERER, true);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookies.txt'); // set cookie file to given file
  //curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookies.txt'); // set same file as cookie jar
  //curl_setopt($ch, CURLOPT_COOKIE, 'cookies.txt');
  curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36');
  curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
  curl_setopt($ch, CURLOPT_POST, true);
  curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);   
  curl_setopt($ch, CURLOPT_HTTPHEADER, $theaders);


  curl_setopt($ch, CURLOPT_HEADER, false);
  curl_setopt($ch, CURLOPT_COOKIESESSION, true);
  //curl_setopt($ch, CURLOPT_NOBODY, false);
  //curl_setopt($ch, CURLOPT_REFERER, "http://www.ripoffreport.com/go/login");      
  //curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  //curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);

  echo $content = curl_exec($ch);
  $headers = curl_getinfo($ch); 
  $errors  = curl_error($ch);

  echo "<pre>";
  print_r($headers);
  curl_close ($ch);

注意:自从10天以来,我一直在谷歌上搜索很多东西,但似乎没有什么对我有用。

EN

回答 1

Stack Overflow用户

发布于 2014-08-20 06:03:44

您必须将CURLOPT_COOKIEJARCURLOPT_COOKIEFILE选项设置为相同的绝对路径值('cookies.txt'是一个相对路径)。这是必要的,以便在脚本将具有的重定向系列中启用cookie自动处理(因此也是会话维护)。

此外,您不应该将CURLOPT_CUSTOMREQUESTCURLOPT_POST选项放在一起,只设置其中一个选项(在您的情况下是CURLOPT_POST)。

因此,脚本应该有以下几行:

代码语言:javascript
复制
curl_setopt($ch, CURLOPT_COOKIEFILE, dirname(__FILE__).'/cookies.txt');
curl_setopt($ch, CURLOPT_COOKIEJAR, dirname(__FILE__).'/cookies.txt');

顺便说一句:session_write_close()不影响CURL请求。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25397585

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档