Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

209
Views
Why does it run the API call twice when there is a sleep in the code??? (PHP/Laravel)
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...

over 4 years ago · Santiago Trujillo
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!