Read a JSON file with php

Json file, how to read it using php?

Most importantly, get the JSON content using file_get_contents() and to not avoid warnings specify the sources:

To read the file with an http request specify http/https:

$jsonData = file_get_contents('http://www.sitename.io/data/jsondata.JSON');

To read the file form the filesystem:

$config = file_get_contents(__DIR__ . '/config.json');

decode the JSON using json_decode():

$data = json_decode($config, true);

Use var_dump or print_r to understand the data structure. Having this data:

{
  "databaseOne": {
    "dbhost": "localhost:8889",
    "dbname": "dev_db_1",
    "dbusername": "root",
    "dbpassword": "root"
  },
  "databaseTwo": {
    "dbhost": "localhost:8889",
    "dbname": "dev_db_2",
    "dbusername": "root",
    "dbpassword": "root"
  }
}

To print the database name of databaseOne:

echo $data['databaseOne']['dbname'];

Use this code to loop through the array:

foreach ($data['databaseOne'] as $field => $value) {
    echo $field . ':' . $value . '</br>';
}

This is an example to show how to print the database name for each entries:

foreach ($$data as $content) {
        if($content) {
            foreach ($content as $key => $value) {
                if ($key === 'dbname'){
                    echo ($value . '</br>');
                }
            }
        }
    }

Composer basics

Here a basics list of Composer commands to retrieve the most used one.

Start new php project:

composer init

Install a new packages with:

composer require packageName

Use https://packagist.org/ to find the exact component name

In order to get the latest versions:

composer update

Uninstall packages from composer:

composer remove packageName

Search package from composer:

composer search packageName

Shows a list of installed packages that have updates available:

composer outdated

You should always run the validate command before you commit your composer.json file:

composer validate

WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!

When you try to connect to a remote server via ssh and you receive the message:

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@    WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!     @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
Someone could be eavesdropping on you right now (man-in-the-middle attack)!
It is also possible that a host key has just been changed.
The fingerprint for the RSA key sent by the remote host is
SHA256:shacode.
Please contact your system administrator.
Add correct host key in /Users/username/.ssh/known_hosts to get rid of this message.
Offending RSA key in /Users/username/.ssh/known_hosts:21
RSA host key for IP_ADDRESS has changed and you have requested strict checking.
Host key verification failed.

you can reset the known_host for the ip_address with the command:

ssh-keygen -R <hostname or ip_address>

the -R hostname Removes all keys belonging to hostname from a known_hosts file.