4

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.

1
  • wait, serverA and serverB are both web servers, right? and you're trying to get data from serverA to serverB using curl? Commented Oct 13, 2017 at 11:08

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

5 Comments

I have used below command to retrieve output $data=sudo /usr/bin/curl --silent -m 15 http://$serveraip/treeview.php; This it show output as string instead of array.
if you are using curl for retrieving the result then it will always give you the string as output unless and until you change the header type of it
Show me that header type
it's the content-type man. As I know there is no content-type that will return the content as PHP array
Dhaval is correct. Return the response from treeview.php as json_encode($data); and on the server fetching it, you can use $data = json_decode($result). You cannot send a php object from one server to another as an php array

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.