Why does it run the API call twice when there is a sleep in the code??? (PHP/Laravel)
Get the solution ↓↓↓public function createall()
{
$users = User::orderBy('id', 'ASC')
->where('server', 'Demo')
->where('enable', '1')
->where('mt4_account', '!=', 0)
->take(4)
->get();
foreach ($users as $user) {
$server = Server::where('Serverdesc', $user->server)->first();
$post = [
'server' => $server->serverdesc,
'api_key' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
'login' => intval($user->mt4_account),
];
$datapost = json_encode($post);
// initialize cURL
$ch = curl_init();
// set options
curl_setopt($ch, CURLOPT_URL, "http://xxxxxxxxxxxxxxxxx/closetrades");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $datapost);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($datapost))
);
$result = curl_exec($ch);
curl_close($ch);
sleep(5);
$ptr = fsockopen($server->serveraddress, $server->serverport);
// Checking errors
if (!$ptr) {
dump("Connection error");
exit;
}
$useracc = $user->mt4_account;
$userpass = $user->mt4_pass;
$request = "WUSERINFO-login=" . $useracc . "|password=" . $userpass . "\nQUIT\n";
fputs($ptr, $request);
while (!feof($ptr)) {
// Reading the string of symbols
$line = fgets($ptr, 128);
// End of result transmission flag
if ($line == "end\r\n") {
fclose($ptr);
break;
}
// Processing
$data = (explode(chr(9), $line));
if (substr($data[0], 0, 4) == "Bala") {
$balance = substr($data[0], 9, strlen($data[0]) - 11);
dump($data, $useracc, $userpass, $balance);
}
}
}
}
With the sleep function in the code, the API call above it, and apparently only the API call, gets executed twice for every record. I see the API write out the results on the terminal window of the server, so I'm absolutely sure it gets executed double.
I remove the sleep function and the API call gets executed once for every record. The terminal window on the server shows the right amount of calls...
The rest of the code, for example the ones that dumps the variables, gets printed only once, so apparently that part gets executed only once...
Why? How? I'm dumbfounded...
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: too few arguments to function laravel
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.