Posted on

Include HTML content from external url in Magento2

I have HTML file which is hosted on other server.

which has the content like this(As a example)

<meta http-equiv=”content-type” content=”text/html; charset=UTF-8″/>
<link rel=”stylesheet” href=”css/custom.css” type=”text/css”>
<script src=”js/custom.js”></script>
<div class=”container”>
<div class=”homepage”>
<div id=”testdiv” class=”test-container”>
<div id=”text”>
<div class=”center”><h2>Welcome page</h2>
<h3>Custom text goes here…</h3>

I have to integrate the above HTML code with functionality in magento footer

So I tried in following way.


<?xml version=”1.0″?>
<page layout=”3columns” xmlns:xsi=”” xsi:noNamespaceSchemaLocation=”urn:magento:framework:View/Layout/etc/page_configuration.xsd”>
<referenceContainer name=”before.body.end”>
<block class=”MagentoFrameworkViewElementTemplate”
template=”before_body.phtml” name=”before_body_js”/>
<referenceContainer name=”footer” remove=”true” />
<referenceContainer name=”footer-container”>
<block class=”VendorModuleBlockIndex” name=”custom_footer” before=”-” template=”customfooter.phtml”/>


namespace VendorModuleBlock;
class Index extends MagentoFrameworkViewElementTemplate
public function getCustomFooter()
$url = ‘’;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
$footer = curl_exec($ch);
$footer = preg_replace(‘#<script(.*?)>(.*?)</script>#is’, ”, $footer));
$footer = trim(preg_replace([
# Strip tags around content
], ”, $footer));
return $footer;


echo $block->getCustomFooter();


$scopeConfigInterface = MagentoFrameworkAppObjectManager::getInstance()- >get(MagentoFrameworkAppConfigScopeConfigInterface::class);
$customjs = $scopeConfigInterface->getValue(“customfooter/general/footercontent”,
MagentoStoreModelScopeInterface::SCOPE_STORE); // This returns <script src=”” src_type=”url”></script>
<?= /* @noEscape */ $customjs; ?>

In the admin I have an option to add javascript file src which will load before the body end tag .

The above code loads the HTML content properly in magento footer, but the js and other functionality is not working.

Please someone help me on this, How can I load the external HTML content with js and css files into the magento footer and content area.

Please suggest without requirejs-config.js approach, I have the requirement to achieve without that.

Thanks in Advance!!

Leave a Reply

Your email address will not be published. Required fields are marked *