PHP file_get_contents not working from CRON - php

I'm using PHP with a CRON job to get the contents of a URL using:
$content = file_get_contents("http://www.example.com");
I then use this content to send as an HTML newsletter. I'm running PHP 7, and I have enabled the relevant PHP INI settings:
allow_url_fopen = On
However, the $content variable is still turning up empty and my error_log is showing the following error:
PHP Warning: file_get_contents(http://www.example.com):
failed to open stream: no suitable wrapper could be found
in /home/xxxx/public_html/cron.php on line xxx
Everything else in the cron.php is working as expected.
Additionally if I manually visit the cron.php via a browser, file_get_contents is working perfectly.
Running WHM/cPanel etc.
Any help is greatly appreciated.

Get path to php.ini in you script:
php_ini_loaded_file();
set path to php.ini:
/opt/php/7.3/bin/php -c /var/www/u1234567/php-bin/php.ini -f /var/www/u1234567/public_html/site.com/script.php

Related

file_get_contents and curl not working via php but working on CLI

When i use file_get_contents with a url (http://example.com or other), it will return false with different context, user_agent, etc:
file_get_contents(http://example.com):failed to open stream: operation
failed
However when we used file_get_contents() for a local file, it works. But it shows the above error when we try to access any file through http.
I tried the same using cURL and got the following error:
cURL Error (7): couldn't connect to host
According to some solutions on stackoverflow, we changed this setting in php.ini file:
ini_set('allow_url_fopen', 1);
I have also used snoopy library after including snoopy class.php
require "Snoopy/Snoopy.class.php";
$snoopy = new $Snoopy;
fetchtext("http://example.com");
$text = $snoopy->results;
it is also giving an empty response.
If I cURL or wget the same URL using terminal, it works perfectly. But it doesn't work in the PHP code when the file is not local (through HTTP).
Server details:
Debian GNU/Linux 7.6 (wheezy)
Apache/2.2.22 (Debian)
PHP 5.4.45-0+deb7u7
In Debian, there will be a group aid_inet. Apache should be a member of that group to allow to access network.
Run the following command to get the apache user. In my machine its www-data.
cat /etc/passwd
Try this command to add apache user in aid_inet group.
usermod -a -G aid_inet www-data
[Note: if you have different apache user then use your own]
Now, run your php code again to test. It should work.
There is an error in your code, which could be the issue:
fetchtext("http://"google.com");
should be:
fetchtext("https://www.google.com");
(Google uses HTTPS, and always redirects to www subdomain).
Have you checked that the URL you're attempting to query is valid (i.e. structure, escaping of special characters)? It would really help if you can give a real URL you're trying to call.
Regarding from where you are running your php files ( apache or command line ), check that you have allow_url_fopen = On on both php.ini files :
/etc/php5/cli/php.ini
/etc/php5/apache2/php.ini

popen() in php works from command line but not from browser

I have a simple php script which parses the user input from html form. Since Python does string processing so well, I actually wrote the parser in python. So I forward the user entered string to python script for processing using
$query = "avocados"; // from HTML form
$handle = popen("./NLParser.py $query","r");
$read = fread($handle,2048);
pclose($handle);
when I execute the php from command line interpreter, I get the desired output. But when I visit the same php file from a browser, I get empty string from fread(). I tried checking if the $handle is FALSE and I also read from stderr, no problems there. Please help me figure out this inconsisteny.
I tried running
phpinfo();
The php.ini file used by command line php interpreter was
Loaded Configuration File => /etc/php5/cli/php.ini
and the same entry on web was
Loaded Configuration File /etc/php5/apache2/php.ini
I am using Ubuntu 14.04.1 LTS with PHP Version 5.5.9-1ubuntu4.4. All the files have permission set to 755
Try changing the owner and group of NLParser.py to the ones of your server.

PHP Not Executing on Command Line

Hello I have php working fine with my apache server. However, when executing the php script from the command line (like /usr/bin/php file.php) it just echos the sourcecode of my script back at me, even when executing a PHP in the web directory. Anybody know how to fix this?
Command line scripts STILL need to have at least <?php to switch over to PHP mode. Remember that there's really no such thing as a PHP script. There's only files which have a <?php ... ?> code blocks within them.
So, something like
#!/usr/bin/php
echo 'hello world!';
Would actually output
echo 'hello world!';
because There's no actual PHP code in there. There's just some text that LOOKS like php code. You need to have
#!/usr/bin/php
<?php
echo 'hello world!';
and then you'll get the expected
hello world!
as output. Without <?php the php interpreter will never "switch" over to actual PHP mode, and just treat all of the text in the file as plain output.
In your past questions, you seem to prefer using short open tags (<?), so that is probably the issue. Try running this instead:
/usr/bin/php -d short_open_tag=yes file.php
If this works, then your current config has short_open_tag disabled. You'll have to edit your CLI config file to change this. You can find the location of the config file using this:
/usr/bin/php -i | grep php.ini
You should see:
Configuration File (php.ini) Path => /etc/php5/cli
Loaded Configuration File => /etc/php5/cli/php.ini
Edit the file listed by "Loaded Configuration File". The line with:
short_open_tag = Off
Should be set to On.
I have been fighting this issue for over 4 months and finding nothing to fix it.
My problems started when I installed a new 3T drive and then moved all my projects off the default 1.5T drive. Could never get anything working right in "localhost" since. Also cannot get aliases working in the 2 virtual hosts I created, but their index.html files now show fine at:
projects.com
tbnk-svr.com
But none of the following work:
localhost/phpmyadmin,
localhost/seopanel,
localhost/test.php,
localhost/info.php,
php on the command line.
I wrote this up at:
https://www.linuxquestions.org/questions/newreply.php?do=newreply&noquote=1&p=6214546
and since I get no errors in any of the error logs, can not figure out what is denying localhost and the cmd line from working. Test of:
php <<< "<?php echo 1;"
php -v
Both work, so not sure why it is only echoing the file contents as "cat file" would do.
Has to do somehow with it not rendering "localhost"
Cheers!
TBNK

php script works in browser but trows fatal error at command line

I've got a basic script that I want to run from a cronjob:
require("/opt/lampp/htdocs/cronjobs/aws-sdk-for-php/sdk.class.php");
$amazonSes = new AmazonSES(array( "key" => $AWS_KEY, "secret" => $AWS_SECRET_KEY, "certificate_authority" => $CA ));
$response = $amazonSes->send_raw_email(array(
'Data'=> base64_encode($message)),
array('Source'=>$src, 'Destinations'=> $dest));
The above works perfectly when I load the .php from the browser.
But from the command line or conjob it returns this fatal error:
Fatal error: Call to undefined function curl_init() in /opt/lampp/htdocs/cronjobs/aws-sdk-for-php/lib/requestcore/requestcore.class.php on line 615
Any ideas?
Your webserver (apache presumably) is either using a different copy of PHP than the one you use when calling it from the commandline (called CLI), or it is using a different configuration file.
Regardless, you can use phpinfo() and call that on your browser to see the path to your PHP copy which is being used. On the commandline you can call php -ini and look for the path to your PHP copy.
The error you get is because your CLI version of PHP is configured to not incorporate the curl library. Therefore use the commandline to get the loaded configuration file (INI) and use a text editor to edit that file and enable curl.
Your CLI (command line interface) PHP is using a different PHP.INI. When calling PHP from command line, use arguments to make PHP use the same PHP.INI. (see php --help to see the available arguments.)
Or ... configure the alternative PHP.INI that is used by the CLI PHP. (Try opening a php file from CLI that only contains the following code: <?php phpinfo(); ?>. It will tell you which PHP.INI is exactly being used.

cURL not working in Windows Task Scheduler

I've a PHP script that uses cURL to perform certain tasks. At the moment, I have the script running every 10 minutes. This is what I'm running via Windows Task Scheduler.
C:\wamp\bin\php\php5.4.3\php.exe -f C:\wamp\www\autoscripts\index.php
However, for some reason, whenever the argument quoted above is run through the command line, I get the error "Fatal error: Call to undefined function curl_init()". The script works perfectly when I access it via the browser. Is there any reason why PHP isn't able to access the cURL extension via the command line?
Most likely running from command line does not use any ini file that loads the extensions. Open phpinfo() from the browser, copy path to loaded ini file and change your task to:
C:\wamp\bin\php\php5.4.3\php.exe -c "C:\path\to\php.ini" -f C:\wamp\www\autoscripts\index.php
Figured it out. Basically, on WampServer, there are TWO php.ini files that you need to be aware of.
C:\wamp\bin\php\php5.4.3\php.ini
C:\wamp\bin\apache\apache2.2.22\bin\php.ini
Forgot that the command line uses a different ini file than the web server. :(

Categories