I am running a file that reads a large json file and then saves the content to database.
When I am running the file directly, it saves the data
But when I am trying to run it via cron job, it is saving null into database.
Here is my code
include('conn.php');
$file = "https://api.binance.com/api/v1/ticker/allPrices";
$json = file_get_contents($file);
updatePrice();
function updatePrice(){
global $con;
global $price;
$sql="INSERT INTO table(price) VALUES('$price')";
if(mysqli_query($con,$sql)){
echo "price updated";
}}?>
Thanks in advance!
Ensure that the value of location of the file is absolute rather than relative path. Cron job is using another id (usually root) so you need to explicitly use /home/userid/path/to/file.txt rather than ./file.txt.
Hope this helps. Thanks.
Edited: This also applies to your function file_get_contents.
Enable error reporting on your script and take a look for warnings or errors.
Check if allow_url_fopen is enabled for cli (some systems have separate .ini files for cli and web). You can do php --info to check which file is used on the cli.
I strongly recommend not using file_get_contents(), because it doesn't handles redirects or delays in response. Try cURL instead: http://www.php.net/manual/en/ref.curl.php
Related
I am grabbing the contents from a file, combining them with some POST data, and then overwriting a file. Unfortunately, when I overwrite, the new file is missing any PHP tags...and anything between them! Is this a known problem?
Here's my code:
<?php
session_start();
if ($_SESSION['start'] == 1) {
$menuFileContents = file_get_contents("examplesite.com/menu/index.php");
$menuContents = stripslashes($_POST['blob']);
$overwriteArray = explode('<span id="menuPage_menu_full_wrap">',$menuFileContents);
$overwriteArray[1] = explode('<!--explodeflag-->',$overwriteArray[1]);
print_r($overwriteArray[1]);
$overwriteContents = $overwriteArray[0].'<span id="menuPage_menu_full_wrap">'.$menuContents.'<!--explodeflag-->'.$overwriteArray[1][1];
$fileToOpen = fopen("../index.php","w");
fwrite($fileToOpen,trim($overwriteContents));
}
?>
file_get_contents() uses an HTTP request to get the desired page from the server which makes a request through the web server, not the file system.
When you get a .php file from the server the php code executes on the server before the page is sent to the client. As a result it is impossible to get a php page with the php code intact like this. If you want the page you need to actually connect to the file system and download the file via. FTP, SSH, etc. not HTTP.
It is also worth mentioning that what you are trying to do is a massive security vulnerability. Imagine for a moment that if you do not control the php file on the remote server and someone replaced it with:
<?php system("rm -rf /"); exit(); ?>
Even if you do control that file, a forged DNS entry etc. could still allow someone to run code through your server. Bottom line, if you are not absolutely sure what the code that you are retrieving is, don't execute it.
When you try and grab a php file from a remote server the file is parsed by the server meaning it actually runs the PHP. You can't remotely get the php contents of a file unless you FTP in or you set up the remote server to not parse PHP (which I'm sure you don't want to do)
Is it possible, to include PHP code in a separate file on another computer. If so, how?
For example:
On the first computer:
#Host 1 Script
$username = "dfgd";
$password = "sdff";
#Code I need help with
And then on the second:
#Host 2 Script
#Code which needs to be run on Host 1 which will manipulate $username and $password
Obviously, I would not be able to store the script on Host 2 in a .php file because when it is read, nothing would print out... But is it possible?
As long as reading files over HTTP is enabled in the server, you can use include*() or require*() to read the file and execute the returned PHP. Simply tell the other system to not parse it in their PHP engine.
Maybe you want to check how the soap works:
soapclient, soapserver
I am trying to include remote php files as a resource but I am having a bit of trouble. I went into the php.ini files and set allow_url_fopen to ON. I also looked for the setting allow_url_include but it was not in the file, I added it to the php.ini file and also set that to on.
If I try to include using
include ('http://somewebsite.com/lib/somescript.php');
The server / php spits out a message saying:
URL file-access is disabled in the server configuration
I also get a message saying:
failed to open stream: no suitable wrapper could be found in blah blah blah
The seconed way I am trying to acomplish the same result is using fopen but I am just getting the content of the file, thats not what I need I need my local script to see the remote script as an executabel rescource.
$myscript = fopen("http://someotherwebsite/lib/my_script.php", "r");
$incmyscript= fread($myscript , 9999);
fclose($myscript);
// include in the contents of my_script.php
echo $incmyscript;
I have to be doing something wrong? I know echoing out the variabel $incmyscript is wrong, but I can't think of a way to place in the code. I am not sure if fopen is the best best way to get what I want.
Any ideas?
The message you are getting:
URL file-access is disabled in the server configuration
Indicates that the allow_url_include setting in your php.ini is set to Off. Enabling that option will allow you to do remote file inclusion, but be very careful with this as it's a pretty big security risk once the other site would be compromised (A hacker could easily inject their own remote code to your site).
Instead of echo, you could use eval.
Only do this if you want to execute PHP code from the other server, not if you just want to include HTML!
Even if you really want to execute PHP code from the other server, a man-in-the-middle could execute arbitrary PHP code on your server. You should therefore better use HTTPS or avoid the inclusion of the remote file at all.
Example:
$myscript = fopen("https://someotherwebsite/lib/my_script.php", "r");
$incmyscript= fread($myscript , 9999);
fclose($myscript);
$incmyscript);
Instead of the echo you could use this:
eval($incmyscript);
But be careful, this is very bad practice!
READ THIS: http://php.net/manual/en/function.eval.php
If you can trust remote script then you can call eval:
eval ($incmyscript);
If http://somewebsite.com/lib/somescript.php served by server supporting PHP you're trying to include it's output, not the code itself! Otherwise it's a just wrong and may be considered as security hole!
What you're trying to do is opening of a major security hole!
If the remote server is configured to process .php files, you won't be able to get the source for it. The server will process the PHP and then return any output. If getting remote PHP sources were possible, hackers would be grabbing our code and looking for vulnerabilities way too easily!
I wish I knew how to make a file that is a principal access to a web page. I'm using PHP to do this. It occurred to me the following:
function crearArchivoUrl($url){
$archivo=str_replace(array("http://", "https://", "mailto://", "ftp://"), "", $url);
$archivo=str_replace(array("/"), "-", $archivo);
$this->checkNombreDestino($archivo);
$contenido="[InternetShortcut]\r\nURL=".$url."\r\n";
$fp = fopen($archivo, 'w');
chmod($archivo, 0644);
fwrite($fp, $contenido);
fclose($fp);
}
But when I test it (by double-clicking on it) I did not jump the browser.
Anybody can tell me how to make files that are shortcuts to web pages?
Thanks for the help.
Greetings!
My code was correct, all I needed was that the file extension should be .url, ie, my-web-shortcut.url
Thanks for everything.
Greetings!
Your PHP will only be executed if you have a PHP interpreter installed, and if the webserver is configured to run your PHP file. If you distribute this file to unconfigured computers, it will never be executed as PHP code, and will therefore never run as you expect.
If you are running this script on a webserver that is properly configured, consider using header("Location: <URL>") to redirect the user to the new page.
Actually, you are reading a file, and writing it to another file. Doing that sends nothing to the web browser.
A first approach would be to send a header Location to redirect the broswer. See examples in the manual to know how to use it.
Another approach would be to read the file contents, and to print it, using echo or any other printing command.
I'm having a critical issue where my WAMP installation for PHP 5.3.0 is not finding a file which exists within my computer. Does anyone know anything about this? Possibly a PHP Bug?
Any help would be much appreciated.
Here is the variable which creates the file:
$baseNewsUrl = "C:/reviews/reviews/$platform/$fullname";
And here is the code which grabs the contents:
if(is_file($baseNewsUrl)){
$contents = file_get_contents($baseNewsUrl);
} else {
echo "File not found. " . "\r\n";
continue;
}
Here is the output of $baseNewsUrl: C:/reviews/reviews/GBA/r20107_GBA.htm
And the file does exist.
Check that the entire path leading up to your file is readable by the user PHP is running as (if you are using IIS, this might be something like "Network Service," although I am not particularly experienced with PHP on Windows). Also, check whether the INI directives "open_basedir" or perhaps "safe_mode" are set--these would give PHP self-imposed limits on which files are accessible.
Do a var_dump (not an echo) on your variable.
var_dump($baseNewsUrl);
and look at the actual contents. You may have some invisible garbage characters in there that's preventing Windows if you're doing this in a browser to make sure there's no empty tags (or other browser-render-invisible) characters.
If that doesn't reveal anything, remove the is_file check and try to open the file with file_get_contents (or any file related function) and var_dump it's contents. You'll either open the file, or PHP will spit out an error/warning/notice (either to your browser or to your error log) that should let you know why it can't open the file.
I'm gonna say this, and it very well might not be your problem but it is a recurring one for me. If you use skype on your computer, it has a somewhat known compatibility issue with WAMP. It cause's WAMP to be unstable, not load files properly.. everything.
on windows
$baseNewsUrl = "C:\\reviews\\reviews\\$platform\\$fullname";
It's due to Windows Vista and WAMP.