Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/231.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Instagram以编程方式登录_Php_Cookies_Instagram_Messages - Fatal编程技术网

Php Instagram以编程方式登录

Php Instagram以编程方式登录,php,cookies,instagram,messages,Php,Cookies,Instagram,Messages,我试图通过自己的网站以编程方式登录Instagram,因为我想从Instagram检索直接消息(这需要登录,因为Instagram API(目前)不支持)。但是Instagram登录页面需要cookies才能登录 我不断收到消息,页面无法加载,我可能需要启用cookies 有没有办法通过PHP编程登录Instagram 这就是我目前所拥有的 $ch = curl_init('https://instagram.com/accounts/login/'); curl_setopt($ch, CUR

我试图通过自己的网站以编程方式登录Instagram,因为我想从Instagram检索直接消息(这需要登录,因为Instagram API(目前)不支持)。但是Instagram登录页面需要cookies才能登录

我不断收到消息,页面无法加载,我可能需要启用cookies

有没有办法通过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);

您可以使用带有“force\u classic\u login”参数的经典登录表单

<?php
    $username = "username";
    $password = "password";
    $useragent = "Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.13) Gecko/20101206 Ubuntu/10.10 (maverick) Firefox/3.6.13";
    $cookie=$username.".txt";

    $url="https://instagram.com/accounts/login/?force_classic_login";

    $ch  = curl_init();
    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);

    // try to find the actual login form
    if (!preg_match('/<form method="POST" id="login-form" class="adjacent".*?<\/form>/is', $page, $form)) {
        throw Instagram_Manager('Failed to find log in form!');
    }

    $form = $form[0];

    // find the action of the login form
    if (!preg_match('/action="([^"]+)"/i', $form, $action)) {
        throw Instagram_Manager('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);

    // set additional curl options using our previous options
    curl_setopt($ch, CURLOPT_URL, "https://instagram.com/".$url2);
    curl_setopt($ch, CURLOPT_REFERER, $url);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
   $page = curl_exec($ch);

    // connect to profile edit page
    $url="https://instagram.com/accounts/edit/";
    curl_setopt($ch, CURLOPT_URL, $url);
    echo curl_exec($ch);    
?>

看起来您的方向是正确的,在您的示例中,您似乎得到了一个经过身份验证的响应,但是对经过身份验证的页面的后续请求没有按预期工作

我怀疑instagram通过使用Ajax调用或类似的方式运行检查来积极防止这种情况

作为替代方案,您可以查看以下内容

这是一个无头虚拟浏览器,您可以在其中访问页面并与页面上的元素交互,但无法查看它们

使用的简单示例如下所示:

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());
      });
    });

  });

});

希望能有所帮助。

我非常尊敬@Fatih Kısa的代码,干得很好。我已经尝试过这段代码,但目前它不起作用,可能是因为Instagram服务器端做了一些更改。我已经用他的代码玩了2天,并强迫它与我的小改动一起工作。 这段代码非常重要的一部分是Instagram只接受包含cookies数据(csrftoken和mid)的带有curl referer的post表单。另一个重要的部分是,您必须仅与WWW一起使用,并在创建Cookie后删除关于卷曲信息的字符串:

#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,如Gecko)Ubuntu Chromium/50.0.2661.102 Chrome/50.0.2661.102 Safari/537.36”;
$cookie=$username.“.txt”;
@取消链接(dirname(_文件).“/”$cookie);
$url=”https://www.instagram.com/accounts/login/?force_classic_login";
$ch=curl_init();
$arrSetHeaders=数组(
“用户代理:$useragent”,
'接受:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'接受语言:en-US,en;q=0.5',
'接受编码:放气,br',
“连接:保持活动状态”,
'缓存控制:最大年龄=0',
);
curl_setopt($ch,CURLOPT_HTTPHEADER,$arrSetHeaders);
curl_setopt($ch,CURLOPT_URL,$URL);
curl_setopt($ch,CURLOPT_COOKIEJAR,dirname(_文件).“/”$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_头,1);
curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1);
$page=curl\u exec($ch);
卷曲关闭($ch);
//尝试查找实际的登录表单

如果(!preg_match('/@Vaha的代码停止工作

改变

$cookieFileContent = str_replace('sessionid=; ', '', $cookieFileContent);
为了

代码也会起作用


define('USERNAME',“”);
定义('PASSWORD',“”);
定义('USERAGENT',“Mozilla/5.0(Windows NT 6.1;Win64;x64)AppleWebKit/537.36(KHTML,类似Gecko)Chrome/66.0.3359.181 Safari/537.36”);
定义('COOKIE',USERNAME..txt');
函数登录\u inst(){
@取消链接(dirname(_文件).“/!instagram/”.COOKIE);
$url=”https://www.instagram.com/accounts/login/?force_classic_login";
$ch=curl_init();
$arrSetHeaders=数组(
“用户代理:用户代理”,
'接受:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'接受语言:en-US,en;q=0.5',
'接受编码:放气,br',
“连接:保持活动状态”,
'缓存控制:最大年龄=0',
);
curl_setopt($ch,CURLOPT_HTTPHEADER,$arrSetHeaders);
curl_setopt($ch,CURLOPT_URL,$URL);
curl_setopt($ch,CURLOPT_COOKIEJAR,dirname(uuu FILE_uuu)。“/!instagram/”.COOKIE);
curl_setopt($ch,CURLOPT_COOKIEFILE,dirname(uuu FILE_uuu)。“/!instagram/”.COOKIE);
curl_setopt($ch,CURLOPT_USERAGENT,USERAGENT);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_头,1);
curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
$page=curl\u exec($ch);
卷曲关闭($ch);
//变量转储(第页);
//尝试查找实际的登录表单

如果(!preg_匹配(“/谢谢您的回答@Vaha,我设法为instagram登录制作了一些代码,以获取获取访问令牌所需的授权代码:我收到了无法找到登录表单的消息!。此代码是否仍然适用于您,@Vaha?我很久没有检查它。以前它工作得很好。您能告诉我,您是如何获得csrftoken和mid co的吗?”okies?将你的CURLOPT_标题选项设置为TRUE,这样你就可以在标题中找到它。ı现在可以获取csrftoken和mid Cookie,但我还有一些其他问题。ı可以获取你的facebook或电子邮件地址来问一些问题吗?(如果你有时间)我已经完成了建议的修复,但它看起来仍然不起作用。到目前为止它是否对您起作用?结果似乎是:@Serhii我收到消息无法找到登录表单!将我的工作代码添加到注释中
$cookieFileContent = str_replace('sessionid=; ', '', $cookieFileContent);
$cookieFileContent = str_replace('sessionid=""; ', '', $cookieFileContent);
define('USERNAME', "");
define('PASSWORD', "");
define('USERAGENT', "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36");
define('COOKIE', USERNAME.".txt");

function login_inst() {

    @unlink(dirname(__FILE__)."/!instagram/".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__)."/!instagram/".COOKIE);
    curl_setopt($ch, CURLOPT_COOKIEFILE, dirname(__FILE__)."/!instagram/".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_SSL_VERIFYPEER, false);

    $page = curl_exec($ch);
    curl_close($ch);  

    //var_dump($page);

    // try to find the actual login form
    if (!preg_match('/<form 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__)."/!instagram/".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__)."/!instagram/".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__)."/!instagram/".COOKIE);
    curl_setopt($ch, CURLOPT_COOKIEFILE, dirname(__FILE__)."/!instagram/".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);  
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

    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);  

}