3

I was wondering if it's possible to open multiple URLs with cURL or maybe something else.

I tried this until now.

$urls = array(
          "http://google.com",
          "http://youtube.com",
          );

foreach($urls as $url) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT,0);
    curl_setopt($ch, CURLOPT_TIMEOUT_MS, 200);
    curl_exec($ch);
    curl_close($ch);
}

The 200ms are there to let the site open fully. Maybe you know any alternatives.

Is it possible to open multiple URLs in PHP at the same time? Not client sided, server side.

1
  • 1
    I think curl_multi does what you need. Instead of curl_exec you should add the handles to a curl_multi handle and then do a curl_mutli_exec Commented Jul 18, 2017 at 16:22

1 Answer 1

3

Your solution would be simultaneous cURL HTTP requests.
For faster implementation, you can use this function (thanks to phpied):

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

    // 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 use it like this:

$data = array(
  'http://search.yahooapis.com/VideoSearchService/V1/videoSearch?appid=YahooDemo&query=Pearl+Jam&output=json',
  'http://search.yahooapis.com/ImageSearchService/V1/imageSearch?appid=YahooDemo&query=Pearl+Jam&output=json',
  'http://search.yahooapis.com/AudioSearchService/V1/artistSearch?appid=YahooDemo&artist=Pearl+Jam&output=json'
);
$r = multiRequest($data);

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

Hope it helps.
Also read this.

Sign up to request clarification or add additional context in comments.

4 Comments

But I still have a little question. It doesn't have to do with the other question above. Is it possible to ignore the reponse time of a websites? Like after five seconds it just goes on and doesn't wait for the response of the website.
Yep, you still able to use CURLOPT_TIMEOUT_MS. Just apply it after curl_setopt($curly[$id], CURLOPT_RETURNTRANSFER, 1); in that function.
Thank you very much. Everything is working how I wanted to! I'll give you some credits :) With best regards
Happy to help ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.