I use api movie search to display movies from the site from here in php implementation

<?php $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "http://api.kinopoisk.cf/getFilm?filmID=714888 "); curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); curl_setopt($ch, CURLOPT_HEADER, FALSE); $response = curl_exec($ch); curl_close($ch); $querys = json_decode($response); $star=$querys->ratingData->rating; $IMDd=$querys->ratingData->ratingIMDb; 

and if on a page with one request it works quickly everything is fine, then on the categories page where there can be a lot of such requests, everything works much more slowly. Writing to the database is not an exit ratings can change very quickly (which is important for newly made films). Would you recommend how to speed up work with api?

  • Need to parallelize. PHP is not suitable for this. I would recommend js either java - rjhdby
  • @rjhdby if you could use js believe used. - Sergalas
  • curl can work "multi-threaded". That is, he can be given a dozen urls and he will load them in parallel. And if the service from the other side is ready to give it back, it will be really fast. Another thing is that the service may not like these loads. - KoVadim

1 answer 1

I can say one thing that this approach is critical to the design of the functional. You essentially rely in Live mode on a third-party resource that does not always have to be available.

In your case, I would initially think about caching the received data from the API to some temporary storage (for example, Memcached) with a period from 1 to 24 hours.

Is the difference in the rating of 24 hours or the difference in the rating of 15 minutes important to you? I think not, for some reason the performance of your data source will be hundreds of times faster than getting data from a third-party source. The minimum plus caching here, even in the fact that when the third-party API "lies", your resource will continue to work as usual. Also, the same user requests will not create an additional load on the third-party API - overloading the network with the same requests.

You can also speed up your program with the help of workers and queues, you have a queue of tasks, and the connected workers who watch the queue, as soon as you need to update the movie rating, you send the task to the queue and the workers write the value to some temporary storage, after which You show the user a rating. The advantage is that you can add as many workers as you like, depending on the required performance and they will work in parallel. This is an analogue of multithreading for the client.