user1037355
user1037355

Reputation:

json output to file readable again in php

I want to be able to transfer a php array from one server to another via ftp in the form of a file.

The receiving server needs to be able to open the said file and read its contents and use the array provided.

I've thought about going about this two ways, either writing a php file from server 1 with php code of an array then simply loading this file on server 2. However writing the said file is getting tricky when the depth of the array is unknown.

So I though about writing the array to the file json encoded but I don't know how the second server could open and read the said data.

Could I simply do:

$jsonArray= json_encode($masterArray);
$fh = fopen('thefile.txt' , 'w');
fwrite($fh, $thePHPfile);
fclose($fh);

Then on the other server open the data into a variable:

$data = json_decode( include('thefile.txt') );

Has anyone had any experience of this before?

Upvotes: 0

Views: 253

Answers (4)

J. Bruni
J. Bruni

Reputation: 20492

To "transfer" the array between servers, using a file as medium, you found a nice solution by using json_encode and json_decode. The serialize and unserialize functions would perform the same goal nicely.

$my_array = array('contents', 'et cetera');

$serialized = serialize($my_array);
$json_encoded = json_encode($my_array);

// here you send the file to the other server, (you said you know how to do)
// for example:
file_put_contents($serialized_destination, $serialized);
file_put_contents($json_encoded_destination, $json_encoded);

In the receiving server, you just need to read the file contents and apply the corresponding "parse" function:

$serialized = file_get_contents($serialized_destination);
$json_encoded = file_get_contents($json_encoded_destination);

$my_array1 = unserialize($serialized);
$my_array2 = json_decode($json_encoded);

Upvotes: 0

Gregory Burns
Gregory Burns

Reputation: 840

The PHP Function that your looking for is : file_get_contents

$masterArray = array('Test','Test2','Test3');
$jsonArray= json_encode($masterArray);
$fh = fopen('thefile.txt' , 'w');
fwrite($fh, $jsonArray);
fclose($fh);

Then on the other server:

$masterArray = json_decode( file_get_contents('thefile.txt') );
var_dump($masterArray);

Upvotes: 0

Jez
Jez

Reputation: 2444

If you're only going to be interested in reading the file using PHP, have you thought about using serialize() and unserialize()?

See http://php.net/manual/en/function.serialize.php

It's also probably faster than json_encode() / json_decode() (see http://php.net/manual/en/function.serialize.php#103761).

Upvotes: 1

Mihai Iorga
Mihai Iorga

Reputation: 39704

For first server, connect to second server by FTP and put that file contents into a file

$jsonArray = json_encode($masterArray);
$stream    = stream_context_create(array('ftp' => array('overwrite' => true))); 
file_put_contents('ftp://user:pass@host/folder/thefile.txt', $jsonArray, 0, $stream);

use file_get_contents() for second server:

$data = json_decode( file_get_contents('/path/to/folder/thefile.txt') );

Upvotes: 3

Related Questions