Help with speeding up php code

Reply to Post #15

Wouldent your first suggestion do the same thing my code is doing now just within a div? I need the entire process to be sped up somehow.

I cant necessarily guess if they are online or offline. The list has to be comprised of only live users that are streaming. When a visitor comes to my website then they will be able to click the live stream link bringing them to a page to watch their stream.

Hi sgsecret;

I checked and found that using position:relative and position:absolute works fine using html script with images but fails miserably when including PHP script :slight_smile:

So I have been busy looking for a solution and think that PHP Curl with Multilpe Threads maybe the answer to…

http://php.net/manual/en/function.curl-multi-init.php

Check out the demo using your data (source link included at the bottom of the page):
http://johns-jokes.com/downloads/sp-a/curl-multi-exec/
The code does not work exactly just returns the source greater than about 5 bytes.
I think it is a good base and does not require a lot of work to complete.

Upon reflection that the guy that wrote the API would have expected a search for users of a particular game and supplied a link including the game that returns all the players online. Maybe worth checking?

I cant view the source, 400 error.

Whoops, sorry about that - try it now.

Provlem due to: The link used to work when I was testing on a subdomain and used /index.php/src=true. When I moved the source to the downloads folder I forgot to make the link relative.

If it still does not work after refreshing the page then try this link:

http://johns-jokes.com/downloads/sp-a/curl-multi-exec/index.php?source=true

@sgsecret;

I cant view the source, 400 error.

Here we go:


<?php /* */ ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"  lang="en" xml:lang="en">
<head>
<?php include '_head.php';?>
</head>
<body>
  <hr />
    <h1>
      <a href='http://www.sitepoint.com/forums/showthread.php?890672-Help-with-speeding-up-php-code&p=5201789#post5201789'>
        SitePoint Forum:
      </a>
    </h1>
  <hr />

  <div id='main'>
    <?php
      echo "<h2 class='flr'>start: " .date('H:i:s') ,'</h2>';
      $nodes = array
      (
          "meditor996", "malidude", "tacsecret", "izual155", "porkyhd",
          "tachydro#", "church_", "tainttubes", "kurimlinn", "TacvFOX",
          "tacpinkiepie", "dkrstarcraft", "chaosstarcraft", "thepandrexlive",
          "zren89", "1soulfire", "sallvain", "sg_goldster", "adramelechii",
          "dragon", "dignitasselect", "sing_sing", "desrowfighting", "eghuk",
          "eg_idra", "rootcatz", "liquidsheth", "spanishiwa", "mtwdimaga",
          "whitera", "meditor996", "colqxc", "kraglord", "tsm_dyrus", "nyjacky",
          "crs_saintvicious", "tsm_theoddone", "mstephano", "protech", "tacmoose"
      );
      $node_count = count($nodes);

      $curl_arr = array();
      $master   = curl_multi_init();

      // initialise each and every node
      $uuu =  "http://api.justin.tv/api/stream/list.json?channel=";
      for($i2 = 0; $i2 < $node_count; $i2++)
      {
        # echo '<br />',
        $url = $uuu ."{" .$nodes[$i2] ."}";
        $curl_arr[$i2] = curl_init($url);
        curl_setopt($curl_arr[$i2], CURLOPT_RETURNTRANSFER, true);
        curl_multi_add_handle($master, $curl_arr[$i2]);
      }

      // Curl_Multi_exec - go and do your stuff
      do
      {
        curl_multi_exec($master,$running);
      } while($running > 0);

      //
      echo '<dl>';
        for($i2 = 0; $i2 < $node_count; $i2++)
        {
          $results = curl_multi_getcontent  ( $curl_arr[$i2]  );
          $len = strlen( $results );

          if($len > 2)
          {
             echo '<dt>';
               fn_json_decode($results, $nodes[$i2] );
             echo '</dt>';
             echo '<dd>';
               echo htmlspecialchars($results);
             echo '</dd>';
          }
        }
      echo '</dl>';
      echo "<h3 class='flr'>finish: " .date('H:i:s') .'</h3>';


  //===============================
  function fn_json_decode( $results, $NAME)
  {
      $json_file  =  $results; #@file_get_contents($FFF, 0, null, null);
      $json_array = json_decode($json_file, true);
      if ( isset($json_array[0]['name']) && $json_array[0]['name'] == "live_user_{$NAME}")
        $live = true;
      else
        $live = false;

      # Prevent warnings and errors - Not known
      $viewers      = ' $viewers ';
      $channelTitle = ' $channelTitle ';
      $gameSTR      = ' $gameSTR ';
      $i            =  999;
      $url          = "http://www.twitch.tv/$i";

      echo "<a
              href='" .$url ."'
              target='_blank'
              title='" .$channelTitle ."'
              style='color:#00f'
            >"
              .$NAME
           .'</a>';

      #echo "<font style=tahoma color=white size=2>" ."<br>" ."&nbsp;&nbsp; &nbsp;"
      #     .   $viewers . " " . $gameSTR . " " . $userLINK . $i . "</a>"
      #     ."</font>";
  }
  ?>
  </div>

  <div id='src'>
    <a href='index.php?source=true'>
      Source
    </a>
    <br />
    <?php
      if(isset( $_GET['source']))
      {
        highlight_file( __FILE__);
      }
    ?>

  </div>

</body>
</html>

For some reason my provider has disabled line 46 “curl_multi_exec($master,$running);” for security reasons. =(

Okay, now login via FTP or SSH and check the timestamp of your channels.txt file to verify it is indeed being updated every two minutes, like you asked it to be. Once that is verified, let me know. As logically that should be the only reason why you are not seeing it updated.

Ur right. I have confirmed that it is actually not executing the cron job. “php5 /home/sglivest/public_html/livestreams.php” does not work. I will continue to try some different variations.

I worked with @sgsecret ; via PM as we needed to exchange confidential information, but now his cronjob is running smoothly using the command php ~/public_html/sglivest/Livestreams.php

Just wanted to give everyone an update.

Yes. It is working properly. And I just wanted to thank everyone who helped me out here. I know I was a pain in the ass but I am a very happy man now that everything works.

If you are interested heres my main website where my new live stream list is broadcasted
http://www.sentinelgaming.net - Its on the right hand side of the home page.

THANKS!

I am pleased that you managed to sort your problem and while looking for a solution I learnt a lot about Curl which I am sure will be useful for a future project.

I am curious to know why the script I supplied is timing out. This is unfortunate because it is appears to be a very fast solution.

www.johns-jokes.com/downloads/sp-a/curl-multi-exec/index.php



{"url":"/api/stream/list.json?channel={meditor996}","error":"rate limit exceeded for 173.199.151.52"}


Nice site by the way, quite colourful :slight_smile:

I don’t think your code was “timing out” just that his hosting provider disabled or does not allow curl_multi_exec. I agree, it is a snazzy solution (I like it), seems the host didn’t want to support multiple connections simultaneously however (I could see where a shared host may do this for a variety of reasons).

Yes I like the solution and was impressed when tried with different URLs.

I also liked the script because it is small, flexible and fast.

I will have to run some more tests to create multiple links to another server and see if I can find a way round the problem.