【问题标题】:PHP cURL works with session id, but it doesn't work with cookiejarPHP cURL 适用于会话 ID,但不适用于 cookiejar
【发布时间】:2017-03-01 23:42:47
【问题描述】:

我一直在尝试使用 cURL 从该网站获取一些页面数据。页面落后于授权,使用 cookie,没有 SSL。
我检查了很多设置 php cURL 脚本的手册和示例,但似乎都没有工作。

每次我运行我的脚本时,cookie 文件都会更新,但结果却是空字符串。如果我将 CURLOPT_FOLLOWLOCATION 设置为 1,我会得到登录页面。所以我假设,原始脚本返回重定向到登录页面。
我尝试弄乱 CURLOPT_USERAGENT、CURLOPT_REFERER,但没有帮助。

另外,如果我手动设置 CURLOPT_COOKIE、PHPSESSID(来自使用浏览器和人工输入的真实登录会话),它工作正常。

所以,这是我的代码:

<?php
set_time_limit(10);
define('USERNAME', 'username');
define('PASSWORD', 'password');
define('USER_AGENT', 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.2309.372 Safari/537.36');
define('COOKIE_FILE', 'cookie.txt');
define('LOGIN_FORM_URL', 'http://website/auth');
define('LOGIN_ACTION_URL', 'http://website/distribution/index');
$postValues = array(
    'login_msisdn' => USERNAME,
    'password' => PASSWORD
);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, LOGIN_FORM_URL);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($postValues));
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_COOKIEJAR, realpath(COOKIE_FILE));
//curl_setopt($curl, CURLOPT_COOKIE, "PHPSESSID=relkdrgg94gfdgfg834g");
curl_setopt($curl, CURLOPT_USERAGENT, USER_AGENT);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_REFERER, LOGIN_FORM_URL);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 0);
curl_exec($curl);
if(curl_errno($curl)){
    throw new Exception(curl_error($curl));
}
curl_close($curl);

$curl = curl_init()
curl_setopt($curl, CURLOPT_URL, LOGIN_ACTION_URL);
curl_setopt($curl, CURLOPT_COOKIEFILE, realpath(COOKIE_FILE));
curl_setopt($curl, CURLOPT_USERAGENT, USER_AGENT);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$html = curl_exec($curl);
echo $html;

【问题讨论】:

    标签: php session curl cookies


    【解决方案1】:

    您只是在第一次 curl 调用中保存 cookie(使用 CURLOPT_COOKIEJAR),而不是在第二次 curl 调用期间加载。这就是为什么在第二次通话期间不使用 cookie。在您的第二个 curl 调用中使用以下内容。

    curl_setopt($curl, CURLOPT_COOKIEFILE, realpath(COOKIE_FILE));
    

    其次,在发出第二个 curl 请求之前,您必须关闭 curl 手并再次对其进行初始化。 curl 选项 CURLOPT_COOKIEJAR 有助于将 cookie 保存在文件中,但它会在 curl 句柄关闭时进行。

    curl_close($curl);
    $curl = curl_init();
    // here goes the second one
    curl_setopt($curl, CURLOPT_URL, LOGIN_ACTION_URL);
    

    另外,根据评论中的建议(我错过了),启用选项 CURLOPT_RETURNTRANSFER 以便 curl 返回输出。

    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    

    另外请注意,请使用 cookie 文件的完整路径。否则,当您从浏览器运行时,它可能无法正常工作(还要确保您对 cookie 文件具有写入权限)。

    define('COOKIE_FILE', '/some/directory/cookie.txt');
    

    【讨论】:

    • 感谢您的建议,我修复了我的代码,但它现在返回 1,而不是页面。如果添加 curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);到第二次调用,它返回认证页面。
    • 澄清一下,这并没有改变结果。
    • CURLOPT_RETURNTRANSFER 未在第二次调用中设置
    • 添加并修复了我的帖子,但它仍然返回空白页。
    • @Maksim.T 请使用 cookie 文件的完整路径。我也更新了我的答案。 define('COOKIE_FILE', '/some/directory/cookie.txt');
    【解决方案2】:

    为了保存和使用带有 CURL 的 cookie 文件,我使用以下代码:

    $ckfile = tempnam('/tmp', 'CURLCOOKIE');
    curl_setopt($curl, CURLOPT_COOKIEJAR, $ckfile);
    curl_setopt($curl, CURLOPT_COOKIEFILE, $ckfile);
    

    【讨论】:

      【解决方案3】:
      /*
          1) Make first request in main page and after do the login
          2) I added some headers
          3) Check if are all parameters in post ( ex: "&login=Submit" )
          4) If is basic authorization use curl_setopt($curl, CURLOPT_USERPWD, "$username:$password");
          5) Debug header/ errors ...
      */
      
      $url1 = "http://website/";
      $url2 = "http://website/auth";
      $url3 = "http://website/distribution/index";
      
      $user = "username";
      $pass = "password";
      
      $post = "user=".$user."&pass=".$pass;
      
      
      get_url($url1,'',$url1);
      
      $login = get_url($url2,$post,$url1);
      
      $data = get_url($url3,'',$url1);
      
      print_r($data);
      
      
      
      function get_url($url,$post,$refer) {
          $ssl = substr(strtolower($url),0,8)=='https://' ? true : false;
          $cookie = getcwd().DIRECTORY_SEPARATOR.'cookie.txt';
          $header[0] = "text/xml,application/xml,application/xhtml+xml,";
          $header[0] .= "text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
          $header[] = "Cache-Control: max-age=0";
          $header[] = "Connection: keep-alive";
          $header[] = "Keep-Alive: 300";
          $header[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7";
          $header[] = "Accept-Language: en-us,en;q=0.5";
          $header[] = "Pragma: ";
          $agent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.95 Safari/537.36";
          $refer = !empty($refer) ? $refer : "http://www.google.com/";
          $curl = curl_init();
          curl_setopt($curl, CURLOPT_URL, $url);
          curl_setopt($curl, CURLOPT_USERAGENT, $agent);
          if( !empty($post) ) {
              curl_setopt($curl, CURLOPT_POST, 1);
              curl_setopt($curl, CURLOPT_POSTFIELDS, $post);
          }
          if( $ssl ) {
              curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
          }
          curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
          curl_setopt($curl, CURLOPT_REFERER, $refer);
          curl_setopt($curl, CURLOPT_COOKIEJAR, $cookie);
          curl_setopt($curl, CURLOPT_COOKIEFILE, $cookie);
          curl_setopt($curl, CURLOPT_AUTOREFERER, true);
          curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
          curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
          curl_setopt($curl, CURLOPT_TIMEOUT,60);
      
          $html = curl_exec($curl);
          $info = curl_getinfo($curl);
          $error = '';
          if( $html === false ) {
              $error = 'Curl error: ' . curl_error($curl);
          }               
          curl_close($curl);
          $arr = array();
          $arr['html'] = $html;
          $arr['info'] = $info;
          $arr['error'] = $error;
          return $arr;    
      
      }   
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-01-21
        • 2011-09-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-09-19
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多