W
W
WQP2015-11-02 16:00:22
PHP
WQP, 2015-11-02 16:00:22

How to use curl cookies?

Hello, there is a function with which I log in to the site and receive cookies. Everything is going well, but how do I continue to use these cookies? I've tried dozens of options, nothing helps

function login($url, $param){
  $user_agent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322)";
  $header [] = "Accept: text/html, application/xml;q=0.9, application/xhtml+xml, image/png, image/jpeg, image/gif, image/x-xbitmap, */*;q=0.1";
  $header [] = "Accept-Language: ru-RU,ru;q=0.9,en;q=0.8";
  $header [] = "Accept-Charset: Windows-1251, utf-8, *;q=0.1";
  $header [] = "Accept-Encoding: deflate, identity, *;q=0";

  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL,$url);
  curl_setopt($ch, CURLOPT_FAILONERROR, 1);
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
  curl_setopt($ch, CURLOPT_TIMEOUT, 40);
  curl_setopt($ch, CURLOPT_REFERER, $url);
  curl_setopt($ch, CURLOPT_HTTPHEADER, $header );
  curl_setopt($ch, CURLOPT_COOKIESESSION, true);

  curl_setopt($ch, CURLOPT_COOKIEJAR, $_SERVER['DOCUMENT_ROOT'] . '/cookie.txt');

  curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);
  curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
  curl_setopt($ch, CURLOPT_POST, 1);
  curl_setopt($ch, CURLOPT_HEADER, 1);
  curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($param));
  $result = curl_exec($ch);
  curl_close($ch);

  return $result;
}

Here is a function with which I try to get the page code with cookies
function getFile($url) {
  $user_agent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322)";
  $header [] = "Accept: text/html, application/xml;q=0.9, application/xhtml+xml, image/png, image/jpeg, image/gif, image/x-xbitmap, */*;q=0.1";
  $header [] = "Accept-Language: ru-RU,ru;q=0.9,en;q=0.8";
  $header [] = "Accept-Charset: Windows-1251, utf-8, *;q=0.1";
  $header [] = "Accept-Encoding: deflate, identity, *;q=0";

  $ch = curl_init(); 
  curl_setopt($ch, CURLOPT_URL, $url); 
  curl_setopt($ch, CURLOPT_TIMEOUT, 80);
  curl_setopt($ch, CURLOPT_REFERER, $url);
  curl_setopt($ch, CURLOPT_HTTPHEADER, $header );
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
  curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
  
  curl_setopt($ch, CURLOPT_COOKIEFILE, $_SERVER['DOCUMENT_ROOT'] . '/cookie.txt');


  $result = curl_exec($ch);
  curl_close($ch);

  return $result;
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
riot26, 2015-11-02
@riot26

curl_setopt($ch, CURLOPT_COOKIEFILE, $_SERVER['DOCUMENT_ROOT'] . '/cookie.txt');

V
Vitaly Artemyev, 2015-11-04
@Vitaly48

Try
curl_setopt($ch, CURLOPT_COOKIE, "cookie1=1;cookie2=2");

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question