php
  • curl
  • instagram
  • 2017-01-09 72 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'

    相關問題