1

I am trying to get some webpage content using php curl.

I have this php code:

$target_url="http://me.dealintech.com/partho.php";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $target_url);
curl_setopt($ch, CURLOPT_HEADER, 0);
$file_contents=curl_exec($ch);
include('simple_html_dom.php');
$html = str_get_html($file_contents); 
$elem = $html->find('div[id=basic_info_content]');
echo "var_dump output: ";
var_dump($elem);

And the output I get is :

[ here I get the whole webpage contents I targeted ]

var_dump output: array(0) { }

So I am getting all contents but not the specific div content. How I can get that using div id or/and class

3
  • add $html value in question Commented Dec 30, 2015 at 6:38
  • Just adding to @AmitRajput comment, echo $html; please. Does that div you are looking at show? Commented Dec 30, 2015 at 6:40
  • Yes. output of echo $html; is very large string. Rather you see dealintech.com/a.php which shows the output you are asking for. Commented Dec 30, 2015 at 8:51

1 Answer 1

2

You need to use the CURLOPT_RETURNTRANSFER option to make curl_exec return the contents of the URL rather than output it.

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

If you're not using any other cURL options, you could instead use the simpler file_get_contents:

$file_contents = file_get_contents($target_url);

And Simple HTML DOM Parser includes a function that does this automatically for you:

$html = file_get_html($target_url);
Sign up to request clarification or add additional context in comments.

6 Comments

that almost answered my question. But I am looking for specific div content. Can you say,how?
In my target page there's a div id=basic_info_content. I want to get text of that div. (Which I said in my question). Please give an idea
That part of your code should work fine. The only problem was the way you were getting the HTML.
You can also write it as $elem = $html->find("#basic_info_content").
output of $elem = $html->find("#basic_info_content"); print_r($elem); is very large. You can see here the output dealintech.com/a.php
|

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.