简体   繁体   English

以更快的方式在线阅读 json 文件 php

[英]Read online json file in faster way php

I am using file_get_contents to read online json URL and i don't have cURL installed any advised how to make my request faster我正在使用file_get_contents在线阅读 json URL 并且我没有安装cURL任何建议如何使我的请求更快

Thanks, Mariana谢谢,玛丽安娜

Do Some simple benchmarking:做一些简单的基准测试:

<?php
$start = microtime(true);
for ($i=0;$i<=10;$i++){
    $handle = fopen("http://example.com/", "r");
    while (!feof($handle)) {
        $result .= fread($handle, 1024);
    }
    fclose($handle);
}
$end = microtime(true);
$time = $end - $start;

echo "Did fopen test in $time seconds<br />\n";
?> 

Did fopen test in 6.1602981090546 seconds是否在 6.1602981090546 秒内进行了 fopen 测试

<?php
//file_get_contents is basically a wrapper for the above fopen so hence not much of a difference
$start = microtime(true);
for ($i=0;$i<=10;$i++){
    $result = file_get_contents('http://example.com');
}
$end = microtime(true);
$time = $end - $start;

echo "Did file_get_contents test in $time seconds<br />\n";
?>

Did file_get_contents test in 6.5289459228516 seconds是否在 6.5289459228516 秒内测试了 file_get_contents

<?php
$start = microtime(true);
for ($i=0;$i<=10;$i++){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "http://example.com");
    curl_setopt($ch, CURLOPT_HEADER, 0);
    $result = curl_exec($ch);
}
$end = microtime(true);
$time = $end - $start;

echo "Did cUrl test in $time seconds<br />\n";
?>

Did cUrl test in 2.9657130241394 seconds在 2.9657130241394 秒内完成 cUrl 测试

cURL wins hands down... time to look for a better host cURL 轻而易举地获胜......是时候寻找更好的主机了

Either way sould be fast.无论哪种方式都应该很快。
http://www.ebrueggeman.com/blog/php_benchmarking_fopen http://www.ebrueggeman.com/blog/php_benchmarking_fopen

Theres not better.没有比这更好的了。

There is not really a way to make your request faster.没有真正的方法可以使您的请求更快。 What you can do is cache the data locally if it does not change a lot.如果数据变化不大,您可以做的是在本地缓存数据。

Pseudocode:伪代码:

if (get_modification_time_of_file('cached.json') < time() - 300) {
    download_file()
} else {
    read_locally()
}

You have 2 options.你有两个选择。

1: By using the fopen/file_get_contents function(s) 1: 通过使用 fopen/file_get_contents 函数

2: By setting a client side bridge and sending it to php via a POST method using AJAX. then get it on the PHP using json_decode. 2:通过设置客户端桥并使用 AJAX 通过 POST 方法将其发送到 php。然后使用 json_decode 在 PHP 上获取它。

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

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