1

Here is my code:

<?php
    $url = "http://www.sportsdirect.com/adidas-goletto-mens-astro-turf-trainers-263244?colcode=26324408";   

    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_SSLVERSION, 3);
    curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10);
    curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
    curl_setopt($curl, CURLOPT_VERBOSE, true);
    $str = curl_exec($curl);  
    curl_close($curl);  


    libxml_use_internal_errors(true); 
    $doc = new DOMDocument();
    $doc->loadHTMLFile($str);

    $xpath = new DOMXpath($doc);

    $name  = $doc->query('//span[@id="ProductName"]')->item(0)->nodeValue;

    echo $name;

?>  

I am trying to get the name of the product but i receive:

Fatal error: Call to undefined method DOMDocument::query() in /public_html/test.php on line 24

Can you please help me out resolve my problem and find my mistake because i am stuck on this problem for hours?

I just want to get the name of the product which is contained in span element with id ProductName.

Thanks in advance!

Venelin
  • 2,905
  • 7
  • 53
  • 117

1 Answers1

-1

You can get the desired result by using PHP regexes:

<?php
$url = "http://www.sportsdirect.com/adidas-goletto-mens-astro-turf-trainers-263244?colcode=26324408";   

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSLVERSION, 3);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
curl_setopt($curl, CURLOPT_VERBOSE, true);
$str = curl_exec($curl);  
curl_close($curl);

preg_match_all("/id=\"ProductName\"(.+?)>(.+?)</", $str, $str_out);

$name = $str_out[2][0];

echo $name;
?>

This works using preg_match_all() to extract the product name.

C.Liddell
  • 1,084
  • 10
  • 19