Call REST API - Redux

To beat this topic to death…

I’ve built a simple html website that does four things;

Turn on a spark core red LED
Turn off a spark core red LED
Turn on a spark core blue LED
Turn off a spark core blue LED

On the server side (Apache) I’m calling one of four php scripts (see below) that, using curl, posts to the spark api. These php scripts, when executed server side turn the leds on and off and the response header appears almost instantly.
These scripts are simply called by an HTML “form action” button.

What is driving me crazy is that when the form action button is clicked it’s taking 16 seconds to respond. I’m all over the place on trying to solve this.

No, I’m not a developer but I play on on TV :smile:

Any help would be appreciated!

<?php $ch = curl_init(); curl_setopt($ch, CURLOPT_URL,""); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, "access_token=my_access_token_here&params=l0,HIGH"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_exec ($ch); curl_close ($ch); ?>

So if you send direct curl you get an instant response, but using the php it takes 16 seconds?

What is the server running on? Laptop, pc?

It would be useful to test if a direct curl call or (REST call) immediately changes the LED. It should. If so, then you know you’ve got a PHP issue as opposed to a core issue.

Dave O

As dorth said. If the curl goes through instantly. You could find that where your running your server, is running the php slow. It could be many different things. I will gladly help you write your code for this so you dont need 4 php pages as you only need 1 :slight_smile:

Drop me a pm, and I will show you how to do it


I am running curl from within a php script. Running the php scripts on the server is nearly instant.

Simply running curl outside of the script produces the same desired results as running within the php script. I get the same desired response time running curl

The server is one of mine running CentOS 5.3 and is in a datacenter where I lease space and it’s running fine. I have several other php, perl and tomcat apps running nicely.

The path I’ve been staggering down for the last two days has taken me everywhere but close to the answer.


Right guys, i’ve just sent RickSanson a basic php script for what he wants to do. if it works does anyone else want the link to the code posting?

Thanks for the php code,

Can anyone tell me how I can get a reading with php code? for example the reading of the temperature example. I want it to display it in an ios app.

if I can get the temperature reading as and display it as a nsinteger that would be great.

I just uploaded my file into my server and ran the php file through ios and the lights turned on almost instantly. I am using 1and1 for my server.

Thanks for the help guys.

@bibomain you can do it in PHP but it’s just going to read it once and display it… statically.

It’s easy to do in jQuery and you can set the variables to refresh how ever often you want. Check out my example here: