PHP的cURL花了很长时间

时间:2014-08-22 18:21:22

标签: php cookies curl

我有这个PHP代码登录到远程网站并提取一些数据:

$random=rand(1, 100000);
$cookie=$random . ".txt";
$agent="Mozilla/5.001 (windows; U; NT4.0; en-US; rv:1.0) Gecko/25250101";

function doRequest($method, $url, $referer, $agent, $cookie, $vars) {
$ch=curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
if($referer != "") {
curl_setopt($ch, CURLOPT_REFERER, $referer);
}
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT,1);
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);
if ($method == 'POST') {
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $vars);
}
if (substr($url, 0, 5) == "https") {
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
}
$data = curl_exec($ch);
curl_close($ch);
if ($data) {
    return $data;
} else {
    return curl_error($ch);
}
}

function get($url, $referer, $agent, $cookie) {
return doRequest('GET', $url, $referer, $agent, $cookie, 'NULL');
}

function post($url, $referer, $agent, $cookie,  $vars) {
return doRequest('POST', $url, $referer, $agent, $cookie, $vars);
}

我执行了四次post函数和两次get函数。问题是加载和执行需要花费很长时间。

0 个答案:

没有答案