Help with CURL

I have a php script that runs from a cron job, it uses CURL to read a URL (hosted by my arduino) to determine if my garage door is open. It will return "D7 is ON" when the door is closed, and "D7 is off" when the door is open.

When I manually open the link in a browser, I get the correct status based on the door position. It seems that CURL is not refreshing or is caching the url so it always thinks the door is closed. When I first built the script, I tested opening and closing a hundred times and it always seemed to update, but all of a sudden it seemed to stop….

Any problems with this code?

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, "http://XXXXXXXXXXXXXXX/digitalRead/7"); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
$manstatus = curl_exec($ch); 
curl_close($ch);   

My test output code is:

if ($manstatus = "D7 is ON")
    {
    echo 'Man door is closed!';
    echo '
';
    }
else if ($manstatus = "D7 is off")
    {
    echo 'Man door is open!';
    echo '
';
    }

It is saying closed EVERY TIME, even when I know the URL is saying open. I cant seem to figure out why all of a sudden this changed.

This is my first time using CURL, so feel free to tell me if I am doing this all wrong!

3 Replies

You need to use double equals (==) in your if tests. Single equals is used for assignment.

@Nibbler:

You need to use double equals (==) in your if tests. Single equals is used for assignment.

Made that change and a few others from feedback received from other members on IRC.

Solved!

There's nothing wrong with using CURL, but FYI PHP has built-in functions to do this stuff without resorting to external libraries. The most simplified one would be:

$manstatus = filegetcontents("http://XXXXXXXXXXXXXXX/digitalRead/7");

Reply

Please enter an answer
Tips:

You can mention users to notify them: @username

You can use Markdown to format your question. For more examples see the Markdown Cheatsheet.

> I’m a blockquote.

I’m a blockquote.

[I'm a link] (https://www.google.com)

I'm a link

**I am bold** I am bold

*I am italicized* I am italicized

Community Code of Conduct