I'm trying to programmatically login to Instagram via my own website because I want to retrieve direct messages from Instagram (this requires a login as it is not supported in the Instagram API (yet)). But the Instagram login page requires cookies to login.
I keep getting the message that the page could not be loaded and that I might need to enable cookies.
Is there any way possible to login programmatically via PHP to Instagram?
This is what I have so far.
$ch = curl_init('https://instagram.com/accounts/login/');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
$result = curl_exec($ch);
preg_match_all('/^Set-Cookie:\s*([^\r\n]*)/mi', $result, $ms);
$cookies = array();
foreach ($ms[1] as $m) {
list($name, $value) = explode('=', $m, 2);
$cookies[$name] = $value;
}
$ccode = substr($cookies['ccode'], 0, 2);
$mid = array_shift(explode(';', $cookies['mid']));
$csfrtoken = array_shift(explode(';', $cookies['csrftoken']));
$header = array();
$header[] = 'Accept: */*';
$header[] = 'Accept-Encoding: gzip,deflate';
$header[] = 'Accept-Language: nl-NL,nl;q=0.8,en-US;q=0.6,en;q=0.4';
$header[] = 'Connection: keep-alive';
$header[] = 'Content-Length: 46';
$header[] = 'Content-Type: application/x-www-form-urlencoded; charset=UTF-8';
$header[] = 'X-Instagram-AJAX: 1';
$header[] = 'X-Requested-With: XMLHttpRequest';
$ch = curl_init('https://instagram.com/accounts/login/ajax/');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'username=*****&password=*****&intent=');
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.120 Safari/537.36');
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_COOKIEJAR, dirname(__FILE__).'/test.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, dirname(__FILE__).'/test.txt');
curl_setopt($ch, CURLOPT_COOKIESESSION, true);
curl_setopt($ch, CURLOPT_COOKIE, 'mid='.$mid.'; ccode='.$ccode.'; csrftoken='.$csfrtoken.';');
curl_setopt($ch, CURLOPT_ENCODING, '');
$response = curl_exec($ch);
Authentication — Instagram Basic Display is not an authentication solution. Data returned by the API cannot be used to authenticate your app users or log them into your app. If you need an authentication solution we recommend using Facebook Login instead.
7502748966%7D"; $url = "https://www.instagram.com/accounts/login/"; $ch = @curl_init(); curl_setopt($ch, CURLOPT_URL, $url); $head[] = "Connection: keep-alive"; $head[] = "Keep-Alive: 300"; $head[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7"; $head[] = "Accept-Language: en-us,en;q=0.5"; curl_setopt($ch, ...
1. On the login screen, under the username and password fields, tap Get help logging in (if you're using an Android phone) or Forgot password? (on iOS or a browser). 2. Enter your email address, phone number or username and tap Next (if you are using Android) or Send Login Link (in a web browser).
Tap or your profile picture in the bottom right to go to your profile. Tap in the top right, then tap Your activity. Tap Download your information. Enter the email address where you'd like to receive a link to your data, then tap Request Download.
It looks like you were headed in the right direction, in your example, it seems that you get an authenticated response but a consequent request to an authenticated page does not work as expected.
I suspect that instagram is actively preventing this by running a check with an Ajax call or something similar.
As an alternative you can look at something like zombie.js.
This is a headless virtual browser where you can visit pages and interact with the elements on it, but you cant view them.
A simple example using zombie.js is as follows:
var Browser = require('zombie');
var browser = Browser.create();
browser.visit('https://instagram.com/', function() {
browser.wait(10000, function(){
browser.fill('input[name="username"]', 'myusername');
browser.fill('input[type="password"]', 'mypasswd');
browser.pressButton('Log in', function() {
browser.visit('https://instagram.com/accounts/edit/', function() {
console.log(browser.html());
});
});
});
});
Hope it helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With