3

I want to convert following code to simultaneous http request using PHP and cURL

   $select_ids = $db->query("SELECT job_id, tids FROM sent_items");    
    if ($select_ids->num_rows) {
        while($row = $select_ids->fetch_object()) {
            $records[] = $row;            
        }
        $select_ids->free();
    }

    print_r($records);

    if(!count($records)) {
        echo 'No records';
    } else {
        foreach ($records as $r) {      
            $chandle = curl_init();    
            $url = "http://api.mvaayoo.com/apidlvr/APIDlvReport?user=USER ID:PASSWORD&tid=$r->tids&jobid=$r->job_id";    
            curl_setopt($chandle, CURLOPT_URL, $url);    
            curl_setopt($chandle, CURLOPT_RETURNTRANSFER, 1);    
            $curl_result[] = curl_exec($chandle);
        }    
    }

print_r($records); returns jobid and tid

See Here

I give the job id and tid to the api url

$url = "http://api.mvaayoo.com/apidlvr/APIDlvReport?user=USER ID:PASSWORD&tid=$r->tids&jobid=$r->job_id";

tid=$r->tids&jobid=$r->job_id

$curl_result[] returns

See Here

My Problem is how to convert above code to simultaneous http request ????

The code article

Simultaneuos HTTP requests in PHP with cURL

0

3 Answers 3

1

Create array of URLs what you want to use with CURL and send them to multiRequest function. See GET example in article.

So it will end up creating CURL for each of these URLs and making requests. After requests it will return results into $multi_curl_result as array. Then you can use returned data.

<?php

// Add multiRequest function here from article

$select_ids = $db->query("SELECT job_id, tids FROM sent_items");
if ($select_ids->num_rows) {
    while($row = $select_ids->fetch_object()) {
        $records[] = $row;
    }
    $select_ids->free();
}

if(!count($records)) {
    echo 'No records';
} else {
    $curls = array();
    foreach ($records as $r) {
        $curls[] = "http://api.mvaayoo.com/apidlvr/APIDlvReport?user=USER ID:PASSWORD&tid=$r->tids&jobid=$r->job_id";
    }

    $multi_curl_result = multiRequest($curls);
}

You could add ID to CURL job. Then you can get data from results array by ID.

$curls[$r->job_id] = "http://api.mvaayoo.com/apidlvr/APIDlvReport?user=USER ID:PASSWORD&tid=$r->tids&jobid=$r->job_id";

Now you know which data belongs to which job. For example if job_id would be 10, then this job's data is in $multi_curl_result[10].

Some comments on how multiRequest function works

multiRequest function (source).

function multiRequest($data, $options = array()) {

  // array of curl handles
  $curly = array();
  // data to be returned
  $result = array();

  // multi handle
  // Allows the processing of multiple cURL handles asynchronously.
  $mh = curl_multi_init();

  // loop through $data and create curl handles
  // then add them to the multi-handle
  foreach ($data as $id => $d) {
    // Create new CURL handle and add it to array
    // $id makes it possible to detect which result belongs to which request
    $curly[$id] = curl_init();

    // If $data item is array, get URL from array, otherwise item should be URL (for GET requests)
    $url = (is_array($d) && !empty($d['url'])) ? $d['url'] : $d;

    // Set regular CURL options
    curl_setopt($curly[$id], CURLOPT_URL,            $url);
    curl_setopt($curly[$id], CURLOPT_HEADER,         0);
    curl_setopt($curly[$id], CURLOPT_RETURNTRANSFER, 1);

    // If $data item is array, then we could have POST data
    if (is_array($d)) {
      // If item has POST data
      if (!empty($d['post'])) {
        // Set POST data
        curl_setopt($curly[$id], CURLOPT_POST,       1);
        curl_setopt($curly[$id], CURLOPT_POSTFIELDS, $d['post']);
      }
    }

    // Set extra CURL options from array
    if (!empty($options)) {
      curl_setopt_array($curly[$id], $options);
    }

    // Add normal CURL handle to a CURL multi handle
    curl_multi_add_handle($mh, $curly[$id]);
  }

  // execute the handles
  // $running will have value, if CURL is still running
  // Every execute will change it, until all requests has been done
  $running = null;
  do {
    curl_multi_exec($mh, $running);
  } while($running > 0);

  // Add request responses to array
  // Recognizable by $id
  foreach($curly as $id => $c) {
    // Return the content
    $result[$id] = curl_multi_getcontent($c);

    // Removes CURL handle ($c) from multi handle ($mh)
    curl_multi_remove_handle($mh, $c);
  }

  // All done
  curl_multi_close($mh);

  return $result;
}
Sign up to request clarification or add additional context in comments.

4 Comments

your solution not working...so please explain with multiRequest() function definition
How it's not working? Any errors? I have explained how it works.
Yes. What doubts you have?
I am very new to PHP cURL can you explain multiRequest() function line by line in your answer because this is very useful for me and other viewers
0

it is simple change your foreach like:

$data = array();
foreach ($records as $r) {                       
    $data[] = "http://api.mvaayoo.com/apidlvr/APIDlvReport?user=USER ID:PASSWORD&tid=$r->tids&jobid=$r->job_id";                  
}  

$curl_result = multiRequest($data);

1 Comment

I don't know full functionality of multiRequest($data); function, so please explain with multiRequest($data) function
0

You could use PHP's curl_multi_init but one of the many available libraries might make this easier than coding it all yourself.

Check these out:

PHP-multi-curl: https://github.com/jmathai/php-multi-curl

and

Guzzle: https://github.com/guzzle/guzzle

Comments

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.