我目前正在使用以下(旧)代码登录网站...
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 login的例子。我只是用curl_multi_exec替换所有curl_execs吗? 此外,如果你看到任何其他明显的错误,肯定会受到评论。
要明确的是,我想使用单个用户/通行证登录,然后将这些凭据重新用于多个页面请求。
答案 0 :(得分: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的文档很好地描述了这一点,所以我不会在这里重申它。