简体   繁体   English

PHP性能问题

[英]PHP performance issue

I am trying to get the coin data of this website: http://www.tf2wh.com . 我正在尝试获取该网站的硬币数据: http : //www.tf2wh.com

With this script: 使用此脚本:

$name = $_POST["item"];

$url = file_get_contents("http://www.tf2wh.com/allitems");
$dom = new DOMDocument();
@$dom->loadHTML($url);
$dom->saveHTML();

$code = "";
$xpath = new DOMXPath($dom);
foreach($xpath->query('//div[contains(attribute::class, "entry qual")]') as $e ) {
    $code .= $e->nodeValue;
}
$code = substr($code,strpos($code,$name)-30,30);
$code = explode("(",$code);
$coins = "";
for($i = 0; $i < strlen($code[0]); $i++){
    if(is_numeric($code[0][$i])){
        $coins .= $code[0][$i];
    }
}
echo $coins;

It works fine but there are two problems. 它工作正常,但是有两个问题。 First, its sooo slow, the time between request and response is around 15-30 seconds. 首先,它太慢了,请求和响应之间的时间大约为15-30秒。 Second, sometime this error occurs: 其次,有时会发生此错误:

Fatal error: Maximum execution time of 30 seconds exceeded in C:\\xampp\\htdocs\\steammarket\\getCoins.php on line 6 致命错误:第6行的C:\\ xampp \\ htdocs \\ steammarket \\ getCoins.php中超过30秒的最大执行时间

How can I fix this problem with the performance issue. 我该如何解决性能问题。

Connect site slow. 连接网站速度慢。 First php code set_time_limit(0); 第一个php代码set_time_limit(0); or ini_set('max_execution_time', 300); ini_set('max_execution_time',300); //300 seconds = 5 minutes // 300秒= 5分钟

<?php
set_time_limit(0);
$name = $_POST["item"];

$url = file_get_contents("http://www.tf2wh.com/allitems");
$dom = new DOMDocument();
@$dom->loadHTML($url);
$dom->saveHTML();

$code = "";
$xpath = new DOMXPath($dom);
foreach($xpath->query('//div[contains(attribute::class, "entry qual")]') as $e ) {
    $code .= $e->nodeValue;
}
$code = substr($code,strpos($code,$name)-30,30);
$code = explode("(",$code);
$coins = "";
for($i = 0; $i < strlen($code[0]); $i++){
    if(is_numeric($code[0][$i])){
        $coins .= $code[0][$i];
    }
}
echo $coins;

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

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