Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

210
Visualizações
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
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda