0

I created a php array file.

My PHP Array File Example.

I put array to g.horje.com/t.php

<?php

$val = array (
  0 => 
  array (
    0 => '1',
    'id' => '1',
    1 => 'Search Engine Google',
    'title' => 'Search Engine Google',
    2 => 'Search the world.',
    'description' => 'Search the world.',
  ),
  1 => 
  array (
    0 => '2',
    'id' => '2',
    1 => 'Social Network',
    'title' => 'Social Network',
    2 => 'Facebook Social Network',
    'description' => 'Facebook Social Network',
  ),
 
);
?>

Now I want to get Array Result using url file get contents. Example:

<?php

$homepage = file_get_contents("https://g.horje.com/t.php");
echo $homepage;

foreach($val as $fetch){

  echo $fetch['title'];
  echo '</br>';
  echo $fetch['description'];
}
?>

Can anyone tell me how to execute it ?

12
  • In the t.php, add echo json_encode($val); as the last line of php code. On the other page, add $val = json_decode($homepage, true); below echo $homepage; Commented Nov 12 at 6:24
  • Note that anyone requesting g.horje.com/t.php will see the array contents. you may want to protect it with some token Commented Nov 12 at 6:25
  • You could include the file, which would execute the content and create the $val array. This isn't a great idea. It represents a possible security hole, and editing the file is prone to error. Go for something like JSON, and use json_decode to decide it into an array. It's easier to edit, and doesn't have the risk of executing arbitrary code. Commented Nov 12 at 6:27
  • Are these two PHP files part of the same application, or different ones? Commented Nov 12 at 9:56
  • 1
    @ADyson --👍 👍 Commented Nov 16 at 23:49

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.