首页 > 解决方案 > PHP Curl cookie 不同的页面

问题描述

我有一个用 PHP 编写的 CURL 机器人,它登录到一个站点并从另一个站点的页面下载数据,该页面可以从第一个站点访问。这是我的代码:

登录

    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6");
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 1800);
    curl_setopt($ch, CURLOPT_TIMEOUT, 3600);
    curl_setopt($ch, CURLOPT_URL, "https://www.aaaaa.bbbb.com/");
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postFields));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_COOKIESESSION, true);
    curl_setopt($ch, CURLOPT_COOKIEJAR, $strCookieRandName);
    curl_setopt($ch, CURLOPT_COOKIEFILE, $strCookieRandName);

登录后

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6");
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 1800);
    curl_setopt($ch, CURLOPT_TIMEOUT, 3600);
    curl_setopt($ch, CURLOPT_URL, "https://www1.aaaaa.bbbb.com/");
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postFields));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_COOKIEJAR, $strCookieRandName);
    curl_setopt($ch, CURLOPT_COOKIEFILE, $strCookieRandName);

会话 cookie 由https://www.aaaaa.bbbb.com/创建,但第二页是https://www1.aaaaa.bbbb.com/

在浏览器中完美运行,通过 curl 第二页告诉我没有登录。你有什么想法吗?Tks

标签: phpcurlcookies

解决方案


推荐阅读