1

EDITED

I am attempting to use curl multi to check the response from a website and additionally check each curl response for a portion of text. I have grouped the data into an array but I cannot figure out if I am using the correct/most efficient method to run the strpos() function using the 'post' text.

$data = array(array());

$data[0]['url']  = 'http://www.google.com';
$data[0]['post'] = 'google text';

$data[1]['url']  = 'http://www.yahoo.com';
$data[1]['post'] = 'yahoo text';

$r = multiRequest($data);

echo '<pre>';
print_r($r);

Here is my function:

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,      'Chrome: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.2 (KHTML, like Gecko) Chrome/22.0.1216.0 Safari/537.2');

    // 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_getinfo($c, CURLINFO_EFFECTIVE_URL);
    $result[$id][] = curl_getinfo($c, CURLINFO_HTTP_CODE);
    $result[$id][] = curl_getinfo($c, CURLINFO_CONTENT_TYPE);
    $url = curl_getinfo($c, CURLINFO_EFFECTIVE_URL);
    // loop data again
    foreach ($data as $id => $d){
        if($url==$d['url']){ // only check current url data
            $text = curl_exec($c);
        $result[$id][] = strpos($text, $d['post']); 
        }
    }
    curl_multi_remove_handle($mh, $c);
  }

  // all done
  curl_multi_close($mh);

  return $result;
}

Can anyone advise on whether my solution is appropriate? Is there a more efficient/better way to perform my strpos() check?

Thanks

user1098178
  • 697
  • 3
  • 9
  • 25

1 Answers1

0

Use an array so you can relate all the urls, strings, and curl handles together:

$stuff = array(
   0 => array('url' => 'google', 'text' => 'googletext', 'curl' => null)
   1 => array('url' => 'yahoo', 'text' => 'yahootext', 'curl' => null)
   etc..
);

foreach($stuff as $key => $info) {
   $stuff[$key]['curl'] =  curl_init($stuff[$key]['url']);
   curl_multi_add_handle($mh, $stuff[$key]['curl']);   
}

Then do a similar loop when you're processing the results.

Marc B
  • 356,200
  • 43
  • 426
  • 500
  • Can you advise on whether my solution is appropriate? Is there a more efficient/better way to perform my strpos() check? – user1098178 Jan 28 '15 at 18:31