密码网站上的PHP curl multi



我目前正在使用以下(旧)代码登录网站。。。

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_multi-WITH登录的示例。我只是用curl_multi_exec替换所有curl_exec吗?此外,如果你看到任何其他明显的错误,欢迎评论。

需要明确的是,我希望使用单个用户/通行证登录,然后将这些凭据用于多个页面请求。

这已经有一段时间了,但我想发布我的最终解决方案。我发现了一个很棒的多卷曲库,滚动卷曲,这很有帮助。基本上,在收集登录cookie(如我最初的问题所示)后,我将其和其他选项反馈到每个多请求的滚动curl实例中,然后执行批处理。工作起来很有魅力。

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的文档很好地描述了这一点,所以我不会在这里重复。

最新更新