0

I am trying to get the coin data of this website: 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. Second, sometime this error occurs:

Fatal error: Maximum execution time of 30 seconds exceeded in C:\xampp\htdocs\steammarket\getCoins.php on line 6

How can I fix this problem with the performance issue.

11
  • Have you checked whether they have an API, so you don't have to scrape the web page? Commented Dec 27, 2014 at 13:32
  • Did you check what line is slow? Use some profiling tool? Or print a timestamp? Commented Dec 27, 2014 at 13:34
  • @Barmar No there isn't an API to get the coin data. Commented Dec 27, 2014 at 13:40
  • @LajosVeres I don't know :), i am new in php and have no idea how i should handle this problem. Commented Dec 27, 2014 at 13:41
  • Avg time to load that site is already 2 - 3 secs Commented Dec 27, 2014 at 13:46

1 Answer 1

1

Connect site slow. First php code set_time_limit(0); or ini_set('max_execution_time', 300); //300 seconds = 5 minutes

<?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;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.