ASH*_*OSH 0 php arrays file-get-contents
我有一个php文件list.php
<?php
$arr=array('444','555');
echo var_export($arr);
?>
Run Code Online (Sandbox Code Playgroud)
现在我想使用file_get_contents从另一个php脚本获取数组.怎么能实现这个目标?我不想使用会话.这两个脚本位于不同的服务器上.
您可以使用serialize()数组或使用json_encode()JSON编码数组.然后,在另一个PHP脚本中,您将使用unserialize()或json_decode()将字符串返回到数组中.
示例,使用serialize():
在a.php中(在服务器A上)
$array = array( "foo" => 5, "bar" => "baz");
file_put_contents( 'array.txt', serialize( $array));
Run Code Online (Sandbox Code Playgroud)
在b.php中(在服务器B上)
$string = file_get_contents( 'http://www.otherserver.com/array.txt');
$array = unserialize( $string);
var_dump( $array); // This will print the original array
Run Code Online (Sandbox Code Playgroud)
您还可以从PHP脚本输出字符串,而不是将其保存到文件中,如下所示:
在a.php中(在服务器A上)
$array = array( "foo" => 5, "bar" => "baz");
echo serialize( $array); exit;
Run Code Online (Sandbox Code Playgroud)
在b.php中(在服务器B上)
$string = file_get_contents( 'http://www.otherserver.com/a.php');
$array = unserialize( $string);
var_dump( $array); // This will print the original array
Run Code Online (Sandbox Code Playgroud)