Score:-1

How to include remote web page

ms flag

I have a simple problem but can't find a simple solution. I want to include remote (dynamically created) web page into Drupal generated page. Previously i used PHP filter module in Drupal, so i would write something like readfile("remoteURL") in CKeditor and that worked fine. Now, i'm searching for something more elegant and more secure but i just can't find it. Anybody knows? Thx.

Regards.

in flag
Iframes might be the safer route. You wouldn't want to "include" arbitrary HTML into your page, you don't know what scripts or resources that HTML contains. And you definitely don't want that HTML's JavaScript running in the context of your domain. Alternatively, if that remote webpage has an API that gets you the data you need, I suggest you use that data and craft an interface for it yourself.
orcanic avatar
ms flag
I forgot to mention that i would not like to use an iframe. Anther site is under my control so there are no worries about unknown content/scripts. I just want to display specific pages from that site on a Drupal site too. I think this shouldn't be such a problem.
ru flag
I-Frame is the only simple solution. Per HTML specification you can't nest multiple HTML pages inside each other, there must be only one HTML tag per page. Anything else requires coding a a JS widget, APIs,... there is no standard solution except I-Frame.
orcanic avatar
ms flag
Well, it shouldn't be so hard to make a module that could get the page via cURL, parse it in the way that head tags are stripped and only body left. I'm talking about simple pages that have one table filled with data and that's it. No javascript, css etc. Suppose you publish a simple table with a week menu in a "print" form (no CSS) on one site and you just want to include that table in a Drupal one. I just wanted to see if such module exists. It seems it doesn't so i will stick to PHP filter and use readfile :-)
Kevin avatar
in flag
The answer in this case is iframe. Not sure why you'd want to use curl and deal with stripping etc. This is pretty much the use case for iframes. https://www.drupal.org/project/ckeditor_html_embed
ru flag
It is not hard, it just is a very bad idea. It has numerous issues (JS or CSS files in header, network connectivity,...) and a very simple, robust and flat out better alternative with I-Frames.
Score:0
cn flag

Add a preprocess hook to your theme and parse the DOM of the external page for the HTML you want to include:

mytheme.theme

use Drupal\Component\Utility\Html;

/**
 * Implements hook_preprocess_HOOK() for node templates.
 */
function mytheme_preprocess_node(&$variables) {
  $url = 'www.example.com';
  $response = \Drupal::httpClient()->get($url);
  $dom = Html::load((string) $response->getBody());
  $xpath = new \DOMXPath($dom);
  $nodes = $xpath->query('/html/body/div');
  foreach ($nodes as $node) {
    $external_content = $dom->saveHTML($node);
    $variables['content']['external'][] = ['#markup' => $external_content];
  }
}
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.