• 4
name

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191

Backtrace:

File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

name Punditsdkoslkdosdkoskdo

file_get_contents not returning entire webpage

I've been trying to retrieve the contents of a webpage (http://3sk.tv) using file_get_contents. Unfortunately, the resulting output is missing many elements (images, formating, styling, etc...), and just basically looks nothing like the original page I'm trying to retrieve.

This has never happened before with any other URLs I have tried retrieve using this same method, but for some reason, this particular URL (http://3sk.tv) refuses to work properly.

The code I'm using is:

<?php
$homepage = file_get_contents('http://3sk.tv');
echo $homepage;
?>

Am I missing anything? All suggestions on how to get this working properly would be greatly appreciated. Thank you all for your time and consideration.

I have one quick workaround to fix relative paths

http://www.w3schools.com/tags/tag_base.asp

Just add to your code <base> tag.

<?php
$homepage = file_get_contents('http://3sk.tv');
echo str_replace(
   '<head>', 
   '<head><base href="http://3sk.tv" target="_blank">',
    $homepage
);
?>

It's should help.

  • 4
Reply Report
      • 1
    • Hi @jQuery00, tried using your suggested method, there was some improvement in the final output (images in the body appeared) but still many elements of the CSS & styling are missing. Any suggestions would be highly appreciated. Thanks

This is to be expected. If you look at the source code, you'll notice many places which do not have a full URL (ex lib/dropdown/dropdown.css). This tells the browser to assume http://3sk.tv/lib/dropdown/dropdown.css. However, on your website, it will be YOURURL.COM/lib/dropdown/dropdown.css, which does not exist. This will be the case for much of the content.

So, you can't just print another website's source and expect it to work. It needs to be the same URL.

The best way to embed another website is usually to just use an iframe or some alternative.

  • 3
Reply Report

The webpage is not completely generated server-side, but it relies heavily on JavaScript after the HTML part loads. If you are looking for rendering the page as it looks in browser, you may need a headless browser instead - see e.g. this binding to PhantomJS: http://jonnnnyw.github.io/php-phantomjs/

  • 2
Reply Report
      • 2
    • "brace yourself" <= good to know thanx. I'm currently testing the solution you suggested (just waiting for phantomjs to build... long process) will let you know how this went soon as its done.

Warm tip !!!

This article is reproduced from Stack Exchange / Stack Overflow, please click

Trending Tags

Related Questions