Stream_get_contents() not working with cron job

Hello,

Recently I’ve add a “Crob Job” using “Cron Jobs” tool in “CPanel”.
The Cron Job executes command below once per minute (* * * * *):
php /home/melkobi/public_html/index.php

In the mentioned “index.php” source code, I’ve used the query below to insert a new row to the table1:
$query = “INSERT INTO table1 (temp) VALUES (’$temp’)”;

Good part: Whenever I visit my website using browser (Firefox) and refresh the tab, a new row including temperature ('C) will be added to table (showing everything including query and connection works perfectly).

The problem is whenever I close the browser and rely on Cron Job itself to execute “index.php”, wrong value “false” will be added to table instead of temperature (showing the query will be send, but “false” will be added to table instead of temperature).

---------- Questions:
1 - Is the problem with “Cron Job” or with the “query”?
2 - Is the problem related to sql comand? I’ve used commands below:

        // Creating connection:
        $conn = mysqli_connect('myServerName', 'myUserName', 'myPassword', 'myDBname);
        // Checking connection:
        if (!$conn) {
            die("Connection failed: " . mysqli_connect_error());
        }

        // Add temperature as a new row:
        $query = "INSERT INTO table1 (temp) VALUES ('$temp')";

        // Checking for error:
        if (mysqli_multi_query($conn, $query)) {
            echo "New records created successfully";
        } else {
            echo "Error: " . $query . "<br>" . mysqli_error($conn);
        }

        // Closing the connection:
        mysqli_close($conn);

3- Is the problem related to PHP version? (My virtual web host PHP version: v7.2)
4- Is the problem with the string “$query”? (Did I concatenate string and variable perfectly?)

I’ve googled all day long and nothing practical found. I’ll be appreciated if any suggestion.

Regards

If it inserts data into the database then clearly the query somewhat works. Where does $temp come from?

$temp is acquired from commands below:

        $handle = fopen($domain, 'r');
        $source = stream_get_contents($handle);
        fclose($handle);

        $keyword = "temprature = "; 

        //$position = strpos($source,$keyword);
        $position = stripos($source,$keyword);
        
        //$temp = substr($source,$position+strlen($keyword),10);
        $temp = mb_substr($source,$position+strlen($keyword),10);

1- Can the problem be with the output of stream_get_contents()?
2- Do I have to use html_entity_decode() to make sure the output of stream_get_content() is necessarily “string”?
the most important question:
3- I can’t understand the behavior of strpos() and stripos(). When I refresh the tab of the browser, functions find the position of $keyword in $source perfectly! But, when I close the browser (alternatively: when the codes run once per minute using cron job), strpos() and stripos() return “faslse”!
I’m really confused…

Thanks for your reply, JimL

And what is $domain?

Dear astonecipher, unfortunately, I don’t have rights to tell.
Why could it be depended on $domain?

stream_get_contents() returns “false” while executes using cron job. otherwise it works fine.

Because $domain is a file, correct? Cron jobs need an absolute path, not a relative path, to function correctly. So, if you are passing in,

…/files/my_file.txt and the path is actually /home/user/files/my_file.txt it won’t work.

The other side of it is, this is a CLI program when running from a job. So, if $domain is for a url and you don’t have those options allowed in the ini file the CLI version uses, it would result in an error, that you are not watching for or logging.

1 Like

Hello,

I’ll be appreciated if anyone tell me why:
stream_get_contents() returns “false” while is executed using Cron Job.
otherwise it works fine.

Thank you for your information. Below, I reply each part of your comment:

“Because $domain is a file, correct?”

“…/files/my_file.txt and the path is actually /home/user/files/my_file.txt it won’t work.”

  • I’ve add a “Crob Job” using “Cron Jobs” tool in “CPanel” with this path:
    php /home/melkobi/public_html/index.php
    Sorry, I’m not sure if I catch you, you mean the path above is correct?

“The other side of it is, this is a CLI program…”

  • Yes, the program checks $domain once per minute, grab it’s html sources code as string variable $source, Search in $source for $keyword, and finally select the part of string including temprature information and save it as $temp.

So, its kind of data logging and you may right. Would you please tell more about “CLI program” and “ini file CLI version uses”?

Sponsor our Newsletter | Privacy Policy | Terms of Service