我有一个登录网站并在下一页中搜索字符串的功能。该过程目前需要 10 秒,但想看看是否有什么我可以做的来加快它。我想知道是否可以让 curl 登录在客户端会话中持续存在,或者更好地搜索文档。
public function curlLogin($url, $post_values, $cookieJar) {
$timeout = 30;
$curl_connection = curl_init();
curl_setopt($curl_connection, CURLOPT_URL, $url);
curl_setopt($curl_connection, CURLOPT_TIMEOUT, $timeout);
curl_setopt($curl_connection, CURLOPT_USERAGENT,"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
curl_setopt($curl_connection, CURLOPT_COOKIEJAR, $cookieJar);
curl_setopt($curl_connection, CURLOPT_COOKIEFILE, $cookieJar);
curl_setopt($curl_connection, CURLOPT_COOKIESESSION, 0);
curl_setopt($curl_connection, CURLOPT_HEADER, 1);
curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl_connection, CURLOPT_POST, 1);
curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post_values);
curl_setopt($curl_connection, CURLOPT_HTTPHEADER,
array("Content-type: application/x-www-form-urlencoded"));
curl_exec($curl_connection);
return $curl_connection;
}
public function curlPost($curl_connection, $url, $post_values, $cookieJar) {
$timeout = 30;
curl_setopt($curl_connection, CURLOPT_URL, $url);
curl_setopt($curl_connection, CURLOPT_TIMEOUT, $timeout);
curl_setopt($curl_connection, CURLOPT_USERAGENT,"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
curl_setopt($curl_connection, CURLOPT_COOKIEJAR, $cookieJar);
curl_setopt($curl_connection, CURLOPT_COOKIEFILE, $cookieJar);
curl_setopt($curl_connection, CURLOPT_COOKIESESSION, 0);
curl_setopt($curl_connection, CURLOPT_HEADER, 1);
curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl_connection, CURLOPT_POST, 1);
curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post_values);
curl_setopt($curl_connection, CURLOPT_HTTPHEADER,
array("Content-type: application/x-www-form-urlencoded"));
$result = curl_exec($curl_connection);
return $result;
}
$cookieJar = tempnam ("/tmp", "CURLCOOKIE");
$curl_connection = $this->curlLogin($login_url, $post_values, $cookieJar);
$result = $this->curlPost($curl_connection, $next_url, $params, $cookieJar);
if (strpos($result,'string 1') > 0) {
$success = true;
$message = 'string 1 is present';
}else if (strpos($result,'string 2') > 0){
$success = false;
$message = 'string 2 is present';
}else if (strpos($result,'string 3') > 0){
$success = false;
$message = 'string 3 is present';
}else{
$success = false;
$message = 'None of the above strings are present.';
}
curl_close($curl_connection);
unlink($cookieJar);