2017-01-16 11 views
0

私はカールとPHPとInstagramのにログインだし、それがこのようなテキストは、それぞれの間にいくつかのバリエーションがエコーされリダイレクトループに入るがPHPカールログインが

> HTTP/1.1 301 Moved Permanently Location: https://www.instagram.com/ 
> Content-Type: text/plain Server: proxygen Date: Mon, 16 Jan 2017 
> 12:53:26 GMT Connection: keep-alive Content-Length: 0 
> 
> HTTP/1.1 200 OK Content-Type: text/html Vary: Cookie, Accept-Language, 
> Accept-Encoding Content-Language: en Pragma: no-cache Expires: Sat, 01 
> Jan 2000 00:00:00 GMT Strict-Transport-Security: max-age=86400 Date: 
> Mon, 16 Jan 2017 12:53:27 GMT X-Frame-Options: SAMEORIGIN 
> Content-Encoding: gzip Cache-Control: private, no-cache, no-store, 
> must-revalidate Set-Cookie: sessionid=; expires=Thu, 01-Jan-1970 
> 00:00:00 GMT; Max-Age=0; Path=/; HttpOnly; Domain=instagram.com 
> Set-Cookie: csrftoken=uWHWfgmVVhdROoG3HsyIevXMq4mcEGVU; expires=Mon, 
> 15-Jan-2018 12:53:27 GMT; Max-Age=31449600; Path=/; Secure Connection: 
> keep-alive Content-Length: 3373 

エコーリダイレクトループに入ります私が使用しているコードは、このあなたがミスのカップルを行う

<?php 

    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/pruebalogininsta3.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-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=http_build_query(array('username'=>$usuario,'password'=>$password)); 


     # Creo que falta agregar la variable POST para que mande un POST y no un GET 

     curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
     curl_setopt($ch, CURLOPT_POSTFIELDS, $sPost); 
     curl_setopt($ch, CURLOPT_ENCODING, 'gzip, deflate'); 
     curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); 
     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; 

    ?> 
+0

あなたは本当のユーザー名とパスワードの組み合わせは公開されていますか?私は、この@pablodalmazzoアカウントが、私が掲示した使い捨てアカウントのような、問題なく妥協できることを願っています。そうでない場合は、パスワードをできるだけ早く変更する必要があります。 – hanshenrik

+0

ありがとう、私は気づいていませんでしたが、そこには何も重要ではありません – Pablo

+0

あなたがhttp://stackoverflow.com/posts/41676960/revisionsで公開されている編集履歴をチェックすると、ユーザ名/パスワードがまだ見えることに気づくでしょう。パスワードを – hanshenrik

答えて

2

1:手動'Accept-Encoding: gzip, deflate',ヘッダを追加します。サーバーが実際にgzip/deflateを使用することを選択した場合、応答本体はすべての文字化け(バイナリ圧縮データ)を表示します。圧縮を使用する場合はCURLOPT_ENCODINGを使用し、curlは圧縮解除します。 (少なくとも2回目の呼び出しで正しく実行します)

2:Referer: https://www.instagram.com/を追加してください。後でその瞬間をcurl_setopt($ch, CURLOPT_REFERER, $sTarget);で上書きして、カスタムヘッダーを無視し、AND $ sTargetは未定義の変数のようです。

3:httpヘッダーにキャプション文字を使用します。私が見る限り、ログインプロトコルはどんなヘッダーでも大文字を使用しません。

<?php 
declare(strict_types = 1); 
require_once('hhb_.inc.php'); 
$hc = new hhb_curl(); 
$hc->_setComfortableOptions(); 

$username = 'nigeriansdddd'; 
$password = '[email protected]'; 
// get a cookie session and login page etc 
$hc->exec ('https://www.instagram.com/'); 
// hhb_var_dump($hc->getStdErr(),$hc->getResponseBody()); 
$html = $hc->getResponseBody(); 
$token = getCsrfToken ($html); 
$hc->setopt_array (array (
     CURLOPT_URL => 'https://www.instagram.com/accounts/login/ajax/', 
     CURLOPT_POST => true, 
     CURLOPT_HTTPHEADER => array (
       'x-csrftoken: ' . $token, 
       'x-instagram-ajax: 1', 
       'x-requested-with: XMLHttpRequest', 
       'accept-language: en-US,en;q=0.8,nb;q=0.6', 
       'origin: https://www.instagram.com', 
       'referer: https://www.instagram.com/' 
     ) 
     , 
     CURLOPT_POSTFIELDS => http_build_query (array (
       'username' => $username, 
       'password' => $password 
     )) 
)); 
$hc->exec(); 
// hhb_var_dump ($hc->getStdErr(), $hc->getResponseBody()); 
$jsonRAW = $hc->getResponseBody(); 
$json = json_decode ($jsonRAW, true); 
if (! is_array ($json) || $json ['status'] !== 'ok' || $json ['authenticated'] !== true) { 
    throw new RuntimeException ('failed to login. last curl request: ' . $hc->getStdErr() . $hc->getResponseBody()); 
} 
$hc->exec ('https://www.instagram.com/'); 
$html = $hc->getResponseBody(); 
if (false === strpos ($html, $username)) { 
    throw new RuntimeException ('failed to confirm login. last curl request: ' . $hc->getStdErr() . $hc->getResponseBody()); 
} 
hhb_var_dump ($hc->getStdErr(), $hc->getResponseBody()); 


function getCsrfToken(string $html): string { 
    $matches = array(); 
    $rexret = preg_match ('/\"csrf_token\"\s*\:\s*\"([^\"]*)/', $html, $matches); 
    if ($rexret !== 1) { 
     throw new RuntimeException ('failed to find the csrf token!'); 
    } 
    // hhb_var_dump($rexret,$matches); 
    $csrftoken = $matches [1]; 
    return $csrftoken; 
} 

(注:アカウント、nigeriansddddここで働くのコード例だ

https://github.com/divinity76/hhb_.inc.php/blob/master/hhb_.inc.phpからhhb_curlを使用して

(たとえば、あなたはX-Instagram-AJAX: 1それを呼び出すが、ログインページがx-instagram-ajax: 1それを呼び出します)/[email protected]はちょうどダミーのスローアウェイアカウントです。アカウントが侵害された場合は気にしません。クレデンシャルをここに投稿すると明らかに発生します)

+0

私はコードを試しましたが、それは動作していないようです。ログイン後にメインフィードを取得してもいいですか?そのライブラリはphp7が必要なので、私はphp7を使ったホスティングでアカウントを作成しましたが、同じ結果が得られました。ここではhttps://yotambienmerio.000webhostapp.com/phpversion.pngです。ここで致命的なエラーが表示されますhttps://yotambienmerio.000webhostapp.com/codigo_del_pibe.php – Pablo

+1

@Pabloはいメインページを取得する必要があります。奇妙な、それは私のために働く。それは 'if(!is_array($ json)|| $ json ['status']!== 'ok' || $ json ['authenticated']!== true){'、そのコードを'$ json ['status']!== 'ok' || $ json ['authenticated']!== true){ hhb_var_dump($ hc-> getStdErr()、 $ hc-> getResponseBody()); 新しいRuntimeExceptionをスローする( 'ログインに失敗しました。参照stdout'); } 'を実行し、もう一度実行してください。何を得るのですか? – hanshenrik

+1

@Pablo奇妙なことに、私のテストアカウントが削除されました。「あなたが入力したユーザー名は、アカウントに属していません。あなたのユーザ名を確認してからもう一度やり直してください.' - テストコードがそのアカウントで動作しないのは不思議ではありませんが、実際のアカウントでテストしましたか? – hanshenrik