php multi curl multi get request,multi post request
我需要向特定网页发出多个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:wampwwwsendincludesfunctions.php on line 22
Array (
[0] =>
[1] =>
Done!
)
“0”字段数组不返回“完成”响应,如“1”。
没有找到相关结果
已邀请:
1 个回复
悲帽慑彤电
重置批量发布数组。 更换
通过