Displaying a requested URL

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

After you've captured the data, your going to have to change the instances of

http://theirdomain.com/ to http://yourdomain.com?request=http://theirdomain.com

When you would pass the request variable to your curl function so it knows which page to load. Of course your going to have to ignore style sheets, images, etc.. which is why I said a clever regex. Try posting in the Regex Forum for more help.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

you could always use fsockopen() and spoof the headers with fwrite() so the page will think it's a "real browser" fetching the info. I wrote a function a while back that does just this...if you want I'll dig it up and post it here.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Burrito wrote:you could always use fsockopen() and spoof the headers with fwrite() so the page will think it's a "real browser" fetching the info. I wrote a function a while back that does just this...if you want I'll dig it up and post it here.
Cool! Post it ;)
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

ask and you shall receive 8)

Code: Select all

<?php
/****************************************************************
/*			phphttp function by Burrito and Twindagger			 
/*						Modify at will							 
/*					Last Modified 04/22/2004					 
/***************************************************************/

//$urlwhere must be like http://www.somehost.com/somedir/somedoc.html
function phphttp($urlwhere, $posts = FALSE, $referer = "http://www.somedomain.com/")
{
  if ($posts === FALSE && ($ret = @file($urlwhere)) !== FALSE)
  {
    return implode("", $ret);
  }
  else
  {
    $method = "GET";
    if ($posts)
      $method = "POST";
    $tmp = explode("/", $urlwhere);
    $host = $tmp[2];
    $target = str_replace("http://" . $host, "", $urlwhere);
    if (strlen($target) == 0)
      $target = "/";
    $request  = "$method $target HTTP/1.1\r\n";
    $request .= "Host: $host\r\n";
    $request .= "User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.0.3705)\r\n";
    $request .= "Keep-Alive: 300\r\n";
    $request .= "Connection: Close\r\n";
    $request .= "Referer: $referer\r\n";
    $request .= "Cache-Control: max-age=0\r\n";
    
    if ( $method == "POST" ) 
    {
      $length = strlen( $posts );
      $request .= "Content-Type: application/x-www-form-urlencoded\r\n";
      $request .= "Content-Length: $length\r\n";
      $request .= "\r\n";
      $request .= $posts;
    }
    else
      $request .= "\r\n";
    
  $header = "";
  $response = "";
  
  // connect
    if (!($socket=fsockopen($host,80,$errno,$errstr))) return $errstr;
    else {
       socket_set_timeout($socket,10);
       // send request
       fwrite($socket,$request);
       // get header
       do
       {
         $header = "";
         do $header.=fread($socket,1); while (!preg_match('/\\r\\n\\r\\n$/',$header));
       } while (strpos($header, "HTTP/1.1 1") === 0);
       //echo "<pre>" . $header . "</pre><br>";
       // check for chunked encoding
       if (preg_match('/Transfer\\-Encoding:\\s+chunked\\r\\n/',$header))
         do {
             $byte = "";
             $chunk_size="";
             do {
               $chunk_size.=$byte;
               $byte=fread($socket,1);
             } while ($byte!="\r");      // till we match the CR
             fread($socket, 1);        // also drop off the LF
             $chunk_size=hexdec($chunk_size); // convert to real number'
             if ($chunk_size > 2048)
             {
               while ($chunk_size > 0)
               {
                 $size = ($chunk_size > 2048 ? 2048 : $chunk_size);
                 $response.=fread($socket, $chunk_size);
                 $chunk_size -= $size;
               }
             }
             else
               $response.=fread($socket,$chunk_size);
             fread($socket,2);          // ditch the CRLF that trails the chunk
         } while ($chunk_size);        // till we reach the 0 length chunk (end marker)
       else {
         // check for specified content length
         if (preg_match('/Content\\-Length:\\s+([0-9]*)\\r\\n/',$header,$matches)) {
             if ($matches[1] > 2048)
             {
               $c_size = $matches[1];
               while ($c_size > 0)
               {
                 $size = ($c_size > 2048 ? 2048 : $c_size);
                 $response.=fread($socket, $c_size);
                 $c_size -= $size;
               }
             }
             else
               $response=fread($socket,$matches[1]);
         } else {
             // not a nice way to do it (may also result in extra CRLF which trails the real content???)
             while (!feof($socket)) $response .= fread($socket, 2048);
         }
       }
       // close connection
       fclose($socket);
    }
    return $response;
  }
}
/* example usage
echo phphttp("http://www.yahoo.com");
*/
?>
prejudged_fire
Forum Newbie
Posts: 15
Joined: Sun Dec 18, 2005 6:28 pm

Post by prejudged_fire »

ok you all understand what it is i am trying to do.. what would be the easiest and least complicated way of doing it? Iframe? curl?

I apologize for the newbieness..and for the delay in responses
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post by Maugrim_The_Reaper »

Well, you need to look at what exactly you're trying to do.

a) fetch the content yourself and display for user
b) act as a wrapper around another sites content.
c) some other behaviour not yet revealed to the forum.
prejudged_fire
Forum Newbie
Posts: 15
Joined: Sun Dec 18, 2005 6:28 pm

Post by prejudged_fire »

I have acomplished the first step (displaying the content) but how do i act as a wrapper so that the user stays on my website? in the easiest way...
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

use an iframe.
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post by Maugrim_The_Reaper »

As stated, an iframe is far simpler than using PHP. I mean if all your doing is putting another sites pages into a framed section of your site - do you really need PHP? Why not let the user's browser do all the legwork (and save yourself the extra bandwidth) unless you are actively manipulating that content in some (which incidentally can be interpreted in the wrong way by some people who think they alone have the right to manipulate pages they create).

Iframe makes a lot of sense unless there are specific needs it jus will not resolve...
prejudged_fire
Forum Newbie
Posts: 15
Joined: Sun Dec 18, 2005 6:28 pm

Post by prejudged_fire »

ok but how do i display whatever website the user inputs without php? An iframe is html, but in order to use the url variable i need php right?
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

you'd have to use JS to change the src attribute of the iframe.
prejudged_fire
Forum Newbie
Posts: 15
Joined: Sun Dec 18, 2005 6:28 pm

Post by prejudged_fire »

and..how do i do that?? guys please remember I am a noob to all of this
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

something like this should work:

untested:

Code: Select all

<script>
function changeIt()
{
	var val = document.MyForm.page.value;
	document.getElementById('fr').src = val;
}
</script>

<form name="MyForm">
<input type="text" name="page"><input type="button" value="Update Page" onClick="changeIt()">
</form>
<iframe src="http://www.yahoo.com" width="500" height="500" id="fr"></iframe>
prejudged_fire
Forum Newbie
Posts: 15
Joined: Sun Dec 18, 2005 6:28 pm

Post by prejudged_fire »

*sighs* aparently i am asking too much.. I will simply go through every tutorial ever written on php and javascript and html and every other <span style='color:blue' title='I&#39;m naughty, are you naughty?'>smurf</span> computer language ever written
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

No one said you were asking too much...did you try the code I posted above?

I just tested it for the halibut and it works just as you want.... 8O
Post Reply