I need to run groups of dozens of file_get_contents() from other servers (each group would be a server), so I was thinking about doing it in a parallel / non-blocking manner. At least between each group.

Can someone provide a simple code-example (non-working code will be fine), at least just the concept, so I understand how to do this with httprl?

BTW, I need to analyze the contents returned in the body of the requests, and store the status (boolean, depending on certain conditions) for each of the file_get_contents(), but in a single query for each group.

Thank you for reading!

Comments

mikeytown2’s picture

Check the readme for some examples. I can reply in more detail in about a week, on vacation

Fidelix’s picture

I did check the README, but I guess I did not understand how to make parallel requests and do stuff with the results in a non-blocking way.

Anyway, thanks for answering, Mikey.

mikeytown2’s picture

Have you coded something up using file_get_contents yet? If not, can you describe in sudo code what your hoping to do?

From what I've read this is what I'm picturing you currently have.

// Function used to see if status is TRUE or FALSE.
function get_status_of_data($data) {
  return TRUE;
}

// Array of URLs to get.
$urls = array(
  'http://www.google.com/',
  'http://www.bing.com/',
  'http://www.yahoo.com/',
  'http://www.duckduckgo.com/',
);

// Process list of URLs and set the status.
$results = array();
foreach ($urls as $url) {
  $data = file_get_contents($url);
  $results[$url] = get_status_of_data($data);
}

// Do something with results
var_dump($results);

I'll be basing this example off of the node_load one and the callback one in the readme and the above code I've done this which will do the requests in parallel.

// Function used to see if status is TRUE or FALSE.
function get_status_of_data($data) {
  return TRUE;
}

// Define callback function.
function get_status_of_data_wrapper($result) {
  return get_status_of_data($result->data);
}

// Array of URLs to get.
$urls = array(
  'http://www.google.com/',
  'http://www.bing.com/',
  'http://www.yahoo.com/',
  'http://www.duckduckgo.com/',
);
// Set URLs to be keys and the value to be '' (empty).
$results = array_fill_keys($urls, '');

// Process list of URLs and set the status.
foreach ($results as $url => &$status) {
  $options = array(
    'callback' => array(
      array(
        'function' => 'get_status_of_data_wrapper',
        'return' => &$status,
      ),
    ),
  );
  // Queue up the request.
  httprl_request($url, $options);
}
// Execute requests.
httprl_send_request();

// Do something with results
var_dump($results);

Doing a non blocking request on this is totally dependent on what you plan to do with the results. I really can't help you more until I see what you have coded up. As you can tell the differences are not that big between using file_get_contents and httprl.

mikeytown2’s picture

Status: Active » Fixed

Going to mark this issue as fixed. I think I answered the question.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.