我需要向生成随机数的特定网页发出多个 GET 请求,然后使用该特定数字发出多个 POST 请求。到目前为止,我有这个代码:functions.php
set_time_limit(0);
function multiRequest($data, $options = array()) {
// array of curl handles
$curly = array();
// data to be returned
$result = array();
// multi handle
$mh = curl_multi_init();
// loop through $data and create curl handles
// then add them to the multi-handle
foreach ($data as $id => $d) {
$curly[$id] = curl_init();
$url = (is_array($d) && !empty($d['url'])) ? $d['url'] : $d;
curl_setopt($curly[$id], CURLOPT_URL, $url);
curl_setopt($curly[$id], CURLOPT_HEADER, 0);
curl_setopt($curly[$id], CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curly[$id], CURLOPT_USERAGENT, "Mozilla/5.0 ");
// post?
if (is_array($d)) {
if (!empty($d['post'])) {
curl_setopt($curly[$id], CURLOPT_POST, 1);
curl_setopt($curly[$id], CURLOPT_POSTFIELDS, $d['post']);
}
}
// extra options?
if (!empty($options)) {
curl_setopt_array($curly[$id], $options);
}
curl_multi_add_handle($mh, $curly[$id]);
}
// execute the handles
$running = null;
do {
curl_multi_exec($mh, $running);
} while($running > 0);
// get content and remove handles
foreach($curly as $id => $c) {
$result[$id] = curl_multi_getcontent($c);
curl_multi_remove_handle($mh, $c);
}
// all done
curl_multi_close($mh);
return $result;
}
并发送.php
error_reporting(E_ALL);
set_time_limit(0);
require_once('includes/functions.php');
//see the URL and make multi get requests
$URL = "http://site.com/?par=1";
$ie =0;
while($ie < 2)
{
$url_value[$ie] = $URL;
$ie++;
}
$r = multiRequest($url_value);
echo '<pre>';
print_r($r);
// make multi post requests based on the values
$ie = 0;
while ($ie < 2)
{
$data = array(array(),array());
$data[$ie]['url'] = 'http://site.com/index.php';
$data[$ie]['post'] = array();
$data[$ie]['post']['tempid'] = $url_value[$ie];
$ie++;
}
$r = multiRequest($data);
print_r($r);
但是由于某些原因,我得到了这个错误而不是预期的结果
Array (
[0] => 8896470
[1] => 4642075 )
Notice: Array to string conversion in C:\wamp\www\send\includes\functions.php on line 22
Array (
[0] =>
[1] =>
Done!
)
“0”字段数组不会像“1”那样返回“完成”响应。