从url获取数据的最快方法

时间:2015-03-16 10:29:40

标签: php jquery curl file-get-contents getjson

我想从Linkedin URL读取数据,例如https://www.linkedin.com/countserv/count/share?url=https://stackoverflow.com/&format=json

我有几种方法可以做,但我找不到合适/最快的解决方案。

  1. file_get_contents():大约需要6秒
  2. 使用cURL:大约需要3秒
  3. jquery getJSON:花费不到1秒
  4. 这是什么意思?我的服务器有问题吗?或者我需要使用其他功能吗?

    - 额外的信息: 我已经创建了一个包含多个域的测试。

    <?php
    $time_start = microtime(true);
    $data = file_get_contents("https://www.linkedin.com");
    $time_end = microtime(true);
    $time[test1] = $time_end - $time_start;
    
    $time_start = microtime(true);
    $data = file_get_contents("https://stackoverflow.com/");
    $time_end = microtime(true);
    $time[test2] = $time_end - $time_start;
    
    $time_start = microtime(true);
    $data = file_get_contents("https://www.google.com");
    $time_end = microtime(true);
    $time[test3] = $time_end - $time_start;
    
    $time_start = microtime(true);
    $data = file_get_contents("https://www.expedia.com");
    $time_end = microtime(true);
    $time[test4] = $time_end - $time_start;
    
    $time_start = microtime(true);
    $data = file_get_contents("https://www.booking.com");
    $time_end = microtime(true);
    $time[test5] = $time_end - $time_start;
    
    print_r($time);
    ?>
    

    <?php $time_start = microtime(true); $data = file_get_contents("https://www.linkedin.com"); $time_end = microtime(true); $time[test1] = $time_end - $time_start; $time_start = microtime(true); $data = file_get_contents("https://stackoverflow.com/"); $time_end = microtime(true); $time[test2] = $time_end - $time_start; $time_start = microtime(true); $data = file_get_contents("https://www.google.com"); $time_end = microtime(true); $time[test3] = $time_end - $time_start; $time_start = microtime(true); $data = file_get_contents("https://www.expedia.com"); $time_end = microtime(true); $time[test4] = $time_end - $time_start; $time_start = microtime(true); $data = file_get_contents("https://www.booking.com"); $time_end = microtime(true); $time[test5] = $time_end - $time_start; print_r($time); ?> 输出如下:

    
        Array
        (
            [test1] => 3.5712170600891
            [test2] => 0.92465400695801
            [test3] => 6.2011067867279
            [test4] => 1.8765568733215
            [test5] => 1.758278131485
        )
    
    

0 个答案:

没有答案