0

Here is my code:

<?php
    ini_set('display_errors', 1);
    ini_set('display_startup_errors', 1);
    error_reporting(E_ALL);

    $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  = $xpath->query('//span[@id="ProductName"]')->item(0)->nodeValue;

    echo $name;

?>  

I am trying to parse the text placed in element <span id="ProductName">TEXT here</span> but i receive error:

Notice: Trying to get property of non-object in /home/sportsdi/public_html/test.php on line 28

Can you please tell me where is my mistake, i am stuck on this for hours ? How can i get the text in this element ?

Thanks in advance!

1
  • your var_dupm($xpath) shows :- object(DOMXPath)#2 (1) { ["document"]=> string(22) "(object value omitted)" } means no data retrieve. Commented Mar 9, 2016 at 18:37

1 Answer 1

1

$str is a string, not a file.

Use:

$doc->loadHTML( $str );

instead of:

$doc->loadHTMLFile( $str );

By your code, ->loadHTMLFile() fails, consequently also DOMXpath initialization — based on DOMDocument — fails.

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.