Posted on

Include HTML content from external url in Magento2

I have HTML file which is hosted on other server.

http://testsite.com/test/index.html

which has the content like this(As a example)

<!DOCTYPE HTML>
<html>
<head>
<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>
</head>
<body>
<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>
</div>
</div>
</div>
</div>
</body></html>

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

So I tried in following way.

app/code/Vendor/Module/view/frontend/layout/default.xml

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

app/code/Vendor/Module/Block/Index.php

<?php
namespace VendorModuleBlock;
class Index extends MagentoFrameworkViewElementTemplate
{
public function getCustomFooter()
{
$url = ‘http://testsite.com/test/index.html’;
$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);
if(curl_exec($ch)){
$footer = curl_exec($ch);
$footer = preg_replace(‘#<script(.*?)>(.*?)</script>#is’, ”, $footer));
$footer = trim(preg_replace([
# Strip tags around content
‘/<(.*)doctype(.*)>/i’,
‘/<(.*)html(.*)>/i’,
‘/<(.*)head(.*)>/i’,
‘/<(.*)body(.*)>/i’,
], ”, $footer));
return $footer;
}
}
}

app/code/Vendor/Module/view/frontend/templates/customfooter.phtml

<?php
echo $block->getCustomFooter();
?>

app/code/Vendor/Module/view/frontend/templates/before_body.phtml

<?php
$scopeConfigInterface = MagentoFrameworkAppObjectManager::getInstance()- >get(MagentoFrameworkAppConfigScopeConfigInterface::class);
$customjs = $scopeConfigInterface->getValue(“customfooter/general/footercontent”,
MagentoStoreModelScopeInterface::SCOPE_STORE); // This returns <script src=”http://testsite.com/test/js/custom.js” 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 *