Question

After searching a lot and goggling lots of apis I ma having below issue using php to get the contents of a div.

And also it is not returning proper content of that div if i use any alternate parsing apis.

Could u help below is the code

<?php
$curl = curl_init();
$headers[] = "Accept: */*";
$headers[] = "Cache-Control: max-age=0";
$headers[] = "Connection: keep-alive";
$headers[] = "Keep-Alive: 300";
$headers[] = "Accept-Charset: utf-8;ISO-8859-1;iso-8859-2;q=0.7,*;q=0.7";
$headers[] = "Accept-Language: en-us,en;q=0.5";
$headers[] = "Pragma: "; // browsers keep this blank.
@curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
@curl_setopt($curl, CURLOPT_VERBOSE, false);
@curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
@curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
@curl_setopt($curl, CURLOPT_ENCODING, 'gzip,deflate');
@curl_setopt($curl, CURLOPT_AUTOREFERER, true);
@curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
@curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
@curl_setopt($curl, CURLOPT_HEADER, false);
@curl_setopt($curl, CURLOPT_TIMEOUT, 1000);
@curl_setopt($curl, CURLOPT_URL, "http://www.hindishows.com/cid-sony/case-of-a-lawyers-mysterious-death-episode-1/1027.html");
$page = curl_exec($curl);

//$dom = HTML5_Parser::parse($page); 
//var_dump($dom->saveXml()); 

$dom = new DOMDocument();
$dom->resolveExternals = true;
@$dom->load($page);


        $finder = new DomXPath($dom);
        $elements = $finder->query("//div[@id='yt-video-box']");
        echo "null:".is_null($elements);
if (!is_null($elements)) {
    foreach ($elements as $element) {
        print(var_dump($element->saveXML()));
    }
}
?>

Output is null: Done

It should output the proper contants of that div.

Also asked the same question at http://www.find4answers.com/483/parsing-issue-when-html-contains-javascript

Could you help me. Thanks.

Was it helpful?

Solution

I will go for SimpleHTML way

Something like this:

include_once('libs/simple_html_dom.php');
dm = new DOMDocument();
$dm = file_get_html($item->link);
foreach ($dm->find('div') as $element) {
$tmpInnerText = $element->innerText();
echo $tmpInnerText.PHP_EOL;
            }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top