PHP using CURL inside while loop

Dear Experts,
I connect a server using CURL to send SMS messages.

First, I select the data from my database and send it to the selected numbers.
Then I put it inside a while loop to send it to every number using CURL.

I use below code,

but I wonder which part should I use inside the while loop?
Do I have to initialize CURL for every time?
Which part should I use inside the while loop?

if ($result->num_rows > 0) {

while($row = $result->fetch_assoc()) {
        	
$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => "xxxxxxx",
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => "",
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 30,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => "POST",
  CURLOPT_POSTFIELDS => "{\r\n\t\"xxxxx\"\r\n\t}\r\n}",
  CURLOPT_HTTPHEADER => array(
    "accept: application/json",
    "authorization: ".$kullanici_sifre."",
    "content-type: application/json"
  ),
));

$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

if ($err) {
  echo "cURL Error #:" . $err;
} else {
  echo $response;
}

}
}

Open in new window

LVL 1
BRMarketingAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Chris StanyonWebDevCommented:
Nothing in your while() loop actually uses the $row from the loop, so why would you want anything to do with cURL inside it.

If you do want to set various options based on your loop, then you should be able to init the cURL request before the loop and then just set the specific options inside:

$curl = curl_init();

curl_setopt_array($curl, array(
    ...
    ...
    ...
));

while($row = $result->fetch_assoc()) {

    curl_setopt ( $curl, SomeOption, "someValue" );
    $response = curl_exec($curl);
    $err = curl_error($curl);

}

curl_close($curl);

Open in new window

1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
BRMarketingAuthor Commented:
thank you
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.