Jump to content

how to retrieve and echo a remote page?


angelcool

Recommended Posts

Hello community,

 

I want to retrieve and echo a remote web page using php (perhaps php as a client).

 

Example:

 

File to retrieve: http://10-network.net/angelcool.net/cisw21/index.htm

 

PHP file retriever: http://10-network.net/test.php

 

As you can see my cute face does not get retrieved  ::).

 

Is there any way possible to display the image? Perhaps using a different technique(cURL,fopen etc.)?

Another issue to note is the lack of CSS on the links.

 

code in php file:

 

<?php

 

$file = file_get_contents("http://10-network.net/angelcool.net/cisw21",'r');

 

echo $file;

?>

 

I was thinking a way to solve this is a function to string replace the relative paths for the CSS file and the image from relative to absolute in the PHP file retriever.

 

Any better suggestion will be apreciated.

 

I will apreciate any suggestions/help.

 

Thank you

 

Angel

 

Link to comment
Share on other sites

angelcool,

 

  When you right-click on your missing image and select Properties, it says that the image is: 10-network.net/yummy.jpg , but if you enter:

 

  http://10-network.net/yummy.jpg into a browser, you get 404: The requested URL /yummy.jpg was not found on this server.

 

  I would check test.php to see how you are building your <img element.

 

Scot L. Diddle, Richmond VA 

 

 

Link to comment
Share on other sites

I was thinking a way to solve this is a function to string replace the relative paths for the CSS file and the image from relative to absolute in the PHP file retriever.

 

That's what I would do. More specific, search for href="*" and src="*" (using regular expressions), and replace the paths with absolute paths.

 

This functions works for converting the paths:

 

<?php
// http://w-shadow.com/blog/2007/07/16/how-to-extract-all-urls-from-a-page-using-php/
function relative2absolute($absolute, $relative) {
        $p = @parse_url($relative);
        if(!$p) {
        //$relative is a seriously malformed URL
        return false;
        }
        if(isset($p["scheme"])) return $relative;

        $parts=(parse_url($absolute));

        if(substr($relative,0,1)=='/') {
            $cparts = (explode("/", $relative));
            array_shift($cparts);
        } else {
            if(isset($parts['path'])){
                 $aparts=explode('/',$parts['path']);
                 array_pop($aparts);
                 $aparts=array_filter($aparts);
            } else {
                 $aparts=array();
            }
           $rparts = (explode("/", $relative));
           $cparts = array_merge($aparts, $rparts);
           foreach($cparts as $i => $part) {
                if($part == '.') {
                    unset($cparts[$i]);
                } else if($part == '..') {
                    unset($cparts[$i]);
                    unset($cparts[$i-1]);
                }
            }
        }
        $path = implode("/", $cparts);

        $url = '';
        if($parts['scheme']) {
            $url = "$parts[scheme]://";
        }
        if(isset($parts['user'])) {
            $url .= $parts['user'];
            if(isset($parts['pass'])) {
                $url .= ":".$parts['pass'];
            }
            $url .= "@";
        }
        if(isset($parts['host'])) {
            $url .= $parts['host']."/";
        }
        $url .= $path;

        return $url;
}
?>

 

preg_replace() should be able to do it all in one run; untested code below. Also, what's the second parameter for, in your file_get_contents()?

 

<?php
$abs_url = 'http://10-network.net/angelcool.net/cisw21/index.htm';
$page = file_get_contents($abs_url);
$find = array(
'~href="(.+?)"~ise',
'~src="(.+?)"~ise'
);
$replace = array(
"'href=\"'.relative2absolute($abs_url, '\\1').'\"'",
"'src=\"'.relative2absolute($abs_url, '\\1').'\"'"
);
$page = preg_replace($find, $replace, $page);
echo $page;
?>

Link to comment
Share on other sites

Just tested the code and noticed a small error. Forgot to enclose $abs_url in quotes inside the $replace array. This works:

 

<?php
$abs_url = 'http://10-network.net/angelcool.net/cisw21/index.htm';
$page = file_get_contents($abs_url);
$find = array(
'~href="(.+?)"~ise',
'~src="(.+?)"~ise'
);
$replace = array(
"'href=\"'.relative2absolute('$abs_url', '\\1').'\"'",
"'src=\"'.relative2absolute('$abs_url', '\\1').'\"'"
);
$page = preg_replace($find, $replace, $page);
echo $page;
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.