4

So I've been trying to embed a part of a website within my website due to security reasons I cannot disclose the website that I am trying to embed so for the purpose of this example I am going to use bbc.co.uk.

The following is the php/html code:

<!DOCTYPE html>
<html lang="en">
<head>
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

</head>
<body>
<?php
$page = file_get_contents('http://www.bbc.co.uk/');
$doc = new DOMDocument();
$doc->loadHTML($page);
$divs = $doc->getElementsByTagName('div');
foreach($divs as $div) {
    // Loop through the DIVs looking for one withan id of "content"
    // Then echo out its contents (pardon the pun)
    if ($div->getAttribute('id') === 'orb-footer') {
         echo $div->nodeValue;
    }
}
?>  
</body>

<footer>

</footer>
</html>

However when I load the page I get left with a page displaying the php code. Can anyone tell me where I am going wrong please.

Thank you!!

14
  • Your code works perfectly fine for me. Which version of PHP are you using? Commented Feb 12, 2017 at 17:34
  • Hi that's weird I just get left with a blank page. Using PHP version 5.5.9. Commented Feb 12, 2017 at 17:38
  • @MohdAsimSuhail correction: I get left with a page displaying the php code. Commented Feb 12, 2017 at 17:39
  • 2
    'I get left with a page displaying the php code', this means your server is not rendering PHP code. Commented Feb 12, 2017 at 17:40
  • 1
    Does the file end in .php? If you try and run an html webpage with php, it won't work. Commented Feb 12, 2017 at 18:30

1 Answer 1

2

This is a general guide for solving PHP issues like this, and it's in no way a direct or specific "drop-in" solution for this answer

Check the loaded source on your browser after loading the page

If the page on the browser that you are loading is blank, try viewing the source using Ctrl+U on Chrome, or just right clicking anywhere on the page and choosing the option to view source.

Is the source empty? Then that must mean that the error is from PHP/server.

Is the source fully/partially loaded? That means the error must lie in the PHP code you wrote. Try doing $doc->loadHtml( htmlentities($page) ) instead.

Use a PHP debugger such as XDebug

Nowadays it's easy to use a PHP debugger seeing that it can easily be integrated with most PHP IDEs. Try stepping through the code line-by-line and check where the problem lies in.

Sign up to request clarification or add additional context in comments.

7 Comments

Thanks for this, will give it a go and get back to you.
Using $doc->loadHtml( htmlentities($page) ) did not work.
The php code cannot have an error as @MohdAsimSuhai tried it and it worked for him. Has to be a problem with the server.
How are you running the PHP server? XAMPP/LAMP/WAMP or other packages? Directly from the php CLI?
I will have to ask my administrator for that information as I did not set up this server.
|

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.