-3

Im scraping some websites and saving the data and then echo the data like this:

echo $html->save();

The $html part is a variable which holds the complete website. I would like to echo the complete page in iframe, usually I would put a source src="file.php?url=http://somewebsite.com" How do I echo the variable instead including the Iframe:

echo '<iframe src="$html->save()"></iframe>'; 

Is this somewhat how its done, or am I completely of?

Im using simple-html-dom and CURL like this:

require_once('simple_html_dom.php');
require_once('url_to_absolute.php');



$url = $_GET['url'];

    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $curl_scraped_page = curl_exec($ch);

    $html = new simple_html_dom();
    $html->load($curl_scraped_page, true, false);
    


foreach($html->find('a') as $a) {
    $a->href = url_to_absolute($url, $a->href);
}






echo $html->save();

$html->clear();
4
  • 1
    Would you be willing to explain the context? I'd be sad if I assisted you in doing something naughty. Commented May 23, 2013 at 19:14
  • @George Cummins There is nothing naughty about scraping. Im scraping a news website (legal) and would like to show the content in iframe. However instead of having a regular iframe inside a HTML page I would rather echo the iframe (with its content). Commented May 23, 2013 at 19:17
  • 1
    Does $html->save() return HTML data? If so, this may help: stackoverflow.com/q/6102636 Commented May 23, 2013 at 19:24
  • Why are people downvoting..?? Very useful question..nothing on Google.. Commented May 23, 2013 at 19:52

2 Answers 2

3

Create a file called magicalbuffer.php. Make all your saves and craws on it, and on iframe:

<iframe src="magicalbuffer.php?parameterToPassToScrapper=valueToPassToScrapper"></iframe>

OR

According with that answer

printf('<iframe src="data:text/html;base64,%s"></iframe>', base64_encode($html->save()));
Sign up to request clarification or add additional context in comments.

4 Comments

I would like to echo the iframe (not a php file with parameter inside Iframe)
Inside that file you should echo your HTML.
Thank you but Im getting 'some weird plain text' like this ++ ++ ++ ++ ++ ++ ++ ++ ++ ++var+context++=+{ ++++'dc_params'+:+'', ++++'category'+:+'home', ++++'stir_webdir'+:+'voorpagina', ++++'article_id'+:+'', ++++'article_name'+:+'', ++++'article_author'+:+'' ++}; Definitely not the html page Im scraping:)
I did a mistake within data type, so i've updated with base64 :P
1

To echo scraped data, don't use an iframe. Instead, echo the data into an appropriate container such as a div:

echo '<div>' . $html->save() . '</div>';

If you wish to avoid encapsulating your HTML in PHP, do:

?>
<div><?php echo $html-save(); ?>"></div>
<?php

This is often cleaner and easier to troubleshoot, especially if you are producing a lot of HTML in your script.

11 Comments

Thanks for the input but the first and second code don't work. I can't use the last code in my project. I have updated my question.
Please define "doesn't work." Do you get an error? Wrong output? What does $html contain?
Im getting a 'blank' iframe with nothing in it
Iframes are not suitable for echoing scraped content. They can only take a URL parameter. The URL is then loaded by the browser at render time.
A browser cannot properly render multiple <html> or <body> elements. If you want to display the HTML as-is, you can dump the contents in a <textarea>. If you want to render the page, you can use an iframe but need to specify the URL as the src attribute rather than scraping the data. This prompts the browser to begin rendering the content of the iframe as a separate document.
|

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.