T
T
Talgat Baltasov2015-06-22 12:09:18
Yii
Talgat Baltasov, 2015-06-22 12:09:18

How to automatically like photos on Instagram using curl?

I am making a robot for Instagram, which logs in and likes, subscribes, unsubscribes. Authorization was done, cookies were saved, where there is a sessionid, csrftoken, mid, target_sig, target, ds_user_id. When I run the like function, all this disappears and only csrftoken and mid remain.
here is the like code.
$chl = curl_init();
curl_setopt($chl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.81 Safari/537.36");
curl_setopt($chl, CURLOPT_HEADER, true);
curl_setopt($chl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($chl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($chl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($chl, CURLOPT_ENCODING, "gzip, deflate");
curl_setopt($chl, CURLOPT_COOKIEJAR, Yii::getAlias('@webroot').'/cookies/'.$login.'.txt');
curl_setopt($chl, CURLOPT_URL, " https://instagram.com/web/likes/$id/like/ ");
curl_setopt($chl, CURLOPT_POST, 1);
//curl_setopt($ch, CURLOPT_POSTFIELDS, "X-Instagram-AJAX=1&X-Requested-With=XMLHttpRequest&X-CSRFToken=".$cookie);
curl_setopt($chl, CURLOPT_REFERER, $data->link."?taken-by=".$data->user->username);
curl_setopt( $chl, CURLOPT_HTTPHEADER,
'X-CSRFToken: '.$cookie,
'X-Instagram-AJAX: 1',
'X-Requested-With: XMLHttpRequest',
));
$response = curl_exec($chl);

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Eugene, 2015-07-21
@beatleboy

Faced a similar problem, first of all, disable CURLOPT_FOLLOWLOCATION, instagram makes redirects through Location, curl executes them but does not set cookies. I did like this:

CURLOPT_FOLLOWLOCATION, 0
.....
ist($headers, $response) = explode("\r\n\r\n", $this->last_response, 2);
if(strlen($response) < 5){
      $response = NULL;
}
$header_info['location'] = NULL;
$headers = explode("\n", $headers);
foreach($headers as $header) {
  if (stripos($header, 'Location:') !== false) 
     $header_info['location'] = trim(preg_replace('/Location: /', '', $header) );
}
              
return array('response' => $response, 'header' => $header_info);

This is a piece of code from the Curl library for Codeigniter 3
As a result, I have a Location, I use it to make a new curl and if there is a location again, then I repeat the steps (the usual loop until there is no location).
Thus, I emit the receipt of a user token for the Instagram API, everything takes 3 cycles.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question