我有一个C#客户端,它将json发送到我的php服务器。
有json字符串:
{"数据":[{"名称":" 1"}]}
它是一个有效的json。当我在PHP Sandbox中尝试它时效果很好
$response = $connection -> getData();
// return $response; (Its equal : {"data":[{"name":"1"}]} )
$contents = utf8_encode($response);
$results = json_decode($contents);
dd($results->data); // Error Trying to get property of non-object
但是,当我尝试使用laravel 5.1时,它的效果不佳。
{{1}}
我希望有人可以帮助我。 谢谢!
答案 0 :(得分:1)
根据评论,看起来socket_read()
方法中的getData()
一次读取1个字符,然后将每个NUL终止的字符连接到响应字符串中。 json_decoded()
使所有额外的NUL字符窒息。
您可以使用getData()
方法更新逻辑,这样就不会这样做,或者您可以对结果运行str_replace:
$response = $connection -> getData();
// get rid of the extra NULs
$response = str_replace(chr(0), '', $response);
$contents = utf8_encode($response);
$results = json_decode($contents);
dd($results->data);