<?php
function getRequestCookie($webHomePageUrl, $cookieFileName) {
//初始化一个curl会话
$curl = curl_init();
$options = array(
CURLOPT_URL => $webHomePageUrl, //设置请求的url
CURLOPT_HTTPGET => 1, //以get方式请求
CURLOPT_COOKIEJAR => $cookieFileName, //连接结束后保存cookie信息到文件。
CURLOPT_TIMEOUT => 300, //设置CURL允许执行的最长秒数。
CURLOPT_RETURNTRANSFER => true, //将curl_exec()获取的信息以文件流的形式返回,也就是不直接输出。
CURLOPT_HEADER => TRUE //启用时会将头文件的信息作为数据流输出。
);
curl_setopt_array($curl, $options);
$result = curl_exec($curl);
if ($result === false) {
echo 'Curl error:' . curl_errno($curl);
}
curl_close($curl);
return $result;
}
$webHomePageUrl = 'http://www.oldwebadmin.com';
$cookieFileName = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'cookie.log';
getRequestCookie($webHomePageUrl, $cookieFileName);
function simulatePOSTRequest($requestUrl, $userAgent, $postData, $cookieFileName) {
//初始化一个curl会话
$curl = curl_init();
//curl_setopt 设置curl的传输选项
$options = array(CURLOPT_URL => $requestUrl,
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => $postData,
CURLOPT_HEADER => TRUE, //启用时会将头文件信息作为数据流输出
CURLOPT_COOKIEFILE => $cookieFileName, //包含cookie数据的文件名
CURLOPT_RETURNTRANSFER => TRUE, //将curl_exec()获取的信息以文件流的形式返回,也就是不直接输出。
);
//为curl传输会话批量设置选项
curl_setopt_array($curl, $options);
$result = curl_exec($curl);
if ($result === false) {
echo 'Curl error:' . curl_errno($curl);
}
curl_close($curl);
return $result;
}
$loginUrl = 'http://www.oldwebadmin.com/login';
$userAgentInfo = 'Mozilla/5.0 (Windows NT 6.2; WOW64; rv:40.0) Gecko/20100101 Firefox/40.0';
$postData = array('username' => 'username', 'password' => 'password');
$doLoginInfo = simulatePOSTRequest($loginUrl, $userAgentInfo, $postData, $cookieFileName);
class myThread extends Thread {
private $opInfoFile;
public function __construct($fileNames) {
$parentDir = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'thtest' . DIRECTORY_SEPARATOR;
$this->opInfoFile = $parentDir . $fileNames;
}
public function run() {
$userAgentInfo = 'Mozilla/5.0 (Windows NT 6.2; WOW64; rv:40.0) Gecko/20100101 Firefox/40.0';
$cookieFileName = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'cookie.log';
usleep(10);
$oprate = 'http://www.oldwebadmin.com/op';
$opostData = array('data1' => 'test1', 'data2' => 'test1', 'data3' => 'test2');
$opInfo = simulatePOSTRequest($oprate, $userAgentInfo, $opostData, $cookieFileName);
file_put_contents($this->opInfoFile, serialize($opInfo));
}
}
$threadPool = array();
for ($i = 0; $i < 100; $i++) {
$threadPool[$i] = new myThread('thtest'.$i.'.log');
$threadPool[$i]->start();
}