【问题标题】:CURL get external page cookies and use them in my pageCURL 获取外部页面 cookie 并在我的页面中使用它们
【发布时间】:2012-05-09 12:34:55
【问题描述】:

我正在尝试从外部页面获取所有 cookie 并将它们设置在我的页面中,我有这个脚本:

 $url = "http://www.booking.com/hotel/cz/red-blue-design-prague.html?checkin=2012-07-07&interval=1&selected_currency=USD";
 $ckfile = tempnam ("tmp", "CURLCOOKIE");

 $options = array(
    CURLOPT_RETURNTRANSFER => TRUE,     // return web page
    CURLOPT_HEADER         => TRUE,     // do not return headers
    CURLOPT_HEADER         => TRUE,     // do not return headers
    CURLOPT_FOLLOWLOCATION => TRUE,     // follow redirects
    CURLOPT_USERAGENT      => "googlebot", // who am i
    CURLOPT_AUTOREFERER    => TRUE,     // set referer on redirect
    CURLOPT_CONNECTTIMEOUT => 120,        // timeout on connect
    CURLOPT_TIMEOUT        => 120,        // timeout on response
    CURLOPT_MAXREDIRS      => 3,        // stop after 10 redirects
    CURLOPT_ENCODING       => 'UTF-8',
    CURLOPT_COOKIEJAR      => $ckfile,
    CURLOPT_COOKIEFILE     => $ckfile
  );
  $ch = curl_init();
  curl_setopt_array( $ch, $options );
  curl_setopt($ch, CURLOPT_URL, $url);
  $a = curl_exec($ch);

但是当我检查$ckfile时它没有收到cookie,有人可以帮忙吗,谢谢

【问题讨论】:

  • 查看输入、输出、错误会很有帮助...
  • @LeonardChallis 没有错误,工作正常,但没有获取 cookie
  • cookie 文件是否包含任何内容?
  • 首先检查请求是否有效?我的意思是打印$a 看看是否符合预期>
  • @Shubham 我得到了页面但没有 cookie

标签: php cookies curl


【解决方案1】:

我找到了解决方案,谢谢:

function myCurl($url)
{
    $cookie_file_path = tempnam ("tmp", "CURLCOOKIE");

    $curl = curl_init();
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($curl, CURLOPT_HEADER, 0);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($curl, CURLOPT_AUTOREFERER, 0);
    curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 60);
    curl_setopt($curl, CURLOPT_TIMEOUT, 60);
    curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
    curl_setopt($curl, CURLOPT_COOKIEFILE, $cookie_file_path);
    curl_setopt($curl, CURLOPT_COOKIEJAR, $cookie_file_path);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($curl, CURLOPT_COOKIESESSION, TRUE);
    curl_setopt($curl, CURLOPT_COOKIE, session_name() . '=' . session_id());
    curl_setopt($curl, CURLOPT_URL, $url);

    $a = curl_exec ($curl);

    return $a;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-06
    • 2014-04-25
    • 2013-07-08
    • 2011-10-23
    相关资源
    最近更新 更多