1

It's my first time developing an API, which is why i'm not surpirsed it was running a little slow, taking 2-4 seconds to load (I used a microtime timer on my webpage). But then I found out how long it took for the API commands to execute, they're around 0.002 seconds. So why when I use CURL in PHP, does it take another 2 seconds to load?

My API Connection Code:

function APIPost($DataToSend){
    $APILink = curl_init();
    curl_setopt($APILink,CURLOPT_URL, "http://api.subjectplanner.co.uk");
    curl_setopt($APILink,CURLOPT_POST, 4);
    curl_setopt($APILink,CURLOPT_POSTFIELDS, $DataToSend);
    curl_setopt($APILink, CURLOPT_HEADER, 0);
    curl_setopt($APILink, CURLOPT_RETURNTRANSFER, 1);
    return curl_exec($APILink);
    curl_close($APILink);
}

How I retrieve data in my web page:

$APIData=array(
    'com'=>'todayslessons',
    'json'=>'true',
    'sid'=>$_COOKIE['SID']
);
$APIResult = json_decode(APIPost($APIData), true);
if($APIResult['functionerror']==0){
    $Lessons['Error']=false;
    $Lessons['Data']=json_decode($APIResult['data'], true);
}else{
    $Lessons['Error']=true;
    $Lessons['ErrorDetails']="An error has occured.";
}

The APIPost function is within a functions.php file, which is included at the begging of my page. The time it took from the begging of the second snippet of code, to the end is about 2.0126 seconds. What is the best way to fetch my API data?

Blease
  • 1,380
  • 4
  • 38
  • 64

1 Answers1

0

This is just a guess, so please dont beat me up about it. But maybe its waiting for curl to complete i.e. timeout as you dont close curl before doing the return.

Try this tiny amendment see if it helps:

function APIPost($DataToSend){
    $APILink = curl_init();
    curl_setopt($APILink,CURLOPT_URL, "http://api.subjectplanner.co.uk");
    curl_setopt($APILink,CURLOPT_POST, 4);
    curl_setopt($APILink,CURLOPT_POSTFIELDS, $DataToSend);
    curl_setopt($APILink, CURLOPT_HEADER, 0);
    curl_setopt($APILink, CURLOPT_RETURNTRANSFER, 1);
    $ret curl_exec($APILink);
    curl_close($APILink);
    return $ret;
}
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
  • I can't say that's made much of a change unfortunately RiggsFolly, any other ideas? I will still use your code though as I guess it is better practice. – Blease Mar 19 '14 at 16:38
  • Like I said it was only a guess. Can you identify where abouts the time is being consumed? i.e. is it just a slow server that you are querying? In which case there is little you can do. – RiggsFolly Mar 19 '14 at 17:54
  • I am querying my own server, but it is a subdomain. The API takes a few milliseconds to load the data as you can see here `http://api.subjectplanner.co.uk/?com=login&username=123&password=456`. The executed element shows how long it takes for that data to be loaded which is pretty quick (<0.5s). So I guess its the CURL function in my question that takes time. I measured the time from when the function is called, to when it returns a value. – Blease Mar 19 '14 at 18:12
  • This mught be useful for you to pinpoint where any issues are http://stackoverflow.com/questions/3757071/php-debugging-curl – RiggsFolly Mar 19 '14 at 19:13