Help Curl+ Sleep

Asked

Viewed 274 times

1

There is the possibility to make a script I have in CURL wait 50 seconds to run?

I tried to stop him over Sleep for the whole run, I need to pause only the Curl in question

function writeMessage() {
$fields = array('site' => $site, 'uid' => $uid, 'ip' => $ip, 'id' => $id);
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, 'https://xxxx.xx/widget-ajax.php');
  curl_setopt($ch, CURLOPT_POST, true);
  curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
  $result = curl_exec($ch);
  curl_close($ch);
    }

I can make him wait 50 seconds before executing without locking the rest of the code?

I also tried by ajax on jsonp but it is complicated performs the cross-Domain POST browser security prevents..

2 answers

0

Assuming you already know the ajax request flow (because you have tried jsonp), you can make a "proxy" system where you would make the ajax request to a specific file on your server and that file would make the request via Curl or file_get_contents to the desired site to circumvent CORS. the flow would be more or less like this.

  1. User enters the abc page
  2. The page is redeveloped in the browser
  3. After x seconds, a js on the abc page does ajax to the xpto.php file on the same server.
  4. xpto.php file makes Curl, prepares the data to return to abc
  5. abc page receives the data and makes business logic

In practice the implemented flow would be more or less like this:

Página abc (only the ajax part with jQuery)

jQuery(document).ready(function ($) {
    setTimeout(function () {
        $.get('xpto.php', function (dados) {
            alert('Dados recebidos, veja o console para mais detalhes');
            console.log(dados);
            // fazer alguma coisa coms os dados
        });
    }, 50000); // 50 segundos
});

Filename foo.php

<?php
$fields = array('site' => $site, 'uid' => $uid, 'ip' => $ip, 'id' => $id);
$ch  = curl_init();

curl_setopt($ch, CURLOPT_URL, 'https://xxxx.xx/widget-ajax.php');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);

// Talvez você precise desativar a verificação de peers localmente
//curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);

$result = curl_exec($ch);
curl_close($ch);

// To Do não esquecer de tratar a respota antes de devolvê-la
echo json_encode(['data' => $result]);
exit;

If you need to always be doing the checking from time to time just change the function of setTimeout for setInterval

If you prefer a PHP only side approach you try one of the solutions proposed in this article (in English)

  • This does not work, it paralyzes the whole page script!

  • @Viníciusremonti I edited my answer with an example as an alternative to jsonp that avoids the CORS problem

0

There is but it is not very common to do this in php. As the code runs asynchronous, you will have to make a process Fork and in it you will wait 50s while in the main process will be executed the rest of its logic.

$pid = pcntl_fork();
if ($pid == -1) {
     die('erro');
} else if ($pid) {
    // continua o resto do processo aqui;
    pcntl_wait($status);
} else {
    sleep(50);
    writeMessage();
}
// Qualquer código que tiver aqui vai ser executado pelos dois processos
// então vai passar duas vezes por aqui, por isso deixar o resto da lógica dentro
// do if pra ser rodado somente no processo pai.

I recommend to see better about Fork here. Also if you are using php 7.2+ you can use a new standard library for parallelism.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.