【问题标题】:PHP cURL POST DATA [Redirect 302 problem]PHP cURL POST DATA [重定向 302 问题]
【发布时间】:2011-11-02 15:15:02
【问题描述】:

我使用以下代码登录网站

$postData = array("login" => "Prijava", "loginEmail" => "****@****.***", "password" => "*****t", "signonForwardAction" => "/press/cm/si.press.viasat.tv?cc=si&lc=si");

$URL = "http://si.press.viasat.tv/press/cm/1.167?cc=si&lc=si";

$connection = curl_init();
curl_setopt($connection, CURLOPT_URL, $URL);
curl_setopt($connection, CURLOPT_POST, 1);

curl_setopt($connection, CURLOPT_POSTFIELDS, $postData);

curl_setopt($connection, CURLOPT_FOLLOWLOCATION, true);

curl_setopt($connection, CURLOPT_REFERER, "http://si.press.viasat.tv");
curl_setopt($connection, CURLOPT_AUTOREFERER, true);
curl_setopt($connection, CURLOPT_HEADER, true);
curl_setopt($connection, CURLOPT_POSTREDIR, 2);

curl_setopt($connection,CURLOPT_COOKIEJAR, "C:\@DEV\TextALG\cookie.txt");
curl_setopt($connection,CURLOPT_COOKIEFILE, "C:\@DEV\TextALG\cookie.txt");

curl_exec($connection);



curl_close($connection);

问题是(在 Firebug 中发现)在登录后站点重定向到 URL(响应:302)。结果我又得到了登录屏幕。

我得到这样的 cookie:

# Netscape HTTP Cookie File
# http://curl.haxx.se/rfc/cookie_spec.html
# This file was generated by libcurl! Edit at your own risk.

si.press.viasat.tv  FALSE   /press  FALSE   0   JSESSIONID  00FC3DCA4CFD806CDEBE2CAA7E999463

有什么想法吗?

【问题讨论】:

  • 它适用于没有 302 响应的网站..
  • 你试过curl_setopt($connection, CURLOPT_HEADER, false);

标签: php curl http-status-code-302


【解决方案1】:

尝试添加以下选项

CURLOPT_FOLLOWLOCATION => TRUE,     // follow redirects         
CURLOPT_MAXREDIRS      => 10,       // stop after 10 redirects 

Reference

【讨论】:

    【解决方案2】:

    我尝试了不同的逻辑(浏览逻辑,现在可以了!)

    $ch = curl_init();
    $randnum = rand(1,5000);
    curl_setopt($ch, CURLOPT_COOKIEJAR, "/tmp/cookiejar-$randnum");
    curl_setopt($ch, CURLOPT_COOKIEFILE, "/tmp/cookiejar-$randnum");
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1");
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 0);
    
    
    curl_setopt($ch, CURLOPT_URL,$URL);
    $page = curl_exec($ch);
    
    preg_match("/action=\"(.*)\"/", $page, $action);
    preg_match("/signonForwardAction\" type=\"hidden\" value=\"(.*)\"/", $page, $signonFA);
    
    $action = $action[1];
    $signonFA = $signonFA[1];
    $postData['signonForwardAction'] = $signonFA;
    
    
    curl_setopt($ch, CURLOPT_URL,$URL.$action);
    curl_setopt($ch, CURLOPT_REFERER, $URL);
    curl_setopt($ch, CURLOPT_HTTPHEADER, Array("Content-Type: application/x-www-form-urlencoded"));
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($postData));
    $page = curl_exec($ch);
    

    基本思路是访问站点,设置 cookie,然后将数据(必须是字符串而不是数组!)发布到站点(通过 $action 获取),然后继续爬取站点!

    【讨论】:

      猜你喜欢
      • 2021-12-22
      • 2019-06-21
      • 2020-07-05
      • 2011-05-30
      • 2014-01-24
      • 1970-01-01
      • 1970-01-01
      • 2012-01-18
      • 2021-01-05
      相关资源
      最近更新 更多