0

我正在尝试通过产品代码(也来自数据库表)从 xml 文件中读取价格表和库存表,并将结果放入 MySQL 表中。

除了 xml 文件的响应或读取时间外,一切正常。PHP 脚本、数据库和 XML 文件位于同一台服务器上。在查看 Apache access.log 后,我看到 PHP 脚本在启动后可以非常快速地读取(或响应)。但过了一会儿,它每分钟只读取 1 个:

[...]
1.2.3.4 - - [09/May/2013:13:11:21 +0300] "GET /feeds/stock.xml HTTP/1.0" 200 16202 "-" "-"
1.2.3.4 - - [09/May/2013:13:11:21 +0300] "GET /feeds/price.xml HTTP/1.0" 200 82143 "-" "-"
1.2.3.4 - - [09/May/2013:13:11:21 +0300] "GET /feeds/stock.xml HTTP/1.0" 200 16202 "-" "-"
1.2.3.4 - - [09/May/2013:13:11:21 +0300] "GET /feeds/price.xml HTTP/1.0" 200 82143 "-" "-"
1.2.3.4 - - [09/May/2013:13:11:21 +0300] "GET /feeds/stock.xml HTTP/1.0" 200 16202 "-" "-"
1.2.3.4 - - [09/May/2013:13:11:21 +0300] "GET /feeds/price.xml HTTP/1.0" 200 82143 "-" "-"
1.2.3.4 - - [09/May/2013:13:11:21 +0300] "GET /feeds/stock.xml HTTP/1.0" 200 16202 "-" "-"
1.2.3.4 - - [09/May/2013:13:11:21 +0300] "GET /feeds/price.xml HTTP/1.0" 200 82143 "-" "-"
1.2.3.4 - - [09/May/2013:13:11:21 +0300] "GET /feeds/stock.xml HTTP/1.0" 200 16202 "-" "-"
1.2.3.4 - - [09/May/2013:13:11:21 +0300] "GET /feeds/price.xml HTTP/1.0" 200 82143 "-" "-"
1.2.3.4 - - [09/May/2013:13:11:21 +0300] "GET /feeds/stock.xml HTTP/1.0" 200 16202 "-" "-"
1.2.3.4 - - [09/May/2013:13:11:21 +0300] "GET /feeds/price.xml HTTP/1.0" 200 82143 "-" "-"
1.2.3.4 - - [09/May/2013:13:11:21 +0300] "GET /feeds/stock.xml HTTP/1.0" 200 16202 "-" "-"
1.2.3.4 - - [09/May/2013:13:11:21 +0300] "GET /feeds/price.xml HTTP/1.0" 200 82143 "-" "-"
1.2.3.4 - - [09/May/2013:13:11:21 +0300] "GET /feeds/stock.xml HTTP/1.0" 200 16202 "-" "-"
1.2.3.4 - - [09/May/2013:13:11:21 +0300] "GET /feeds/price.xml HTTP/1.0" 200 82143 "-" "-"
1.2.3.4 - - [09/May/2013:13:11:21 +0300] "GET /feeds/stock.xml HTTP/1.0" 200 16202 "-" "-"
1.2.3.4 - - [09/May/2013:13:11:21 +0300] "GET /feeds/price.xml HTTP/1.0" 200 82143 "-" "-"
1.2.3.4 - - [09/May/2013:13:11:21 +0300] "GET /feeds/stock.xml HTTP/1.0" 200 16202 "-" "-"
1.2.3.4 - - [09/May/2013:13:11:21 +0300] "GET /feeds/price.xml HTTP/1.0" 200 82143 "-" "-"
1.2.3.4 - - [09/May/2013:13:11:21 +0300] "GET /feeds/stock.xml HTTP/1.0" 200 16202 "-" "-"
1.2.3.4 - - [09/May/2013:13:11:21 +0300] "GET /feeds/price.xml HTTP/1.0" 200 82143 "-" "-"
1.2.3.4 - - [09/May/2013:13:11:21 +0300] "GET /feeds/stock.xml HTTP/1.0" 200 16202 "-" "-"
1.2.3.4 - - [09/May/2013:13:11:21 +0300] "GET /feeds/price.xml HTTP/1.0" 200 82143 "-" "-"
1.2.3.4 - - [09/May/2013:13:11:21 +0300] "GET /feeds/stock.xml HTTP/1.0" 200 16202 "-" "-"
1.2.3.4 - - [09/May/2013:13:11:21 +0300] "GET /feeds/price.xml HTTP/1.0" 200 82143 "-" "-"
1.2.3.4 - - [09/May/2013:13:11:21 +0300] "GET /feeds/stock.xml HTTP/1.0" 200 16202 "-" "-"
1.2.3.4 - - [09/May/2013:13:11:21 +0300] "GET /feeds/price.xml HTTP/1.0" 200 82143 "-" "-"
1.2.3.4 - - [09/May/2013:13:11:21 +0300] "GET /feeds/stock.xml HTTP/1.0" 200 16202 "-" "-"
1.2.3.4 - - [09/May/2013:13:11:21 +0300] "GET /feeds/price.xml HTTP/1.0" 200 82143 "-" "-"
1.2.3.4 - - [09/May/2013:13:12:22 +0300] "GET /feeds/stock.xml HTTP/1.0" 200 16202 "-" "-"
1.2.3.4 - - [09/May/2013:13:13:22 +0300] "GET /feeds/price.xml HTTP/1.0" 200 82143 "-" "-"
1.2.3.4 - - [09/May/2013:13:14:22 +0300] "GET /feeds/stock.xml HTTP/1.0" 200 16202 "-" "-"
::1 - - [09/May/2013:13:15:04 +0300] "GET / HTTP/1.1" 200 1007 "-" "Mozilla/5.0 (ISPConfig monitor)"
1.2.3.4 - - [09/May/2013:13:15:22 +0300] "GET /feeds/price.xml HTTP/1.0" 200 82143 "-" "-"
1.2.3.4 - - [09/May/2013:13:16:22 +0300] "GET /feeds/stock.xml HTTP/1.0" 200 16202 "-" "-"
1.2.3.4 - - [09/May/2013:13:17:22 +0300] "GET /feeds/price.xml HTTP/1.0" 200 82143 "-" "-"
1.2.3.4 - - [09/May/2013:13:18:22 +0300] "GET /feeds/stock.xml HTTP/1.0" 200 16202 "-" "-"
1.2.3.4 - - [09/May/2013:13:19:22 +0300] "GET /feeds/price.xml HTTP/1.0" 200 82143 "-" "-"
::1 - - [09/May/2013:13:20:03 +0300] "GET / HTTP/1.1" 200 1007 "-" "Mozilla/5.0 (ISPConfig monitor)"
1.2.3.4 - - [09/May/2013:13:20:22 +0300] "GET /feeds/stock.xml HTTP/1.0" 200 16202 "-" "-"
1.2.3.4 - - [09/May/2013:13:21:22 +0300] "GET /feeds/price.xml HTTP/1.0" 200 82143 "-" "-"
1.2.3.4 - - [09/May/2013:13:22:22 +0300] "GET /feeds/stock.xml HTTP/1.0" 200 16202 "-" "-"
1.2.3.4 - - [09/May/2013:13:23:22 +0300] "GET /feeds/price.xml HTTP/1.0" 200 82143 "-" "-"
1.2.3.4 - - [09/May/2013:13:24:22 +0300] "GET /feeds/stock.xml HTTP/1.0" 200 16202 "-" "-"
::1 - - [09/May/2013:13:25:03 +0300] "GET / HTTP/1.1" 200 1007 "-" "Mozilla/5.0 (ISPConfig monitor)"
1.2.3.4 - - [09/May/2013:13:25:23 +0300] "GET /feeds/price.xml HTTP/1.0" 200 82143 "-" "-"
1.2.3.4 - - [09/May/2013:13:26:23 +0300] "GET /feeds/stock.xml HTTP/1.0" 200 16202 "-" "-"
1.2.3.4 - - [09/May/2013:13:27:23 +0300] "GET /feeds/price.xml HTTP/1.0" 200 82143 "-" "-"
1.2.3.4 - - [09/May/2013:13:28:23 +0300] "GET /feeds/stock.xml HTTP/1.0" 200 16202 "-" "-"
1.2.3.4 - - [09/May/2013:13:29:23 +0300] "GET /feeds/price.xml HTTP/1.0" 200 82143 "-" "-"
::1 - - [09/May/2013:13:30:04 +0300] "GET / HTTP/1.1" 200 1007 "-" "Mozilla/5.0 (ISPConfig monitor)"
1.2.3.4 - - [09/May/2013:13:30:23 +0300] "GET /feeds/stock.xml HTTP/1.0" 200 16202 "-" "-"
1.2.3.4 - - [09/May/2013:13:31:23 +0300] "GET /feeds/price.xml HTTP/1.0" 200 82143 "-" "-"
1.2.3.4 - - [09/May/2013:13:32:23 +0300] "GET /feeds/stock.xml HTTP/1.0" 200 16202 "-" "-"
1.2.3.4 - - [09/May/2013:13:33:23 +0300] "GET /feeds/price.xml HTTP/1.0" 200 82143 "-" "-"
1.2.3.4 - - [09/May/2013:13:34:23 +0300] "GET /feeds/stock.xml HTTP/1.0" 200 16202 "-" "-"
::1 - - [09/May/2013:13:35:03 +0300] "GET / HTTP/1.1" 200 1007 "-" "Mozilla/5.0 (ISPConfig monitor)"
1.2.3.4 - - [09/May/2013:13:35:23 +0300] "GET /feeds/price.xml HTTP/1.0" 200 82143 "-" "-"
1.2.3.4 - - [09/May/2013:13:36:23 +0300] "GET /feeds/stock.xml HTTP/1.0" 200 16202 "-" "-"
1.2.3.4 - - [09/May/2013:13:37:23 +0300] "GET /feeds/price.xml HTTP/1.0" 200 82143 "-" "-"
1.2.3.4 - - [09/May/2013:13:38:23 +0300] "GET /feeds/stock.xml HTTP/1.0" 200 16202 "-" "-"
1.2.3.4 - - [09/May/2013:13:39:24 +0300] "GET /feeds/price.xml HTTP/1.0" 200 82143 "-" "-"
::1 - - [09/May/2013:13:40:03 +0300] "GET / HTTP/1.1" 200 1007 "-" "Mozilla/5.0 (ISPConfig monitor)"
1.2.3.4 - - [09/May/2013:13:40:24 +0300] "GET /feeds/stock.xml HTTP/1.0" 200 16202 "-" "-"
[...]

从 xml 文件中读取价格的函数:

function get_price($product_id, $source='xml'){
    $price      = NULL;
    $url = "http://www.mydomain.com/feeds/price.xml";

    $xml = simplexml_load_file($url);
    $Price = $xml->xpath("//PRODUCT[@CODE='". $product_id ."']");

    return $price;
}

读取股票 xml 的功能非常相似。

所有配置(MySQL、PHP、Apache)几乎都是默认配置,没什么特别的。我不知道为什么一段时间后阅读率会下降:(

服务器规格:

  • CentOS 6.4 版(最终版)
  • ISP 配置 3.0.5.2
  • Apache/2.2.15 (Unix)
  • MySQL 5.1.69
  • PHP 5.3.3
4

1 回答 1

1

不使用http获取文件,直接调用file

使用此代码

$url = "feeds/price.xml";

您还可以使用此代码获取 feeds 目录中的所有文件并在 $Price 变量中获取所有结果

 $Price = "";
 foreach (glob("feeds/*.xml") as $filename) { //Getting all xml files from feeds direcotry
       $xml = simplexml_load_file("feeds/".$filename);
       $Price .= $xml->xpath("//PRODUCT[@CODE='". $product_id ."']");
  }
  print_r($Price);
于 2013-05-10T11:54:44.440 回答