0

I just finished this code for getting some informations about a game.

<?php 
$apik = 'API_KEY';
$versionurl = 'https://ddragon.leagueoflegends.com/realms/na.json';



function request($url) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
    $response = curl_exec($ch);
    curl_close($ch);

    $decode = json_decode($response);

    return $decode;
}



function getChamps(){

    $array = getChampsId();

    $champsn = count($array['champs']);

    $arrayb = array();
    $i = 0;

    do {
        $arrayb[$i] = $array['champs'][$i]->id;
        $i = $i+1;
    } while ($i<=$champsn-1);

    $i = 0;

    do {
        echo getChampsNames($arrayb[$i]);
        $i = $i+1;
    } while ($i<=$champsn-1);
}

function getChampsId() {
    global $apik;
    $url = 'https://euw.api.pvp.net/api/lol/euw/v1.2/champion?freeToPlay=true&api_key='.$apik;

    $req = request($url);

    $array = array();
    $array['champs'] = $req->champions;

    return $array;
}

function getChampsNames($id) {
    global $apik;
    $ver = getVers('champion');

    $url = 'https://global.api.pvp.net/api/lol/static-data/euw/v1.2/champion/'.$id.'?locale=en_US&version='.$ver.'&champData=all&api_key='.$apik;

    $req = request($url);

    $array = array();
    $array = $req->name;

    return $array;
}

function getVers($obj){
    $url = 'https://ddragon.leagueoflegends.com/realms/na.json';

    $req = request($url);

    $array = array();
    $array = $req->n->$obj;

    return $array;
}



?>

And to call:

    getChamps();

The problem is that the website take like 30/40 (?) second to load.. why? I think there are too many curls? But I don't know any other method to do this, you have got some solution?

demetriomontalto
  • 111
  • 1
  • 1
  • 11
  • In PHP, cURL blocks, so it has to wait for each cURL to finish before continuing on to the next. This is, perhaps, a short coming of PHP. If this script is being requested by a browser, you might be better off writing it on the client side and firing all requests simultaneously. – DanielM Apr 15 '15 at 12:02
  • With AJAX in JavaScript... I'm not sure I understand the question? – DanielM Apr 15 '15 at 12:06
  • I tried with AJAX but I can't handle requests easily as php.. – demetriomontalto Apr 15 '15 at 12:08
  • In what way can't you handle requests as easily? It's almost what JavaScript was designed for. – DanielM Apr 15 '15 at 12:09
  • I mean, as you get the datas you have to send them to a element, I can't define a var and call it externally inside HTML. I don't know if I explained well. – demetriomontalto Apr 15 '15 at 12:14
  • Ah I see. Have a look at some JavaScript frameworks. I personally use [AngularJS](https://angularjs.org/) but it's a bit of a beast. A lot of people (including me) start out with [jQuery](https://jquery.com/) which is also huge, but is easier to start with. An up and coming framework I'm hearing a lot of good stuff about is [ReactJS](https://facebook.github.io/react/index.html). Looks like a nice middle ground. All of these libraries will help you with AJAX and DOM manipulation. – DanielM Apr 15 '15 at 12:18
  • I will give it a look, I'm a newbie about JS and its frameworks. Thank you – demetriomontalto Apr 15 '15 at 12:21

2 Answers2

1

An important question to ask yourself is "How often is the source material changed?" Based on this being stats for a game tournament, my bet is it doesn't change every time you load the data.

CURL and fsockopen (and everything similar) are all blocking requests. That means PHP will wait until they are done before moving on. Worse, this is network communication so if the other end is slow, it could mean your page times out waiting. Since your source likely doesn't require a fresh load every time, I would suggest you move this into a cron job and parse the results into a database or flat file. Then do the local page loads from that source. That would remove any chance of a blocking issue causing problems because if the cron fails it will simply run again later.

Community
  • 1
  • 1
Machavity
  • 30,841
  • 27
  • 92
  • 100
1

In PHP, cURL blocks, so it has to wait for each cURL to finish before continuing on to the next. This is, perhaps, a short coming of PHP. If this script is being requested by a browser, you might be better off writing it on the client side and firing all requests simultaneously.

Have a look at some JavaScript frameworks such as:

  • jQuery - It's showing it's age now but this is pretty much the grand daddy of JS Frameworks and a great place to start when learning.
  • AngularJS - This is a full on MVVM application builder. It's modern, popular, and robust, using many of the latest techniques. If you haven't done MVVM (or even MVC) this could be a big paradigm shift but worth learning about.
  • ReactJS - This is Facebook's up and coming framework. As with Angular, it's built on an ideal of programming which might be difficult for new comers, but again it's well worth learning about.

With all of these the basic methodology is to create a callback function or promise and give it to the thing handling your AJAX request. Once the request comes back, the function is called (or the promise is for filled). You can use this to change the state of your page.

You can fire multiple requests at the same time so don't forget your might callbacks might not happen in the order the requests were made. :)

DanielM
  • 6,380
  • 2
  • 38
  • 57
  • Thank you again, you got some private contact where I can message you? – demetriomontalto Apr 15 '15 at 12:39
  • You can follow me on Twitter [@Gisleburt](https://twitter.com/Gisleburt) but it's mostly just me ranting about stuff. – DanielM Apr 15 '15 at 13:42
  • That's what I meant before http://jsfiddle.net/1x5swcx8/ Code is ok, and if I insert Api key it works properly but I'm limited because I can only send the result to a specific object (body in this case) at the moment of the request, am I bad? – demetriomontalto Apr 15 '15 at 14:50
  • When do you want to send it to the DOM? JavaScript is heavily based on callbacks, so it's quite different from older, more traditional languages, and takes a bit of getting used to. You end up queuing up little packets of information and instructions and you don't really know when they will happen (this is why Promises are so much better than basic callbacks). Anyway, you can just prepare and store the information for later use instead. – DanielM Apr 15 '15 at 14:58
  • I can't test it properly without the key, but does this help: http://jsfiddle.net/1x5swcx8/2/ – DanielM Apr 15 '15 at 15:02
  • One more question, why it says that 'info' doesn't exists if I remove the button and just try to show up info['0'].id ? – demetriomontalto Apr 15 '15 at 15:25
  • Sorry, I can't visualise it, too little sleep. :) Can you send me the fiddle? – DanielM Apr 15 '15 at 15:31
  • Ah, again, this is because the callback code doesn't happen when you expect it to. This is a bit of a brain teaser at first (just wait until you get into callback scope issues :p), but it's a good thing really. At the point you're looking at info on line 3, it doesn't yet exist. :) We can start a chat on this if you like. – DanielM Apr 15 '15 at 15:35
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/75305/discussion-between-demetriomontalto-and-danielm). – demetriomontalto Apr 15 '15 at 15:39