- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我正在尝试通过我自己的网站以编程方式登录 Instagram,因为我想从 Instagram 检索直接消息(这需要登录,因为 Instagram API 尚不支持)。但是 Instagram 登录页面需要 cookie 才能登录。
我不断收到无法加载页面的消息,我可能需要启用 cookie。
是否有任何方法可以通过 PHP 以编程方式登录到 Instagram?
这就是我目前所拥有的。
$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);
最佳答案
我非常尊重@Fatih Kısa 的代码,干得非常好。我已经尝试过这段代码,但目前它不起作用,可能是因为 Instagram 服务器端发生了一些变化。我用他的代码玩了 2 天,并强制它与我的小改动一起工作。此代码中非常重要的部分是 Instagram 仅接受带有包含 cookie 数据(csrftoken 和 mid)的 curl referrer 的帖子表单。同样重要的部分是您必须使用 https://www.instagram.com/accounts/login/?force_classic_login ,仅用于 WWW 并在 cookie 创建后删除有关 curl 信息的字符串:
#Netscape HTTP Cookie 文件
#http://curl.haxx.se/docs/http-cookies.html
#这个文件是由libcurl生成的!编辑风险自负。
这是工作代码,尽情享受吧!
$username = "yourname";
$password = "yourpass";
$useragent = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/50.0.2661.102 Chrome/50.0.2661.102 Safari/537.36";
$cookie=$username.".txt";
@unlink(dirname(__FILE__)."/".$cookie);
$url="https://www.instagram.com/accounts/login/?force_classic_login";
$ch = curl_init();
$arrSetHeaders = array(
"User-Agent: $useragent",
'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Language: en-US,en;q=0.5',
'Accept-Encoding: deflate, br',
'Connection: keep-alive',
'cache-control: max-age=0',
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $arrSetHeaders);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_COOKIEJAR, dirname(__FILE__)."/".$cookie);
curl_setopt($ch, CURLOPT_COOKIEFILE, dirname(__FILE__)."/".$cookie);
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$page = curl_exec($ch);
curl_close($ch);
// try to find the actual login form
if (!preg_match('/<form data-encrypt method="POST" id="login-form" class="adjacent".*?<\/form>/is', $page, $form)) {
die('Failed to find log in form!');
}
$form = $form[0];
// find the action of the login form
if (!preg_match('/action="([^"]+)"/i', $form, $action)) {
die('Failed to find login form url');
}
$url2 = $action[1]; // this is our new post url
// find all hidden fields which we need to send with our login, this includes security tokens
$count = preg_match_all('/<input type="hidden"\s*name="([^"]*)"\s*value="([^"]*)"/i', $form, $hiddenFields);
$postFields = array();
// turn the hidden fields into an array
for ($i = 0; $i < $count; ++$i) {
$postFields[$hiddenFields[1][$i]] = $hiddenFields[2][$i];
}
// add our login values
$postFields['username'] = $username;
$postFields['password'] = $password;
$post = '';
// convert to string, this won't work as an array, form will not accept multipart/form-data, only application/x-www-form-urlencoded
foreach($postFields as $key => $value) {
$post .= $key . '=' . urlencode($value) . '&';
}
$post = substr($post, 0, -1);
preg_match_all('/^Set-Cookie:\s*([^;]*)/mi', $page, $matches);
$cookieFileContent = '';
foreach($matches[1] as $item)
{
$cookieFileContent .= "$item; ";
}
$cookieFileContent = rtrim($cookieFileContent, '; ');
$cookieFileContent = str_replace('sessionid=; ', '', $cookieFileContent);
$oldContent = file_get_contents(dirname(__FILE__)."/".$cookie);
$oldContArr = explode("\n", $oldContent);
if(count($oldContArr))
{
foreach($oldContArr as $k => $line)
{
if(strstr($line, '# '))
{
unset($oldContArr[$k]);
}
}
$newContent = implode("\n", $oldContArr);
$newContent = trim($newContent, "\n");
file_put_contents(
dirname(__FILE__)."/".$cookie,
$newContent
);
}
$arrSetHeaders = array(
'origin: https://www.instagram.com',
'authority: www.instagram.com',
'upgrade-insecure-requests: 1',
'Host: www.instagram.com',
"User-Agent: $useragent",
'content-type: application/x-www-form-urlencoded',
'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Accept-Language: en-US,en;q=0.5',
'Accept-Encoding: deflate, br',
"Referer: $url",
"Cookie: $cookieFileContent",
'Connection: keep-alive',
'cache-control: max-age=0',
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_COOKIEJAR, dirname(__FILE__)."/".$cookie);
curl_setopt($ch, CURLOPT_COOKIEFILE, dirname(__FILE__)."/".$cookie);
curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $arrSetHeaders);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_REFERER, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
sleep(5);
$page = curl_exec($ch);
preg_match_all('/^Set-Cookie:\s*([^;]*)/mi', $page, $matches);
$cookies = array();
foreach($matches[1] as $item) {
parse_str($item, $cookie1);
$cookies = array_merge($cookies, $cookie1);
}
var_dump($page);
curl_close($ch);
关于php - Instagram 以编程方式登录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26003063/
我想获取该用户所关注的 Instagram 用户列表。(但不是为我自己,为另一个用户)。 我做 API 调用 GET https://api.instagram.com/v1/users/423423
我提到过这个问题Instagram API: The access_token provided is invalid ,在我自己发布这个问题之前。 我的情况非常相似,我昨天刚刚注册了一个新应用程序,
Instagram 宣布弃用 Instagram 平台 API: “为了不断提高 Instagram 用户的隐私和安全,我们正在加速弃用 Instagram API 平台” 他们的文档和变更日志表示要
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 这个问题似乎与 help center 中定义的范围内的编程无关。 . 已关闭 3 年前。 Improve
我正在发送多条 Instagram 直接消息,并希望预先填充在我和我的关注者之间打开直接消息的 URL。我有每个粉丝的 Instagram ID 和用户名。 目前,Instagram 的“收件箱”地址
我正在做一个 Instagram 应用程序。首先想使用 Instagram 的端点。但是很多功能都被关闭了。用户搜索、关注者、关注列表等。 Instagram 宣布将于 7 月 31 日关闭许多 En
- (IBAction)postToInstagram:(id)sender { NSURL *instagramURL = [NSURL URLWithString:@"instagram://ap
我正在尝试创建一个程序,从 Instagram 帐户中检索喜欢、评论等的总数,不一定是评论的内容等,而只是给出特定帐户的喜欢和评论总数。这可能使用 Instagram API 吗? 最佳答案 所以我一
目前我使用 /users/self/media/liked方法,获取响应,阅读 next_max_like_id并一次又一次地请求数据。我试图通过巨大的 count值,但看起来最大计数值只是 30 .
我阅读了 instargam API 并在谷歌中搜索了代码,但没有得到满足我要求的任何确切解决方案。我想像 Facebook 插件一样在我的网站上显示我最近的照片。我的图像看起来像 - 我尝试了以下
我只是想知道是否有办法在 instagram 上获取用户的性别......我浏览了 instagram 的 api 并且从 users/userId 获得的用户信息不包括性别信息。 谢谢你的帮助。 最
假设我有一个 Instagram 帐户和一个网站。我想在网站上显示来自我的 Instagram 帐户的最新照片。我在文档中不清楚的东西:为了得到我的 access_token我需要验证自己的身份吗?我
我正在使用以下查询https://www.instagram.com/graphql/query/?query_id=17851374694183129&id={acountId}&first=100
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 7年前关闭。 Improve thi
我试图让我的应用程序能够使用除基本权限之外的其他范围,但是当我尝试使用其他范围时,我收到以下错误: Something went wrong :(stdClass Object ( [meta] =>
我正在尝试注册新 Client ID在 Instagram 上,用于提供 API。 但是不知道怎么填Privacy Policy URL .请指导我。 最佳答案 这是格式 @ http://www.g
我有一个显示我的 Instagram 动态的网站。以前我在用Instagram 遵循 API。用户/ self /媒体/最近 此 API 使用我生成一次的访问 token ,并在我的代码中作为变量保存
最近 Instagram 宣布支持多张照片发布。 我尝试使用端点,GET /media/media-id ,但响应只有一张图像的信息。 任何人都可以使用他们的 API 从单个多张照片帖子中检索所有图像
我想检查 Instagram 用户名是否以“正确”的方式可用。目前我正在做的是打开想要的用户名他们的 instagram 页面并检查状态代码是否为 404。但这会带来一个问题,因为如果名称的前一个所有
我正在考虑使用 insta API 的项目,但是当我注册 instagramdeveloper 帐户时,我遇到了一些问题。我找不到创建新客户端的按钮,当我点击管理客户端按钮时,这就是我得到的: 当我点
我是一名优秀的程序员,十分优秀!