2

私は現在、次の(古い)コードを使用してサイトにログインしています...

public function login() {
    $url1 = 'https://...';  /* Initial page load to collect initial session cookie data */
    $url2 = 'https://...';  /* The page to POST login data to */
    $url3 = 'https://...';  /* The page redirected to to test for success */
    $un = 'user';
    $pw = 'pass';

    $post_data = array(
        'authmethod' => 'on',
        'username'   => $un,
        'password'   => $pw,
        'hrpwd'      => $pw
    );

    $curlOpt1 = array(
        CURLOPT_URL            => $url1,
        CURLOPT_COOKIEJAR      => self::COOKIEFILE,
        CURLOPT_COOKIEFILE     => self::COOKIEFILE,
        CURLOPT_FOLLOWLOCATION => TRUE,
        CURLOPT_HEADER         => FALSE,
        CURLOPT_RETURNTRANSFER => TRUE,
        CURLOPT_SSL_VERIFYPEER => FALSE
    );

    $curlOpt2 = array(
        CURLOPT_URL            => $url2,
        CURLOPT_COOKIEJAR      => self::COOKIEFILE,
        CURLOPT_COOKIEFILE     => self::COOKIEFILE,
        CURLOPT_FOLLOWLOCATION => TRUE,
        CURLOPT_POST           => TRUE,
        CURLOPT_POSTFIELDS     => http_build_query($post_data)
    );

    $this->ch = curl_init();
    if ( !$this->ch ) {
        throw new Exception('Unable to init curl. ' . curl_error($curl));
    }

    /* Load the login page once to get the session ID cookies */
    curl_setopt_array( $this->ch, $curlOpt1 );
    if ( !curl_exec( $this->ch ) ) {            
        throw new Exception( 'Unable to retrieve initial auth cookie.' );
    }

    /* POST the login data to the login page */
    curl_setopt_array($this->ch, $curlOpt2);
    if ( !curl_exec( $this->ch ) ) {
        throw new Exception( 'Unable to post login data.' );
    }

    /* Verify the login by checking the redirected url. */
    $header  = curl_getinfo( $this->ch );
    $retUrl = $header['url'];

    if ( $retUrl == $url3 ) {
        /* Reload the login page to get the auth cookies */
        curl_setopt_array( $this->ch, $curlOpt1 );
        if ( curl_exec( $this->ch ) ) {
            return true;
        } else {
            throw new Exception( 'Unable to retrieve final auth cookie.' );
        }
    } else {
        throw new Exception( 'Login validation failure.' );
    }

    return false;
}

次に使用します...

public function getHtml($url) {
    $html = FALSE;

    try {
        curl_setopt($this->ch, CURLOPT_URL, $url);
        $page = curl_exec($this->ch);
    } catch (Exception $e) {
        ...
    }

    /* Remove all tabs and newlines from the HTML */
    $rmv = array("\n","\t");
    $html = str_replace($rmv, '', $page);

    return $html;
}

...ページリクエストごと。私の質問は、これをcurl_multi_execを使用して数百のルックアップを高速化するように変換するにはどうすればよいですか?curl_multiWITHloginの例が見つかりません。すべてのcurl_execをcurl_multi_execに置き換えるだけですか?また、他に明白な間違いを見つけた場合は、コメントを歓迎します。

明確にするために、単一のユーザー/パスでログインしてから、それらの資格情報を複数のページ要求に再利用したいと思います。

4

1 に答える 1

1

久しぶりですが、最終的な解決策を投稿したいと思いました。私はそれが助けになった素晴らしいマルチカールライブラリ、rolling-curlを見つけました。基本的に、ログインCookie(元の質問に表示)を収集した後、それと他のオプションをフィードして、マルチリクエストごとにローリングカールインスタンスに戻し、バッチを実行します。チャームのように機能します。

public function getMultiPage(array $urls, $url_prepend=NULL, $callback) {
    $rc = new RollingCurl(array('Att_Screen_Scraper', $callback));
    $rc->window_size = 15;   /* number of threads to run */
    $rc->options = array(
        CURLOPT_COOKIEJAR      => self::COOKIEFILE,
        CURLOPT_COOKIEFILE     => self::COOKIEFILE,
        CURLOPT_FOLLOWLOCATION => TRUE,
        CURLOPT_HEADER         => FALSE,
        CURLOPT_RETURNTRANSFER => TRUE,
        CURLOPT_SSL_VERIFYPEER => FALSE
    );

    foreach ($urls as $i=>$url) {
        $request = new RollingCurlRequest($url_prepend . $url);
        echo $url_prepend . $url . "<br>\n";
        $rc->add($request);
    }

    if(!$rc->execute()) {
        throw new Exception('RollingCurl execute failed');
    }

    return TRUE;
}

このソリューションでは、各リクエストの戻りを処理するためにコールバックが必要であることに注意してください。RollingCurlのドキュメントにはこれがよく説明されているので、ここでは繰り返し説明しません。

于 2012-05-16T18:41:54.087 に答える