php
  • curl
  • instagram
  • 2017-01-09 74 views 2 likes 
    2
    include_once('simple_html_dom.php'); 
    
        $usuario = "username"; 
        $password = "password"; 
    
        $url = 'https://www.instagram.com/'; 
        $url_login = 'https://www.instagram.com/accounts/login/ajax/'; 
        $user_agent = array("Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 ", 
            "(KHTML, like Gecko) Chrome/48.0.2564.103 Safari/537.36"); 
    
        $ch = curl_init(); 
    
        $headers = [ 
        'Accept-Encoding: gzip, deflate', 
        'Accept-Language: en-US;q=0.6,en;q=0.4', 
        'Connection: keep-alive', 
        'Content-Length: 0', 
        'Host: www.instagram.com', 
        'Origin: https://www.instagram.com', 
        'Referer: https://www.instagram.com/', 
        'User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.103 Safari/537.36', 
        'X-Instagram-AJAX: 1', 
        'X-Requested-With: XMLHttpRequest' 
        ]; 
    
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
        curl_setopt($ch, CURLOPT_URL, $url); 
    
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); 
        curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); 
        curl_setopt($ch, CURLOPT_COOKIEFILE, "/tmp/cookie/pruebalogininsta2.txt"); 
        curl_setopt($ch, CURLOPT_REFERER, $sTarget); 
        curl_setopt($ch, CURLOPT_HEADER, TRUE); 
    
        $html = curl_exec($ch); 
    
        preg_match_all('/^Set-Cookie:\s*([^;]*)/mi', $html, $matches); 
        $cookies = array(); 
        foreach($matches[1] as $item) { 
         parse_str($item, $cookie); 
         $cookies = array_merge($cookies, $cookie); 
        } 
    
    
        $headers = [ 
        'Accept-Encoding: gzip, deflate', 
        //'Accept-Language: ru-RU,ru;q=0.8,en-US;q=0.6,en;q=0.4', 
        'Accept-Language: en-US;q=0.6,en;q=0.4', 
        'Connection: keep-alive', 
        'Content-Length: 0', 
        'Host: www.instagram.com', 
        'Origin: https://www.instagram.com', 
        'Referer: https://www.instagram.com/', 
        'User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.103 Safari/537.36', 
        'X-Instagram-AJAX: 1', 
        'X-Requested-With: XMLHttpRequest' 
        ]; 
    
        $cadena_agregar_vector = 'X-CSRFToken:'. $cookies["csrftoken"]; 
    
        $headers[] = $cadena_agregar_vector ; 
    
        $sPost = "username=".$usuario . "&password=". $password ; 
    
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
        curl_setopt($ch, CURLOPT_POSTFIELDS, $sPost); 
        curl_setopt($ch, CURLOPT_URL, $url_login); 
    
        $html2 = curl_exec($ch); 
    
        curl_setopt($ch, CURLOPT_URL, "http://www.instagram.com/"); 
    
        $html4 = curl_exec($ch); 
    
        echo $html4; 
    

    这就是我得到enter image description herePHP卷曲Instagram的回报奇结果

    回答

    1

    问题是你硬编码Accept-Encoding: gzip, deflate的方式,这使得卷曲确实发送编码头,但它不会对解码功能开启的卷曲,因此你可以得到原始数据,而不需要为你解码。

    删除'Accept-Encoding: gzip, deflate',并添加curl_setopt($ch, CURLOPT_ENCODING, 'gzip, deflate');,和卷曲将其解码为你(前提是卷曲用gzip &放气支持编译) - 或更好,但只是做curl_setopt($ch, CURLOPT_ENCODING, '');,和卷曲会自动列出所有支持的编码,所以你不要遇到编码问题,其中curl不是用gzip支持编译的。

    对不相干的笔记,您可能想要使用CURLOPT_USERAGENT,而不是手动设置用户代理标头。否则,UA字符串只会与这1个请求一起发送,并在下一个请求时复位,而CURLOPT_USERAGENT被保留,直到curl_close($ ch)

    编辑:在我的第一次修改这篇文章时,我写了CURLOPT_POSTFIELDS而不是CURLOPT_ENCODING,对不起,修正了

    编辑2:在另一个不相关的记录上,你编码的用户名/密码错误。而不是$sPost = "username=".$usuario . "&password=". $password ;,做 $sPost=http_build_query(array('username'=>$usuario,'password'=>$password));,否则有&账户或=或密码或用户名空值不会正常工作

    +0

    我不明白你的意思是“其他帐户与&或=或NULL或密码或用户名将无法正常工作”?你能用另一种方式来说吗? – Pablo

    +0

    @Pablo用密码“Adam&Eve = Human”开一个帐户,你会明白我的意思。它会尝试使用错误的密码登录,因为'&'和'='编码不正确。正确的编码,http_build_query会给你,实际上是'Adam%26Eve%3DHuman' – hanshenrik

    +0

    啊我现在明白了,谢谢! – Pablo

    1

    发表@hanshenrik答案应该被接受。但是,如果你只是想要一个简单的解决方案,并且没有错误,请从头文件数组中删除'Accept-Encoding: gzip, deflate'

    相关问题