2

I have a server that requires me to send a curl response to get data back about a given phone number.

$numbers = array('12345','23456','345567','45678');

 foreach ($numbers as $value)
 {
  $curl = curl_init();
  curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
  curl_setopt($curl, CURLOPT_USERPWD, "user:password");
  curl_setopt($curl, CURLOPT_URL, "http://server/data/user=" . $value);

  $ret = curl_exec($curl);
  $result = json_decode($ret,true);

   echo $result['someData'] . "<br>";
  curl_close($curl);
 }

my questions are: is this efficient?

is there a better way?

how can i get the echo to print to screen after each curl result, until waiting until the end of the entire script to run?

5
  • i've had a similar issue like this before. it's down to the browser how they deal with php scripts as it is being executed and output is being generated. for example, firefox will wait for php script to execute and display the result in one go. on the other hand, i've noticed chrome is about to show the output buffer from the php, whilst the script is executing and echoing.... Commented Nov 1, 2013 at 18:00
  • How many requests will you send like this, I think it'll slow down the process, your visitor will be waiting, doesn't look efficient. Are not there any ways to get them within one request ? Commented Nov 1, 2013 at 18:02
  • this is not a page available to any users. I am crunching data for some reports i have to do. so speed isn't necessarily important. and no, the server on the other end is a black box that only takes 1 number request at a time. Commented Nov 1, 2013 at 18:04
  • You could use curl_multi_init() to run multiple requests in parallel. Commented Nov 1, 2013 at 18:06
  • possible duplicate of PHP Multiple Curl Requests Commented Nov 1, 2013 at 18:06

2 Answers 2

1

You need to flush the data now and then. Try something like this

<?php
ob_start(); //Turning ON Output Buffering
$numbers = array('12345','23456','345567','45678');

 foreach ($numbers as $value)
 {
  $curl = curl_init();
  curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
  curl_setopt($curl, CURLOPT_USERPWD, "user:password");
  curl_setopt($curl, CURLOPT_URL, "http://server/data/user=" . $value);

  $ret = curl_exec($curl);
  $result = json_decode($ret,true);

  ob_flush();//Flush the data here
  echo $result['someData'] . "<br>";
  curl_close($curl);
 }
 ob_end_flush();

Go through all those output functions here

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

Comments

0

i think if the server you're contacting only accepts a single number then it would make sense to do it this way.

if you have access to the code on the receiving end then maybe you could serialize your array and send the whole thing over in one shot.

2 Comments

the server on the other end is a black box that only takes 1 number request at a time.
then i would say looping through your array would be the way to go. if it only takes one number request at a time. there isnt anything you can do about that.

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.