我正在制作一个命令行应用程序.在执行登录过程后,我需要同时通过cURL发出多个POST请求 – 这意味着传出请求必须发送会话ID等.
事件链如下:
>我用curl_init打开cURL连接
>我使用curl_exec登录远程站点发送POST请求,并获得返回的HTML代码作为响应
>我同时向同一站点发送多个POST请求.
我在考虑使用类似的东西:
// Init connection
$ch = curl_init();
// Set curl options
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookies.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookies.txt');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_POST, 1);
// Perform login
curl_setopt($ch, CURLOPT_URL, "http://www.mysite/login.php");
$post = array('username' => 'username' , 'password' => 'password');
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));
$result = curl_exec($ch);
// Send multiple requests after being logged on
curl_setopt($ch, CURLOPT_TIMEOUT_MS, 1);
for($i = 0 ; $i < 10 ; $i++){
$post = array('myvar' => 'changing_value');
curl_setopt($ch, CURLOPT_URL, 'www.myweb.ee/changing_url');
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));
curl_exec($ch);
}
但这似乎不起作用,因为只有循环中的第一个请求似乎被发送.
使用curl_multi_init可能是一个解决方案,但我不知道我是否可以多次传递相同的cURL句柄,每个都有更改的选项.
对于那些同时发出的请求,我不需要服务器的任何响应,但如果它也可以以某种方式完成,那将是很棒的.
如果有人能够把我推向正确的方向,那将是完美的.