-2

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();
Youss
  • 4,196
  • 12
  • 55
  • 109
  • 1
    Would you be willing to explain the context? I'd be sad if I assisted you in doing something naughty. – George Cummins May 23 '13 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). – Youss May 23 '13 at 19:17
  • 1
    Does `$html->save()` return HTML data? If so, this may help: http://stackoverflow.com/q/6102636 – gen_Eric May 23 '13 at 19:24
  • Why are people downvoting..?? Very useful question..nothing on Google.. – Youss May 23 '13 at 19:52

2 Answers2

2

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()));
Community
  • 1
  • 1
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.

George Cummins
  • 28,485
  • 8
  • 71
  • 90
  • 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. – Youss May 23 '13 at 19:32
  • Please define "doesn't work." Do you get an error? Wrong output? What does `$html` contain? – George Cummins May 23 '13 at 19:32
  • Im getting a 'blank' iframe with nothing in it – Youss May 23 '13 at 19:33
  • Are you aware that the `src` attribute should have only a URL, not HTML? Your question is unclear about whether `$html` contains scraped data or a URL. – George Cummins May 23 '13 at 19:34
  • $html contains scraped data, I already mentioned this in my question:) – Youss May 23 '13 at 19:38
  • Then you don't need an iframe. You need some other container to display your data. I'll update my question with details. – George Cummins May 23 '13 at 19:40
  • I would really like (just) the Iframe if its possible..It has to do with my project.. – Youss May 23 '13 at 19:42
  • 1
    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. – George Cummins May 23 '13 at 19:43
  • I would like to keep the "content" clean and intact with head/body and everything. If I use a div the whole markup gets destroyed, you would get something like this
    content and so on...not good for my project..If its not possible, to bad:( I will accept your answer if nothing else comes along because I would assume it is not possible like you mentioned
    – Youss May 23 '13 at 19:47
  • 1
    A browser cannot properly render multiple `` or `` elements. If you want to display the HTML as-is, you can dump the contents in a ` – George Cummins May 23 '13 at 19:50
  • Thanks for your effort and time:) And thanks for ` – Youss May 23 '13 at 19:54