With Client-ID, API calls take 3-5 seconds to load on localhost

In my stream overlay I connect to the Twitch API and pull some channel information. Now as I’m trying to rewrite this to accommodate for the upcoming Client-ID requirement, I’ve ran into a really frustrating problem. With this Client-ID check it takes the overlay 3-5 seconds to load the information between scene switches in OBS. How can I eliminate this and have it load instantly like before?

I had some help to even get the API information to display in the first place while running it on localhost with this new Client-ID check, to do that we had to add the following two lines of code. I suspect this bit might be the culprit, but have no idea how to fix it.

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);

And here is the full code for my Client-ID check:

function getFromTwitchAPI($endpoint) {
     $channelsApi = 'https://api.twitch.tv/kraken/';
     $clientId = ' '; // Client-ID key removed before posting here
     $ch = curl_init();

     curl_setopt_array($ch, array(
        CURLOPT_HTTPHEADER => array(
           'Client-ID: ' . $clientId
        ),
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_URL => $channelsApi . $endpoint
     ));

     // Without this I can't pull anything while on localhost
     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
     curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
     // -----------------------------------------------------

     $response = curl_exec($ch);
     curl_close($ch);

     $data = json_decode($response, true);

     return $data;
}

Example information being pulled from the API:

// Game being played on Twitch
function twitchGame() {
  $data = getFromTwitchAPI("channels/" . TWITCHCHANNEL);

  if ($data)
    echo $data['game'];
  else
    echo __FUNCTION__ . "is unable to connect to the Twitch API.";
}

Thank you for any kind of help.

Update:
I’m starting to think this might have to do with how AMPPS is configured when it comes to curl on my computer instead of it being something wrong with the code. Not that would make me any smarter as I have no idea how I would fix that either hehe.

I assume the SSL Certificate isn’t getting verified since you had to disable verification to make it work. api.twitch.tv uses a DigiCert SHA2 Secure Server CA certificate which is from the DigiCert Global Root CA.

You can grab the cacert.pem from this site - curl - Extract CA Certs from Mozilla

and switch your 2 lines to this:

curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, TRUE);
curl_setopt ($ch, CURLOPT_CAINFO, “pathto/cacert.pem”);

See if that works for you.

Information taken from: php - HTTPS and SSL3_GET_SERVER_CERTIFICATE:certificate verify failed, CA is OK - Stack Overflow

While that would fix having to disable ssl verification, that shouldn’t change speed in any way. (Also, please don’t use ssl3, it’s dead and buried.)

@Korlian: Are you sure you’ve initially loaded the site? It might be that your app has some startup code related to the localhost webserver you’re using, which is the delay you’re seeing before receiving the response.

You might try running a local server like apache2 or wamp, which will create a local web server that your computer will keep up and running between requests, and use better memory pooling. (Both of those apps support php, javascript, etc by default)

You might check your server config for how long it keeps the pool initialized between requests, if it’s just you using it locally it’s safe to set that high so it never releases the connection.

@Korlian, I don’t have to do that on my localhost and can call the API no problem. I’m curious as to why you have to do this. What kind of error do you get? I’m using MAMP, so my configuration may be a little different. We haven’t had any complaints about a slowdown when passing in the Client-ID. It may be a configuration issue for sure.

Thank you. This didn’t seem to make any difference, assuming I did it correct.

I run AMPPS which is a similar local web server to WAMP, MAMP etc. I am suspecting that it might be the culprit, because if I upload my overlay to my online web server, the code loads the data instantly as you would expect it to.

I’m actually not getting any error (All error reports are turned on as far as I know in AMPPS), I simply seem to get no data, it’s all blank.

Gotcha. I’ve never had to turn on those two SSL options for localhost, which is why I asked the question. :slight_smile: The code comment implied that it wouldn’t work without them, so I was hoping it might be spitting out an error! Alas. :slight_smile:

The code is essentially what would change the red ssl lock on a web browser green. If you’re ignoring ssl cert validity it’s technically less safe for you’re users because the chain of verification is broken.

But for a local host project that’s presumably not production environment it’s not an issue.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.