Reputation: 1301
I have 2 server serverA and serverB.
server A has below code
$dir = "/boot";
echo "<pre>";
print_r(listFolders($dir));
function listFolders($dir)
{
$dh = scandir($dir);
$return = array();
foreach ($dh as $folder)
{
if ($folder != '.' && $folder != '..')
{
if (is_dir($dir . '/' . $folder))
{
$return[] = array($folder => listFolders($dir . '/' . $folder));
}
}
}
return $return;
This will give me output as array in serverA
But when I user curl from serverB then it give me output as string.
How to get output as array from serverB?
My output show as
Array
(
[0] => Array
(
[efi] => Array
(
)
)
[1] => Array
(
[grub] => Array
(
[0] => Array
(
[fonts] => Array
(
)
)
[1] => Array
(
[i386-pc] => Array
(
)
)
[2] => Array
(
[locale] => Array
(
)
)
[3] => Array
(
[x86_64-efi] => Array
(
)
)
)
)
)
but above output is show me as string instead of array.
Upvotes: 4
Views: 1365
Reputation: 1817
Make serverA print your data in JSON format using json_encode()
instead of print_r()
:
echo json_encode(listFolders($dir));
Then the output of cURL (which will be a JSON array STRING) can be converted to array using json_decode()
:
$dir_array = json_decode($stringReturnedByCURL);
cURL is an HTTP client. The only thing it does is returning the string containing the HTML web page. In this web page you can put that JSON array.
This way you're using HTTP to transport your data encoded in JSON from serverA to serverB.
The output of the page in your question just prints the array in print_r
format, which does not have a function to decode that format.
WARNING: you need to print the JSON only, remove that echo "<pre>";
of the json_decode()
in serverB will find an unexpected <pre>
in the code he has to decode.
Upvotes: 1
Reputation: 1290
Try to return the array in JSON format.
eg. return json_encode($return)
edit 1
if you want to transfer some these kind of data then you have to do it in JSON format or the xml
Upvotes: 0