1

I'm trying to pick out content from a div in an external html file. Here's the html code

<some html>
    {<div id="responseDiv" style="display:none">




    required content
    </div>
</some html>

Here's the php code I'm using

include_once('simple_html_dom.php');
$curl_h = curl_init('http://www.example.com/');

curl_setopt($curl_h, CURLOPT_HTTPHEADER,
array(
    'User-Agent: NoBrowser v0.1 beta',
)
);

curl_setopt($curl_h, CURLOPT_RETURNTRANSFER, true);

$handle = curl_exec($curl_h);

$html = str_get_html('$handle');
$ret = $html->find('div[id=DivID]'); 
   foreach ($ret as $post)
  {
    echo $post->outertext;
      }

I check around and found that $ret itself is an empty array. I have tried playing around with other div IDs etc but all to the same result. What am I doing wrong??

0

2 Answers 2

2

This:

$html = str_get_html('$handle');

should be:

$html = str_get_html($handle);
                     ^--   ^-- no quotes

The ' turn it into a string, which doesn't interpolate variables. So you're feeding the literal text $, h, a, etc... as your html document, NOT the html you just retrieved via curl.

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

2 Comments

Thanks for pointing that out, I have made the changes, however even now when I try echoing $post->outertext it displays a blank page(just like before). Initially I had var_dumped #ret that was how I had found out that it was an empty array. Now when I try a var_dump on $ret it crashes the browser.
Oh never mind, I figured it out. I should have echoed plaintext instead of outertext. Works fine now!! thanks again
0

Is ajax an option for you?

If so:

    $.ajax({
        type:"GET",
        url:"path/to/file.html",
        dataType:"html",
        success:function(data) {
            var out = "";
            $(data).find("#div.id.you.want.to.fetch").each(function(loop, item){
                out += $(item).html();
            });
            data = out;
            $("#responseDiv").html(data);
        },
        error:function() {
            alert("Error");
        }
    });

The .each() is there so you can use it with class instead of ID

1 Comment

I'm still a learner and am still learning PHP. I haven't gotten into ajax yet, else I'd definitely give it a try. Thanks for your reply though. :)

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.