每个cURL请求后更新页面

时间:2013-11-15 18:09:38

标签: php curl

我有以下代码:

$fiz = $_GET['file'];
    $file = file_get_contents($fiz);
    $trim = trim($file);
    $tw = explode("\n", $trim);
    $browser = 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1468.0 Safari/537.36';
    foreach($tw as $twi){
        $url = 'https://twitter.com/users/username_available?username='.$twi;
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_USERAGENT, '$browser');
        curl_setopt($ch, CURLOPT_TIMEOUT, 8);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        $result = curl_exec($ch);
        $json = json_decode($result, true);
        if($json['valid'] == 1){
        echo "Twitter ".$twi." is available! <br />";
        $fh = fopen('available.txt', 'a') or die("can't open file");
        fwrite($fh, $twi."\n");
    } else {
        echo "Twitter ".$twi." is taken! <br />";
    }
}

它的作用是使列表看起来像:

苹果

等等,它会用Twitter检查它以检查名称是否被采用。 我想知道的是,如果以任何方式可以在每次登记后出现请求而不是一次性显示所有请求?

1 个答案:

答案 0 :(得分:0)

您需要使用Ajax调用,如果您熟悉JavaScript或Jquery,则可以轻松完成此操作。

不要一次检查所有名称,而是使用Ajax函数一次向服务器端PHP代码发送一个名称。

首先发送“Cat”,处理页面并使用Ajax返回结果。现在,您可以在页面上显示结果。

发送“狗”得到回复---&gt;显示它等等。

此处已回答类似的问题Return AJAX result for each cURL Request

希望这有帮助,我在这里使用Jquery ......

<强>的JavaScript

<script>

 var keyArray = ('cat','dog','mouse','rat');

 function checkUserName(name, keyArray, position){
  $("#result").load("namecheck.php", {uesername: name, function(){ // Results are displayed on 'result' element
    fetchNext(keyArray, position);
  });}
 }

 function fetchNext(keyArray, position){
   position++; // get next name in the array
   if(position < keyArray.lenght){ // not exceeding the aray count
     checkUserName(keyArray[position], keyArray, position) // make ajax call to check user name 
   }
 }

 function startProcess(){
  var keyArray = ('cat','dog','mouse','rat');
  var position = 0; // get the first element from the array
  fetchNext(keyArray, position); 
 }

</script>

<强> HTML

<div id="result"></div>

<button onclick="startProcess()"> Start Process </button> 

<强> PHP

<?
        $twi = $_GET['username'];

        $browser = 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1468.0 Safari/537.36';
        $url = 'https://twitter.com/users/username_available?username='.$twi;
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_USERAGENT, '$browser');
        curl_setopt($ch, CURLOPT_TIMEOUT, 8);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        $result = curl_exec($ch);
        $json = json_decode($result, true);

        if($json['valid'] == 1){
          echo "Twitter ".$twi." is available! <br />";
          $fh = fopen('available.txt', 'a') or die("can't open file");
          fwrite($fh, $twi."\n");
        }else{
          echo "Twitter ".$twi." is taken! <br />";
        } ?>