Go Back   Site Owners Forums - Webmaster Forums > Web Programming > Programming General > PHP / mySQL

Notices


Reply
 
Thread Tools Rate Thread Display Modes
Old 07-18-2016, 01:22 AM   #1
williamjamestec
Registered User
 
Join Date: Jul 2016
Posts: 21
Post Weird problem writing json result to file

I'm trying to make sure I only pull information from an external resource when it's not already there, and therefore I'm trying to write the result to a file if the file isn't already there.

I use the following code:

if (file_exists($_SERVER['DOCUMENT_ROOT'].'/utilities/holidays_'.$file_date.'.json')) {
$get_holidays_year_and_month = file_get_contents($_SERVER['DOCUMENT_ROOT'].'/utilities/holidays_'.$file_date.'.json', true);
} else {
$write_to_file = json_decode(file_get_contents('http://holidayapi.com/v1/holidays?country=NO&year='.$request_year.'&pretty' ));
file_put_contents($_SERVER['DOCUMENT_ROOT'].'/utilities/holidays_'.$file_date.'.json',$write_to_file);
$get_holidays_year_and_month = file_get_contents($_SERVER['DOCUMENT_ROOT'].'/utilities/holidays_'.$file_date.'.json',true);
}

PHP:
It works, apart from the fact that it doesn't write anything to the file - it creates the file, but the file is empty, and I'm wondering if that's because it returns a stdObject - if that's it, how do I create something I can put into the file?

No errors returned, nothing says it doesn't work, but for some reason, there's nothing in the file after it gets written.

If I var_dump() the $write_to_file variable before the file_put_contents(), it contains the expected result.
williamjamestec is offline   Reply With Quote

Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump


All times are GMT -7. The time now is 08:08 PM.


Powered by vBulletin Copyright © 2020 vBulletin Solutions, Inc.