Slow response PHP API

Asked

Viewed 192 times

3

I’m doing a project with band profiles. I’m trying to use the last.fm API..

For test basis I downloaded the PHP API: https://github.com/fxb/php-last.fm-api

The classes I left in Root/src/classes.php, and started using Artist::search($artistName,$limit);

Only that I want to display only the registered bands in the bank. The Script actually works, but the website response takes 7 seconds to return. I’m no "expert," so if anyone has an idea how to optimize this script,.

            <div class="grid-cont">
            <div class="banda_search">
                <!-- hot news -->

                <form action="#">
                        <i class="fa fa-search"></i>
                        <input type="text" placeholder="Pesquise pelo nome da banda que você procura!">

                    </form>
                        </div>

                <?php

                    $getPage = (!empty($Link->getLocal()[1]) ? $Link->getLocal()[1] : 0);
                    $Pager = new Pager(HOME . '/bandas/');
                    $Pager->ExePager($getPage, 12);

                    $readbanda = new Read;
                    $readbanda->ExeRead("ws_bands", "WHERE band_parent IS NOT NULL ORDER BY band_date DESC LIMIT :limit OFFSET :offset", "limit={$Pager->getLimit()}&offset={$Pager->getOffset()}");


                      if (!$readbanda->getResult()):
                          $Pager->ReturnPage();
                          WSErro("Desculpe, não existem bandas cadastradas no momento, favor volte mais tarde!", WS_INFOR);
                      else:

                            echo'<div class="banda_news">';
                            echo'<div class="grid-row">';
                            foreach ($readbanda->getResult() as $banda):
                            extract($banda);

                               $results = Artist::search($band_title,"1");

                                   foreach($results as $artist):



                                        echo'   <div class="grid-col grid-col-3">
                                        <a href="'.HOME.'/banda/'.$band_name.'" class="small" alt="'.$band_title.'" title="'.$band_title.'">
                                        <span class="pic" alt="'.$band_title.'" title="'.$band_title.'" style="background-image: url(' . $artist->getImage(4) . ')"></span>

                                        <h3>'.$band_title.'</h3>
                                        </a>
                                        </div>';




                                  endforeach;
                            endforeach;

                            echo '</div>
                                  </div>';
                      endif;
                    $Pager->ExePaginator("ws_bands", "ORDER BY band_date");
                    echo $Pager->getPaginator();?>


                </div>                              

If anyone has a better idea to grab images of artists by API and make it quick,.

  • (I don’t understand this class so forgive me if I’m talking nonsense) For every loop that accesses the Artist::search($band_title, "1") a request is made to the server http://a9.com/-/spec/opensearch/1.1/, If the connection was persistent (I don’t think it is) it might not take that long. Or maybe you should call it another way, but unfortunately I have no necessary knowledge about this class.

1 answer

1

To optimize the application group all band names into a string and use the "IN" query in the WHERE conditional of your database query. This will cause you to make only 1 query in the bank, avoiding a few seconds of waiting between request and response.

Construct set values for IN():

$bands = $readbanda->getResult();
$bands_sql = '';
for($i = 0; $i < count($bands); $i++){
    $bands_sql .= '\''.$bands[$i]['band_title'].'\'';
    if($i+1<count($bands)){
    $bands_sql.= ',';
    }
}

This piece of code must generate a string with the name of all bands, now you must create a function to query your database, it must be something like this:

SELECT * FROM bands WHERE band_title IN ($bands_sql);

The return of this query should be exactly what is being done today, but with time optimizations. I hope to have helped.

Browser other questions tagged

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