1

I am making a web application that does API calls frequently. All API calls are just simple GET request, however I want to speed up loading time and output return time up as much as possible. As of right now, I'm using cURL to do the API calls by using the following:

<?php
function api_call($params)
  {
  $base = 'https://api.example.com/Api?';
  $url = $base . http_build_query( $params );
  $ch = curl_init($url);
  curl_setopt($ch, CURLOPT_HEADER, 0);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  $output = curl_exec($ch);
  return json_decode($output,true);
  }
?>

Is there any ways that I can optimize this for faster download and/or output time?

Jon Winstanley
  • 23,010
  • 22
  • 73
  • 116
lolmoney
  • 21
  • 1
  • 1
  • 2

7 Answers7

10

Is it possible to use IP address instead of the hostname api.example.com? If yes, you can speed up the namelookup_delay (a couple of hundred milliseconds in my case)

Keep-alive doesn't help in your case because keep-alives don't pool connections between requests. It is useful in the classic webbrowser-webserver scenario.

mixdev
  • 2,724
  • 2
  • 30
  • 25
6

Is there any way you can use caching if data is sometimes the same between many API calls? It's more of a connection speed issue than a code issue.

MattB
  • 714
  • 1
  • 8
  • 18
4

Not really. The speed of the code can't really be optimized very much there. The bottleneck is going to be the connection between your server and their server. There is not very much you can do to speed that up in the code.

simshaun
  • 21,263
  • 1
  • 57
  • 73
3

One more which you can do is Enable encoding also as it makes less data to be transferred.

curl_setopt($ch, CURLOPT_ENCODING, '');//set gzip, deflate or keep empty for server to detect and set supported encoding.

If you enable encoding then data would be compressed before it is sent. It might take some time to do it but very less data get transferred if you are working with large data.

Arjun J Gowda
  • 720
  • 10
  • 21
0

Optimized:

<?php
    function api_call($params)
    {
        $url='https://api.example.com/Api?'.http_build_query($params);
        return json_decode(file_get_contents($url),true);
    }
?>

You can also:

  1. Remove the $url variable and paste the string in file_get_contents().
  2. If $params is not changed, then you can also remove http_build_query(); and save its result to the variable by one time.
spenibus
  • 4,339
  • 11
  • 26
  • 35
KsaR
  • 581
  • 6
  • 17
0

The one thing you could maybe do is look at using keepalive connections if the requests are to the same server.

Tyler Eaves
  • 12,879
  • 1
  • 32
  • 39
  • Yes, the calls are to the same server, just different parameters with and different returns in the output. Would keep-alive be good for that? If so, how do I keep the connection between the API server and mine alive for longer with cURL. – lolmoney Dec 06 '10 at 19:11
  • 4
    -1 php curl automatically uses keepalive. http://stackoverflow.com/questions/972925/persistent-keepalive-http-with-the-php-curl-library – Byron Whitlock Dec 06 '10 at 19:14
  • @Whitlock seriously do you get a kick out of downvoting people? I really don't think this is such a bad answer. Maybe he did not know better. I think Tyler is trying to help lolmoney? – Alfred Dec 06 '10 at 21:38
  • 1
    @Alfred, while in general keepalives are a good thing, in this instance the answer is wrong. The OP is specifically asking about cURL. Guesses don't make good answers. – Byron Whitlock Dec 07 '10 at 20:19
-1

you can use multi threading for launching more copies of your script. it can be faster perform your requests

RusAlex
  • 8,245
  • 6
  • 36
  • 44