我目前正在使用以下(旧)代码登录网站...
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_execs 吗?此外,如果您发现任何其他明显的错误,当然欢迎发表评论。
需要明确的是,我想使用单个用户/密码登录,然后将这些凭据重用于多个页面请求。