Jump to content

Recommended Posts

I'm running this script inside a div.  What I'm trying to accomplish is loading content from an external site, and also styling it from my own style sheet.  It loads and styles the content perfectly, but if I click a link inside the external page, it takes me to a 404 page.  Is there anyway to navigate through the external site?  The reason I need this is because I'm loading an equipment list for my client, and there is lots of click-through.

 

Here is the code:

 

<div style="border:1px solid black; color:red" id="content area">

 

<?php

 

 

function get_data($url) { 

  $ch = curl_init(); 

  $timeout = 5; 

  curl_setopt($ch,CURLOPT_URL,$url); 

  curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); 

  curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout); 

  $data = curl_exec($ch); 

  curl_close($ch); 

  return $data; 

 

$returned_content = get_data('http://www.externalsitehere.com');

 

echo $returned_content;

?>

 

</div>

 

 

Any help appreciated! :)

Link to comment
https://forums.phpfreaks.com/topic/142007-loading-and-styling-external-content/
Share on other sites

You can always use the base tag in html, doubt that this is valid html, but it should work, this allows assumes there is a </head> tag on the external site:

 

<?php


function get_data($url) { 
  $ch = curl_init(); 
  $timeout = 5; 
  curl_setopt($ch,CURLOPT_URL,$url); 
  curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); 
  curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout); 
  $data = curl_exec($ch); 
  curl_close($ch); 
  return $data; 
} 

$returned_content = get_data('http://www.externalsitehere.com');

echo str_replace("</head>", '<base href="http://www.externalsitehere.com"></head>', $returned_content);
?>

 

Should take care of the urls, however the stylesheet may catch on to this also, so yea.

Thanks for the quick response!

That did allow me to click a link, but once I click the link the content doesn't remain inside the div, but just opens up the external page.

 

Well you will need a script and re-code all the links to goto your site and re-parse it each time.

 

 

 

Basically you would call the current script and have the "get_data($_GET['url'])" then you will have to do that str_relpace and instead of adding just the external site, you would do something like:

 

echo str_replace("</head>", '<base href="http://www.yoursite.com/script.php?url=http://www.externalsitehere.com"></head>', $returned_content);

 

Then make sure you check the $_GET data before parsing it cause it could allow someone to manually change that.

I'm not very good at PHP, so I'm sorry if I have this wrong!

This is what i have for my script now:

 

<?php

 

 

function get_data($url) {

  $ch = curl_init();

  $timeout = 5;

  curl_setopt($ch,CURLOPT_URL,$url);

  curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);

  curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);

  $data = curl_exec($ch);

  curl_close($ch);

  return $data;

}

 

$returned_content = get_data('http://www.externalsitehere.com');

 

echo str_replace("</head>", '<base href="http://www.yoursite.com/script.php?url=http://www.externalsitehere.com"></head>', $returned_content);

?>

 

And it says address not found.

<?php


function get_data($url) {
  $ch = curl_init();
  $timeout = 5;
  curl_setopt($ch,CURLOPT_URL,$url);
  curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
  curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
  $data = curl_exec($ch);
  curl_close($ch);
  return $data;
}

$url = "http://www.externalsitehere.com";
if (isset($_GET['url'])) {
    if (stristr($url, $_GET['url']) !== false) {
         $url = $_GET['url'];
   }
}

$returned_content = get_data($url);

echo str_replace("</head>", '<base href="http://www.yoursite.com/script.php?url=http://www.externalsitehere.com"></head>', $returned_content);
?>

 

Give that a try, and make sure to replace the external site and the yoursite portions.

<?php
function get_data($url) {
  $ch = curl_init();
  $timeout = 5;
  curl_setopt($ch,CURLOPT_URL,$url);
  curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
  curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
  $data = curl_exec($ch);
  curl_close($ch);
  return $data;
}
$main_url = "http://www.phpfreaks.com/";
$url = $main_url;
if (isset($_GET['url'])) {
    if (stristr($main_url, $_GET['url']) !== false) {
         $url = $_GET['url'];
   }
}

$returned_content = get_data($url);

echo str_replace('a href="/', 'a href="http://www.yoursite.com/script.php?url=' . $main_url, $returned_content);
?>

 

I tested that on my server and it worked great. Given that the links are relative and not absolute.

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.