Php multiple curl urls with while loop

Asked
Active3 hr before
Viewed126 times

7 Answers

multiple
90%

yeah all URLs are constant, but each are different URLs. How can I stop processing curl say if 3rd url gives me result. I have to process a while loop and am struck at that. – Arun Code Apr 4 '16 at 15:25 , @Mike yeah it is necessary to stop the process if the result is found. Because once I got the result there is no point of processing remaining URLs because all the remaining will be giving me no result. No there is no need of any specific order for processing. Altering the order is totally fine. – Arun Code Apr 4 '16 at 15:38 ,I want to stop processing curl at a point when one of the URL is giving me a information. Note: I have multiple curl requests.,Also I have a condition where none of the URL produce any result and it will be great if someone shows me how to handle that also.

Try the following:

< ? php

function callCURL($url) {
   $ch = curl_init();
   curl_setopt($ch, CURLOPT_URL, $url);
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
   $combined = curl_exec($ch);
   curl_close($ch);
   return $combined;
}

function getResult($urls) {
   $return = array();

   foreach($urls as $url) {
      $response = callCURL($url);
      if (strlen($response) !== 0) {
         $return[] = $response;
         break;
      }
   }
   return $return;
}

$urls = array("example.com/value1.php?process=$param", "example.com/value2.php?process=$param", "example.com/value3.php?process=$param")

$result = getResult($urls);
load more v
88%

I am developing a small script and I am bit messed up with using a couple of curl and while loop.,I want to stop processing curl at a point when one of the URL is giving me a information. Note: I have multiple curl requests.,I get the output of the page when running,As an example of where it's clearer to use break than a flag, consider:

My sample code.

< ? php
///functions here
do {

   $ch = curl_init();
   curl_setopt($ch, CURLOPT_URL, "example[dot]com/value1.php?process=$param");
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
   $combined = curl_exec($ch);
   curl_close($ch);

   $ch = curl_init();
   curl_setopt($ch, CURLOPT_URL, "example[dot]com/value2.php?process=$param");
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
   $combined = curl_exec($ch);
   curl_close($ch);

   $ch = curl_init();
   curl_setopt($ch, CURLOPT_URL, "example[dot]com/value3.php?process=$param");
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
   $combined = curl_exec($ch);
   curl_close($ch);

   $ch = curl_init();
   curl_setopt($ch, CURLOPT_URL, "example[dot]com/value4.php?process=$param");
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
   $combined = curl_exec($ch);
   curl_close($ch);

   $ch = curl_init();
   curl_setopt($ch, CURLOPT_URL, "example[dot]com/value5.php?process=$param");
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
   $combined = curl_exec($ch);
   curl_close($ch);

   $ch = curl_init();
   curl_setopt($ch, CURLOPT_URL, "example[dot]com/value6.php?process=$param");
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
   $combined = curl_exec($ch);
   curl_close($ch);

   $ch = curl_init();
   curl_setopt($ch, CURLOPT_URL, "example[dot]com/value7.php?process=$param");
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
   $combined = curl_exec($ch);
   curl_close($ch);

   $ch = curl_init();
   curl_setopt($ch, CURLOPT_URL, "example[dot]com/value8.php?process=$param");
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
   $combined = curl_exec($ch);
   curl_close($ch);

   $ch = curl_init();
   curl_setopt($ch, CURLOPT_URL, "example[dot]com/value9.php?process=$param");
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
   $combined = curl_exec($ch);
   curl_close($ch);

   $ch = curl_init();
   curl_setopt($ch, CURLOPT_URL, "example[dot]com/value10.php?process=$param");
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
   $combined = curl_exec($ch);
   curl_close($ch);

} while (strlen($combined) != 0);
echo $combied;

///functions here

?
>
load more v
72%

I want to stop processing curl at a point when one of the URL is giving me a information. Note: I have multiple curl requests.,Also I have a condition where none of the URL produce any result and it will be great if someone shows me how to handle that also.,I am developing a small script and I am bit messed up with using a couple of curl and while loop.,To see all the responses you can foreach $combined.

My sample code.

< ? php
///functions here
do {

   $ch = curl_init();
   curl_setopt($ch, CURLOPT_URL, "example[dot]com/value1.php?process=$param");
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
   $combined = curl_exec($ch);
   curl_close($ch);

   $ch = curl_init();
   curl_setopt($ch, CURLOPT_URL, "example[dot]com/value2.php?process=$param");
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
   $combined = curl_exec($ch);
   curl_close($ch);

   $ch = curl_init();
   curl_setopt($ch, CURLOPT_URL, "example[dot]com/value3.php?process=$param");
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
   $combined = curl_exec($ch);
   curl_close($ch);

   $ch = curl_init();
   curl_setopt($ch, CURLOPT_URL, "example[dot]com/value4.php?process=$param");
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
   $combined = curl_exec($ch);
   curl_close($ch);

   $ch = curl_init();
   curl_setopt($ch, CURLOPT_URL, "example[dot]com/value5.php?process=$param");
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
   $combined = curl_exec($ch);
   curl_close($ch);

   $ch = curl_init();
   curl_setopt($ch, CURLOPT_URL, "example[dot]com/value6.php?process=$param");
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
   $combined = curl_exec($ch);
   curl_close($ch);

   $ch = curl_init();
   curl_setopt($ch, CURLOPT_URL, "example[dot]com/value7.php?process=$param");
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
   $combined = curl_exec($ch);
   curl_close($ch);

   $ch = curl_init();
   curl_setopt($ch, CURLOPT_URL, "example[dot]com/value8.php?process=$param");
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
   $combined = curl_exec($ch);
   curl_close($ch);

   $ch = curl_init();
   curl_setopt($ch, CURLOPT_URL, "example[dot]com/value9.php?process=$param");
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
   $combined = curl_exec($ch);
   curl_close($ch);

   $ch = curl_init();
   curl_setopt($ch, CURLOPT_URL, "example[dot]com/value10.php?process=$param");
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
   $combined = curl_exec($ch);
   curl_close($ch);

} while (strlen($combined) != 0);
echo $combied;

///functions here

?
>
load more v
65%

Why we are discussing this(multiple cURL requests/curl_multi_exec).,Now, if we use cURL then we need to iterate loop for each id and request server therefore if there are 10 ids then on an average response time will be 10X of a single request.,But if use multiple cURL requests then response time reduces significantly.,Result for each cURL request is in $result array some what like this:

Now, if we use cURL then we need to iterate loop for each id and request server therefore if there are 10 ids then on an average response time will be 10X of a single request.

/**
 * Webkul Software.
 *
 * @category Webkul
 * @author Webkul
 * @copyright Copyright (c) 2010-2016 Webkul Software Private Limited (https://webkul.com)
 * @license https://store.webkul.com/license.html
 */

$result = array();
foreach($ids as $id) {
   // URL from which data will be fetched
   $fetchURL = 'https://webkul.com&customerId='.$id;
   $ch = curl_init();
   curl_setopt($ch, CURLOPT_URL, $fetchURL);
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
   $result[] = curl_exec($ch);
   curl_close($ch);
}
load more v
75%

Allows the processing of multiple cURL handles asynchronously. , This example will create two cURL handles, add them to a multi handle, and process them asynchronously. ,curl_multi_close() - Close a set of cURL handles, Returns a cURL multi handle on success, false on failure.

load more v
40%

Using multi_curl to make multiple POST requests , Using multi_curl to make multiple POST requests ,We use the curl_multi_init and add each handle to it., Sending multi-dimensional data and multiple files with CurlFile in one request

In this example, we are using 2 different endpoints:

//array of data to POST
$request_contents = array();
//array of URLs
$urls = array();
//array of cURL handles
$chs = array();

//first POST content
$request_contents[] = [
   'a' => 'apple',
   'b' => 'banana'
];
//second POST content
$request_contents[] = [
   'a' => 'fish',
   'b' => 'shrimp'
];
//set the urls
$urls[] = 'http://www.example.com';
$urls[] = 'http://www.example2.com';

//create the array of cURL handles and add to a multi_curl
$mh = curl_multi_init();
foreach($urls as $key => $url) {
   $chs[$key] = curl_init($url);
   curl_setopt($chs[$key], CURLOPT_RETURNTRANSFER, true);
   curl_setopt($chs[$key], CURLOPT_POST, true);
   curl_setopt($chs[$key], CURLOPT_POSTFIELDS, $request_contents[$key]);

   curl_multi_add_handle($mh, $chs[$key]);
}
load more v
22%

The cURL stands for ‘Client for URLs’, originally with URL spelled in uppercase to make it obvious that it deals with URLs. It is pronounced as ‘see URL’. The cURL project has two products libcurl and curl.  ,curl_exec($ch) function should be called after initialize a cURL session and all the options for the session are set. Its purpose is simply to execute the predefined CURL session (given by ch).,Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.,The curl_init() function will initialize a new session and return a cURL handle.

Other "multiple-undefined" queries related to "Php multiple curl urls with while loop"