Control return order of cURL

-1

I'm doing a proxys checker by putting a list of proxys in textarea and the code returns me on the 1 by 1 screen below the other if they work or do not. You are almost all set, the only problem is that no matter what order the proxy list is, proxys are always displayed before the "#LIVE "; if I have a list with 5 proxys that work and 5 that do not work no matter the order they are in textarea , always the "#DIE" will be displayed first. I want results to be displayed in the order that proxys was placed in textarea , but how do I do this?

<html>
<head>
<title> chk </title>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script><script>functionenviar(){varbin=$("#bin_id").val();
        var linhaenviar = bin.split("\n");
        var index = 0;
        linhaenviar.forEach(function(value){

          setTimeout(

            function(){
              $.ajax({
                url: 'chkp.php',
                type: 'POST',
                dataType: 'html',
                data: "bin=" + value,
                success: function(resultado){
                $('#oi').html($('#oi').html() + resultado + "<br>");
              }
            })

          }, 10 * index);

        index = index + 1;

        })
      }
  </script>

</head>
<body>
<center>
<textarea name="bin" placeholder="PROXY|PORTA" id="bin_id" rows="10" cols="40">
</textarea>
<br>
<input type="button" value="testar" onclick="enviar();"></input>
<br>
<div id="oi" name="oi">
<p></p>
</div>
</center>
</body>
</html>

PHP file:

<?php

error_reporting(0);

if(!empty($_POST["proxy"])){
$proxylist = substr($_POST['proxy'], 0, 90);
$proxy = explode("|", $proxy)[0];
$port = explode("|", $proxy)[1];
explode("|", $proxy)[2];



 $ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "host.com/prpxytest");

curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt'); 
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 10);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_POSTFIELDS, '_proxy=' . urlencode($proxy) . '&_port=' . urlencode($port));

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

$dados = curl_exec($ch);

$q = explode('</i>', $dados);

$sal = $q[4];

$msg = ' ';

if(strpos($sal, "Sair") !== false){
    $msg = "<b><font color='green'>#LIVE $proxy | $port </font></b>";
}else{
    $msg = "<b><font color='red'>#DIE $proxy | $port </font></b>";
}

echo $msg;


}else{ echo 'erro'; }
?>
    
asked by anonymous 10.07.2017 / 18:20

1 answer

1

For what you have explained, what you intend to do is multiple asynchronous requests sequentially. Particularly I see no reason to do this, but one solution is to implement a recursive function that is always called in the success event of the previous request. For example, let's say we will make the request for the following URLs:

const URLs = [
  'https://jsonplaceholder.typicode.com/posts',
  'https://jsonplaceholder.typicode.com/comments',
  'https://jsonplaceholder.typicode.com/albums',
  'https://jsonplaceholder.typicode.com/photos',
  'https://jsonplaceholder.typicode.com/todos',
  'https://jsonplaceholder.typicode.com/users'
];

We then create a function that receives a list of URLs as input and makes requests sequentially:

function asyncSequentialRequests(urls) {

  if (urls.length > 0) {

    let url = urls.shift();

    $.ajax({
      'url': url,
      'method': 'get',
      'dataType': 'json',
      'success': data => {
        console.log('A url ${url} retornou ${data.length} resultados.');
        asyncSequentialRequests(urls);
      }
    });

  }

}

In this way, when you want to start the requests, just invoke the function:

asyncSequentialRequests(URLs);

The result will be:

"A url https://jsonplaceholder.typicode.com/posts retornou 100 resultados."
"A url https://jsonplaceholder.typicode.com/comments retornou 500 resultados."
"A url https://jsonplaceholder.typicode.com/albums retornou 100 resultados."
"A url https://jsonplaceholder.typicode.com/photos retornou 5000 resultados."
"A url https://jsonplaceholder.typicode.com/todos retornou 200 resultados."
"A url https://jsonplaceholder.typicode.com/users retornou 10 resultados."

That matches the values reported on the site , in the exact order that the URLs were placed. For your case, simply generate the list of URLs dynamically, as it already does and adapt the function success , remembering to do the recursion calling the function itself in success . To add an element at the end of another, you can use append of jQuery.

const URLs = [
  'https://jsonplaceholder.typicode.com/posts',
  'https://jsonplaceholder.typicode.com/comments',
  'https://jsonplaceholder.typicode.com/albums',
  'https://jsonplaceholder.typicode.com/photos',
  'https://jsonplaceholder.typicode.com/todos',
  'https://jsonplaceholder.typicode.com/users'
];

function asyncSequentialRequests(urls) {
  
  if (urls.length > 0) {
    
    let url = urls.shift();
    
    $.ajax({
      'url': url,
      'method': 'get',
      'dataType': 'json',
      'success': data => {
        console.log('A url ${url} retornou ${data.length} resultados.');
        asyncSequentialRequests(urls);
      }
    });
    
  }
  
}

asyncSequentialRequests(URLs);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    
11.07.2017 / 00:58