PHP脚本在浏览器中的执行速度比在python /其他PHP脚本中执行得快

时间:2015-09-24 23:03:11

标签: php python curl

我用PHP编写了一个API。当我使用浏览器调用它时,它的执行速度非常快( 3s )。但是,如果我使用另一个PHP脚本(我编写它来进行测试)来调用它,则每个请求需要花费一些时间(24秒)!我使用curl来调用URL。有谁知道发生了什么事?

系统配置:

  • 使用WAMP运行PHP。
  • 在本地计算机上托管。

尝试解决方案:

  1. 禁用所有防火墙
  2. 添加了curl_setopt选项($ ch,CURLOPT_IPRESOLVE,CURL_IPRESOLVE_V4);
  3. 我甚至编写了一个python脚本来调用PHP API,这也需要很长时间。似乎浏览器提供了最佳的响应时间。

    感谢任何帮助。

    更新了代码:

    <?php
    
    // Class to handle all Utilities
    Class Utilities{
    
    // Make a curl call to a URL and return both JSON & Array
    public function callBing($bingUrl){
        //  Initiate curl   
        $ch = curl_init();
        // Disable SSL verification
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        // Will return the response, if false it print the response
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        // Set the url
        curl_setopt($ch, CURLOPT_URL,$bingUrl);
        // Performance Tweak
        curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12');
        session_write_close();
        // Execute
        $bingJSON=curl_exec($ch);
        // Closing
        curl_close($ch);
        $bingArray = json_decode($bingJSON,true);
        return array( "array" => $bingArray , "json" => $bingJSON );
        }
    
    }
    
    
    ?>
    
    <?php
    // The Test script
    include_once('class/class.Utilities.php');
    
    $util = new Utilities();
    
    echo "<style> td { border : thin dashed black;}</style>";
    
        // Test JSON
        $testJSON = '
            {
                "data" : [
                 { "A" : "24324" , "B" : "64767", "expectedValue" : "6.65" , "name" : "Test 1"},
                 { "A" : "24324" , "B" : "65464", "expectedValue" : "14" , "name" : "Test 2"}
                ]
            }
        ';
        $testArray = json_decode($testJSON, TRUE);
    
        echo "<h1> Test Results </h1>";
        echo "<table><tr><th>Test name</th><th> Expected Value</th><th> Passed ? </th></tr>";
        $count = count($testArray["data"]);
        for ($i=0; $i < $count ; $i++) {
            $url = "http://localhost/API.php?txtA=".urlencode($testArray["data"][$i]["A"])."&txtB=".urlencode($testArray["data"][$i]["B"]);
            $result = $util->callutil($url);
    
            if($testArray["data"][$i]["expectedValue"] == $result["value"])
                $passed = true;
            else
                $passed = false;
    
            if($passed)
                $passed = "<span style='background:green;color: white;font-weight:bold;'>Passed</span>";
            else
                $passed = "<span style='background:red;color: white;font-weight:bold;'>Failed</span>";  
    
    
            echo "<tr><td>".$testArray["data"][$i]["name"]."</td><td>".$testArray["data"][$i]["expectedValue"]."</td><td>$passed</td></tr>";
        }
        echo "</table>";
    
    ?>
    

1 个答案:

答案 0 :(得分:0)

启动解释器和解析代码(无论是php,python,ruby等)都会产生开销。当您在服务器进程中运行代码时,在服务器最初启动时支付启动成本,并且仅在请求上执行应用程序逻辑(加上一些次要请求/响应开销)。但是,在手动运行代码时,在运行代码之前会发生额外的启动开销,从而导致您看到的速度变慢。这就是mod_php和mod_wsgi存在的原因(与使用CGI api的框架相反)。

相关问题