询问服务器发送json和客户端接收进程[php]不工作时,服务器有条件,例如如果否则?
当我使用这段代码时,它是ok的。
send.php
{"text": "aaa","number": "111"}
receive.php
<?PHP
$url = "http://www.example.com/send.php";
$json = @file_get_contents($url, true);
$decode = json_decode($json, true);
$number = $decode[number];
echo $decode[number];
?>
但是当我将send.php
更改为receive。php中的代码而不是echo $decode[number];
<?php
$test = "111111";
if ($test != '')
{
?>
{"text": "aaa","number": "111"}
<?PHP
}
else
{
?>
{"text": "bbb","number": "222"}
}
?>
您忘记包含<?php
来关闭if语句,以便在响应中发送}
(使响应无效JSON)。
更好的方法是使用数组或对象,然后JSON对其进行编码:
$output = array(
'text' : 'aaa',
'number' : '111'
);
if($test == ''){
$output['text'] = 'bbb';
$output['number'] = '222';
}
echo json_encode($output);