Answer the question
In order to leave comments, you need to log in
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
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);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question