Search code examples
phparrayscurlcurl-multi

php multi curl multi get request , multi post request


I need to make multiple GET requests to a specific web page which generates a random number and then make multiple POST requests with that specific numbers . So far I have this code : 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;
}

and 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);

However for some reasons I'm getting this error instead of the expecting result

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!

)

The "0" field array doesn't return "done" response like "1".


Solution

  • you are resetting you batch post array with $data = array(array(),array());.

    Replace

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

    By

    $data = array();
    foreach ($url_value as $ie => $value) {
        $data[$ie] = array(
            'url' => 'http://site.com/index.php',
            'post' => array(
                'tempid' => $value
            )
        );
    }