我需要对生成随机数的特定网页发出多个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;
}
和send.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”。
答案 0 :(得分:0)
您正在使用$data = array(array(),array());
重置批量发布数组。
替换
$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++;
}
通过
$data = array();
foreach ($url_value as $ie => $value) {
$data[$ie] = array(
'url' => 'http://site.com/index.php',
'post' => array(
'tempid' => $value
)
);
}