简体   繁体   English

如何在不使用file_get_contents的情况下在PHP中读取JSON文件?

[英]How to read JSON file in PHP without using file_get_contents?

I've created a simple php file which reads a local json file and use the data to response to some queries sent to the page. 我创建了一个简单的php文件,该文件读取本地json文件,并使用该数据来响应发送到页面的某些查询。

I'm using file_get_contents() to read the file. 我正在使用file_get_contents()读取文件。 but as the requests count grown, I faced some performance issues because of concurrent connections to the file. 但是随着请求数量的增加,由于并发连接到文件,我遇到了一些性能问题。

Is it possible to use require() or include() to read and parse the json file? 是否可以使用require()include()读取和解析json文件?

I've found some solution that fits, I'm now using curl to get the file contents like this: 我找到了适合的解决方案,我现在使用curl来获取文件内容,如下所示:

$curlSession = curl_init();
curl_setopt($curlSession, CURLOPT_URL, 'http://mysite/file.json');
curl_setopt($curlSession, CURLOPT_BINARYTRANSFER, true);
curl_setopt($curlSession, CURLOPT_RETURNTRANSFER, true);
$jsonDataString = curl_exec($curlSession);
curl_close($curlSession);

Yes, you can use require() 是的,您可以使用require()

 ob_start();
 ob_implicit_flush(false);
 require('test.json');
 $json = json_decode(ob_get_clean() , true );

but I think its not faster than file_get_contents() 但我认为它不比file_get_contents()

The best way for performance is to load json-file to browser directly (eg by jquery) without php-parsing. 最好的性能方法是直接将json文件加载到浏览器中(例如通过jquery)而不进行php解析。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM