Skip to content

Instantly share code, notes, and snippets.

@x-bart
Created November 27, 2015 04:49
Show Gist options
  • Save x-bart/5a512a4b6940a872d9fb to your computer and use it in GitHub Desktop.
Save x-bart/5a512a4b6940a872d9fb to your computer and use it in GitHub Desktop.
Like Instagram media (!need login&password)
<?php
class InstagramLike
{
const MAIN_URL = "https://instagram.com",
LOGIN_URL = "https://www.instagram.com/accounts/login/ajax/";
private $_csrf,
$_sessionid;
function __construct($username, $password)
{
$c = self::parseCookie($this->_makeCurl(self::MAIN_URL));
$this->_csrf = $c['csrftoken'];
$loginData = array( 'username' => $username,
'password' => $password);
$c = self::parseCookie($this->_makeCurl(self::LOGIN_URL, $this->_csrf, array("mid" => $c['mid']), $loginData));
$this->_csrf = $c['csrftoken'];
$this->_sessionid = $c['sessionid'];
}
function like($media)
{
$likeURL = "https://www.instagram.com/web/likes/{$media}/like/";
$r = $this->_makeCurl($likeURL, $this->_csrf, array("sessionid" => $this->_sessionid));
print_r($r);
}
private function _makeCurl($url, $csrf = null, $cookies = array(), $data = array())
{
$c = "";
foreach($cookies as $key => $value)
$c .= "{$key}={$value};";
$headers = array(
"cookie:csrftoken={$csrf};{$c}",
"referer:https://www.instagram.com/",
"x-csrftoken:{$csrf}");
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
if(!is_null($csrf))
{
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
}
curl_setopt($curl, CURLOPT_HEADER, true);
if(!is_null($csrf) && empty($data))
curl_setopt($curl, CURLOPT_HEADER, false);
// curl_setopt($curl, CURLOPT_VERBOSE, true);
$response = curl_exec($curl);
curl_close($curl);
return $response;
}
static function parseCookie($headers)
{
$pattern = '/Set-Cookie: ([a-z]*)=([^;]*);/';
preg_match_all($pattern, $headers, $matches);
$cookies = array();
foreach($matches[1] as $index => $name)
$cookies[$name] = $matches[2][$index];
return $cookies;
}
}
(new InstagramLike($username, $password))->like($media);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment