1

I am trying to read a JSON file (approx 8KB) using the file_get_contents() method but the browser gives an error saying can't reach the site. I don't think that the file size is a problem because I can read larger (e.g. 10KB) HTML or other files using the same function. My JSON file contains comments and I have a function that removes all the comments in order to give valid json to json_decode(). I've even tried to rename the JSON file (currently names config.json) but it didn't worked. It seems like the contents are the problem but I don't know how to solve this issue. I am using WAMP in Windows. Below is the error screen my browser is showing.

enter image description here

Following is the code to read and var_dump() JSON:

<?php
    function clear($data) {
        return preg_replace("#(/\*([^*]|[\r\n]|(\*+([^*/]|[\r\n])))*\*+/)|([\s\t]//.*)|(^//.*)#", '', $data);
    }

    $data = file_get_contents('login/config.json');
    $data = clear($data);
    var_dump($data);
?>

And the JSON file is below:

{
    /* 
        This is the main configuration file. Some of the below fields are optional.

        MAKE SURE THAT THIS CONFIG FILE IS OUT OF REACH OF THE OUTSIDE WORLD
    ---------------------------------------------------------------------------------
      This file will contain some sensitive data that you must not be sharing with
      others. For example: your app_secret code, database username or/and password 
      Make sure that this file is not stored in public_html or directory where
      you usually store files that your users have access to.
    ---------------------------------------------------------------------------------

    */



    /*
    -------------------------------------------------------------------------------
        REQUIRED
        You get the app_id after creating a Facebook application.
        If not have one, visit https://developers.facebook.com/apps
        and click on the button saying 'Add a new App'
    -------------------------------------------------------------------------------
    */

    "app_id"            :       "",



    /*
    -------------------------------------------------------------------------------
        REQUIRED
        You get an app_secret code after creating a Facebook app.
        DON'T SHARE THE app_secret with anyone. Whenever an API request call
        requires this field as a parameter, the script makes a server-to-server
        call. This field is never meant to be public.
    -------------------------------------------------------------------------------
    */

    "app_secret"        :       "",



    /*
    -------------------------------------------------------------------------------
        REQUIRED
        This is the path to the file to which users are redirected after logging in.
        It's up to you which script do you want this field to refer to but don't
        forget to include the Verify.php class file becuase the script reffered by
        this field captures the response from login dialog and performs some actions.
    -------------------------------------------------------------------------------
    */

    "redirect_uri"      :       "",



    /*
    -------------------------------------------------------------------------------
        REQUIRED
        This field determines what data do you want from Facebook after logging
        users in. To know more about available permissions, refer to below link
        https://developers.facebook.com/docs/facebook-login/permissions/
    -------------------------------------------------------------------------------
    */

    "scope"             :       ["public_profile", "email"],



    /*
    -------------------------------------------------------------------------------
        REQUIRED
        This is an encrypted string unique to each login request. Options other than
        "code" are also available but "code" is best suited for calls from servers.
        https://developers.facebook.com/docs/facebook-login/manually-build-a-login-flow

        Note: Manipulations in the Verify.php file may be reuired if this field
        is manually set to value other than "code".
    -------------------------------------------------------------------------------
    */

    "response_type"     :       "code",



    /*
    -------------------------------------------------------------------------------
        REQUIRED
        If you gonna generate a CSRF token yourself, then set this to false.
        By default, the script automatically generates one for you.
    -------------------------------------------------------------------------------
    */

    "generate_token"    :       true,



    /*
    -------------------------------------------------------------------------------
        REQUIRED
        If you want some data to be saved in database, then set this to true and
        set up database credentials in below fields.
    -------------------------------------------------------------------------------
    */

    "save_to_db"        :       false,



    /*
    -------------------------------------------------------------------------------
        OPTIONAL
        This field is only required if "save_to_database" is set to true. If you
        want to save data in a database, type in the database name here.
    -------------------------------------------------------------------------------
    */

    "db_name"           :       "",



    /*
    -------------------------------------------------------------------------------
        OPTIONAL
        This field is only required if "save_to_database" is set to true.
        Give your database username here. 
    -------------------------------------------------------------------------------
    */

    "db_username"       :       "",



    /*
    -------------------------------------------------------------------------------
        OPTIONAL
        This field is only required if "save_to_database" is set to true.
        Give your database password here.
    -------------------------------------------------------------------------------
    */

    "db_password"       :       "",



    /*
    -------------------------------------------------------------------------------
        OPTIONAL
        This field is only required if "save_to_database" is set to true.
        Give the table name where you want to save user information.
    -------------------------------------------------------------------------------
    */

    "db_table"          :       "",



    /*
    -------------------------------------------------------------------------------
        OPTIONAL
        This field is only required if "save_to_database" is set to true.
        Populate the array with the data fields that you want to save in the database.

        fields are: user_first_name, user_last_name, user_id, user_gender, user_age,
        user_timezone, user_verified, user_friends, user_email, user_about_me,
        user_actions_books, user_actions_fitness, user_actions_music, user_actions_news,
        user_actions_video, user_birthday, user_education_history, user_events,
        user_game_activity, user_groups, user_hometown, user_likes, user_location,
        user_managed_groups, user_photos, user_posts, user_relationships, user_relationship_details,
        user_religion_politics, user_status, user_tagged_places, user_videos, user_website,
        user_work_history, user_custom_friendlists, user_insights, user_audience_network_insights,
        user_mailbox, user_page_mailboxes, user_stream, user_notifications, user_pages,

        Note: Choose what do you want to save wisely. You may never want to save a
        user's Facebook notifications.

        Note: Make sure you have given appropriate permissions (in the "scope" field)
        Without permissions, your app can't pull data from Facebook.
    -------------------------------------------------------------------------------
    */

    "db_save"           :       ["user_id", "user_first_name", "user_last_name"],



    /*
    -------------------------------------------------------------------------------
        OPTIONAL
        If this field is not set, the script will find out the version your server
        is running by manually calling some functions.
    -------------------------------------------------------------------------------
    */

    "php_version"       :       "",



    /*
    -------------------------------------------------------------------------------
        REQUIRED
        The duration (in minutes) you want the user session be saved for. When a
        user logs in, the script flags it by setting user_id as a session veriable.
        By default, this variable will only be saved for 60 minutes. That means, 
        the user will be logged in to the browser for 60 minutes if not requested to
        log out. You can extend or lower the duration of this session storage here.
    -------------------------------------------------------------------------------
    */

    "session_lifetime"  :       "60"
}
Fresher
  • 333
  • 1
  • 3
  • 13
  • "can't reach the site" seems like a custom error message. Post what is actually causing and displaying the error. – WillardSolutions Sep 16 '16 at 13:09
  • @EatPeanutButter I've posted an image showing the error. – Fresher Sep 16 '16 at 13:13
  • I'm behind a corporate firewall so I can't see your image – WillardSolutions Sep 16 '16 at 13:13
  • @EatPeanutButter The browser is taking more time to load the script than usually it takes to load local scripts and finally saying can't reach the site. – Fresher Sep 16 '16 at 13:16
  • are you sure that the path to the file is ok? and the file is there? If you try to load the file in the browser it is loading ok? – Daniel Dudas Sep 16 '16 at 13:21
  • That’s not a valid JSON file; the JSON standard does not allow for C-style block comments like in the example you have provided. – Jack Greenhill Sep 16 '16 at 13:21
  • Could be your regex. Comment out `$data = clear($data);` to see if that is slowing it down. And double-check the file path – WillardSolutions Sep 16 '16 at 13:22
  • @DanielDudas Yes, I am sure – Fresher Sep 16 '16 at 13:22
  • 1
    @JackGreenhill I agree that it's not a valid JSON file because of comments. But reading the file should not create any issue. After reading the file, as you can see, I am removing all the comments and making it a valid JSON string to decode. – Fresher Sep 16 '16 at 13:24
  • Check if `file_get_contents` is blocked or not. If you are on a shared server sometimes it's blocked. Chere here how to check: http://stackoverflow.com/questions/13433660/how-to-check-if-allow-url-fopen-is-enabled-or-not – Daniel Dudas Sep 16 '16 at 13:24
  • Hi @Fresher, Your json file is correct and the code is working properly at my local machine. The error of this type also comes due to some DNS issues. Try pinging your Url and see whether it works. – Domain Sep 16 '16 at 13:25
  • @EatPeanutButter Thanks! It seems like my regex is creating problems. Commenting the line solves the issue. – Fresher Sep 16 '16 at 13:27
  • Glad that helped you identify the issue. It's probably simpler for you to strip the comments out of the file manually instead of trying to do it this way. Regex can be expensive, especially a complex operation like this on a large amount of text. – WillardSolutions Sep 16 '16 at 13:34
  • @EatPeanutButter Thanks :) – Fresher Sep 16 '16 at 13:36

0 Answers0