Control return order of Curl

Asked

Viewed 369 times

-1

I’m doing a check on proxys putting a list of proxys in the textarea and the code returns to me on screen 1 by 1 below each other whether they work or not. Almost everything is ready, the only problem is that it does not matter the order in which the list of proxy be, always the proxys "#DIE" is shown before "#LIVE"; if I have a list of 5 proxys that work and 5 that don’t work no matter the order in which they are in the textarea, always the "#DIE" will be displayed first. I want the results to be displayed in the order in which the proxys were placed in the textarea, but how do I do it ?

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

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
  <script>
      function enviar(){
        var bin = $("#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'; }
?>
  • 1

    For each proxy in the textarea the script shown is executed?

  • 1

    How do you perform this script?

  • Yes that each line has a proxy and the port separated by |

  • The script is executed by an ajax jquery that does all the line requests

  • 1

    So your problem is not in PHP, but in jQuery. What happens is that the tests that fail probably generate the results before those that do not fail, thus displaying them first. Since no asynchronous requests, this order will never be guaranteed.

  • I edited the question with the part that makes the requests

  • 1

    One solution would be to create a recursive function that makes asynchronous requests sequentially, but frankly I see no reason to do so. What is the real need to obtain the result in such an order?

  • To organize the information, tbm would wish if possible that within different iframes the positive and negative results and that the lines of both were counted and displayed on the screen, example: (90 proxys vivo) 2 iframe (100 proxys deceased)

  • Why did you edit the question this way?

  • Hello can help me with this return error ?

  • If it is a new question a little life from the above question should create a new question.

  • And why two iframes? Wouldn’t two elements be enough on the page itself? What you want to do doesn’t seem to make much sense.

  • I want two lists one with the list of dead proxys another with the living that has the sidebar to scroll down

  • Two small frames with different results

Show 9 more comments

1 answer

1

From what you have explained, what you intend to do are several 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 event success of the previous request. For example, 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 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);
      }
    });

  }

}

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 informed on the website, in the exact order the Urls were placed. For your case, just generate the list of Urls dynamically, as you already do and adapt the function success, remembering to make the recursive calling the function itself in success. To add one element at the end of another, you can use the append 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>

Browser other questions tagged

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