3

I'm trying to get links from the site.

"http://www.perfumesclub.com/es/perfume/mujer/c/"

For this use "user-agent" in simple html Sun.

But I get this error ..

Fatal error: Call to a member function find() on string in C:\Users\Desktop\www\funciones.php on line 448

This is my code:

Thanks^^

$url = 'http://www.perfumesclub.com/es/perfume/mujer/c/';

$option = array(
        'http' => array(
            'method' => 'GET',
            'header' => 'User-Agent: Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)',
        )
);
$context = stream_context_create($option);
$html = new simple_html_dom();
$html = file_get_contents ($url, false, $context);


$perfumes = $html->find('.imageProductDouble');  --> this is line 448

foreach($perfumes as $perfume) {
            // Get the link

            $enlaces = "http://www.perfumesclub.com" . $perfume->href;
            echo $enlaces . "<br/>";
}

2 Answers 2

2

wrap your file_get_contents in str_get_html function

// method 1
$html = new simple_html_dom();
$html->load( file_get_contents ($url, false, $context) );
// or method 2
$html = str_get_html( file_get_contents ($url, false, $context) );

you're creating a new dom and assigning it to the variable $html, than reading the url returning the string and setting it to $html, thus overwriting your simple_html_dom instance, so when your invoking the find method you have a string instead of an object.

Sign up to request clarification or add additional context in comments.

1 Comment

It is true. I had not occurred to me. Thank you so much.
2

$html is a string after the call to file_get_contents. Try

$html = file_get_html($url);

OR use

$html = str_get_html($html);

after the call to file_get_contents.

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.