2011-01-23 71 views
1

我需要對特定的網頁發出多個GET請求,該網頁會生成一個隨機數,然後用該特定數字發出多個POST請求。到目前爲止,我有這樣的代碼: 的functions.phpphp多捲曲多請求,多發帖請求

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

什麼是你的問題? – 2011-01-23 16:28:58

回答

0

您正在將重置爲批處理文章數組。

更換

$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 
     ) 
    ); 
}