A
A
Alex Alex2015-11-26 21:57:39
PHP
Alex Alex, 2015-11-26 21:57:39

How to pass cUrl php cookies?

I send a request to the function

function send($url,$post='',$header='',$xhr='') {
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  curl_setopt($ch, CURLOPT_TIMEOUT, 60);
  curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookies/cookie_'.$_SESSION['USER_ID'].'.txt');
  curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookies/cookie_'.$_SESSION['USER_ID'].'.txt');
  curl_setopt($ch, CURLOPT_REFERER, $url);
  curl_setopt($ch, CURLOPT_AUTOREFERER, true);
  curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 YaBrowser/15.10.2454.3658 Safari/537.36');
  curl_setopt($ch, CURLOPT_HEADER, true);
  $respons = curl_exec($ch);
  curl_close($ch);
//
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  curl_setopt($ch, CURLOPT_TIMEOUT, 60);
  curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookies/cookie_'.$_SESSION['USER_ID'].'.txt');
  curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookies/cookie_'.$_SESSION['USER_ID'].'.txt');
  curl_setopt($ch, CURLOPT_REFERER, $url);
  curl_setopt($ch, CURLOPT_AUTOREFERER, true);
  curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.157 YaBrowser/15.9.2403.3043 Safari/537.36');
  if ($post) {
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
  }
  if ($header) {
  curl_setopt($ch, CURLOPT_HEADER, true);
  }
  if ($xhr) {
    preg_match('/csrftoken=(.*?);/i', $respons, $csrf);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('X-CSRFToken:' . trim($csrf[1]), 'X-Instagram-AJAX:1', 'X-Requested-With:XMLHttpRequest'));
  }
  $file = curl_exec($ch);
  curl_close($ch);
  return $file;
}

$respons=send('https://instagram.com/accounts/login/ajax/','username='.$_POST['email'].'&password='.$_POST['pass'], 1,1);

I get cookies from the response:
preg_match_all("/Set-Cookie: (.*?)=(.*?);/i",$a,$res);
foreach ($res[1] as $key => $value) {$cookie.= $value.'='.$res[2][$key].'; ';};

I get the format:
csrftoken=408aa7cc911b2fb362f1f3a096bf2dda; s_network=; ds_user_id=1450872332;

Trying to send the following request with curl_setopt($ch, CURLOPT_COOKIE,$cookie);
where $cookie is csrftoken=408aa7cc911b2fb362f1f3a096bf2dda; s_network=; ds_user_id=1450872332;
But they don't transfer.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
maximw, 2015-11-27
@maximw

You already have a file where your cookies will be stored between requests.
It's enough. The main thing is to close the descriptor curl_close($ch); between requests.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question