【问题标题】:This is causing my page to load very slow or not at all?这导致我的页面加载非常缓慢或根本不加载?
【发布时间】:2011-10-26 02:57:14
【问题描述】:

我刚刚从 php 5.3.4 更新到 5.3.8。更新后,我所有包含 try{ 的代码似乎都会导致我的页面挂起并用完我所有的服务器内存。

if(preg_match('/^(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)(?:[.](?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)){3}$/', $ip)){
$xml = @file_get_contents('http://' . $this->service . '/' . $this->version . '/' . $name . '/?key=' . $this->apiKey . '&ip=' . $ip . '&format=xml');
try{
$response = @new SimpleXMLElement($xml);

foreach($response as $field=>$value){
$result[(string)$field] = (string)$value; }
return $result;
}
catch(Exception $e){ $this->errors[] = $e->getMessage();
return;
}
}

这也导致了一个重大问题

<?php

try{
$gt = new Gtranslate;
$gt->setRequestType('curl');

$SQL = "SELECT * FROM PAGE_CONTENT WHERE live_page = '1'";
$result = mysql_query( $SQL );
while( $row = mysql_fetch_array( $result ) ) {
$page_id_sub = $row["page_id"];
$page_title = $row["page_title"];
$page_permalink = $row["page_permalink"];

if(empty($mylang)){
echo "<a href='/$permalink/$page_permalink.html'>$page_title</a> |";
}
else {
$page_trans = $gt->$mylang("$page_title");
echo "<a href='/$permalink/$page_permalink.html'>$page_trans</a> |";
}
}
}
catch (GTranslateException $ge){
echo $ge->getMessage();
}
?>

【问题讨论】:

    标签: php memory-leaks try-catch


    【解决方案1】:

    这可能与try 关系不大,而与您的远程请求(即file_get_contents()$gt-&gt;$mylang())关系更大。

    作为基准,删除这些行并查看您的网页效果如何。如果他们确实是罪魁祸首,您可能需要考虑缓存他们的响应或其他方法,这样您就不会在每次页面加载时都发出请求。

    【讨论】:

    • 你说得对,我注释掉了 $xml = @file_get_contents('http://' . $this->service . '/' . $this->version . '/' . $name . '/?key=' . $this->apiKey . '&ip=' . $ip . '&format=xml');并且页面加载正常。但是为什么它以前可以工作,我该如何解决?
    • 我用 simplexml_load_file 替换了 file_get_contents。使页面加载速度更快,但仍然有点慢
    • 如果服务器上没有其他任何变化,也许您可以查看 PHP 升级。但我认为这很可能是侥幸。您可以随时删除 try/catch 或错误抑制。无论如何,后者是不明智的。
    猜你喜欢
    • 1970-01-01
    • 2017-03-01
    • 1970-01-01
    • 2021-04-11
    • 2023-04-01
    • 2016-04-03
    • 1970-01-01
    • 1970-01-01
    • 2016-06-11
    相关资源
    最近更新 更多