natie769 Posted January 22, 2009 Share Posted January 22, 2009 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 More sharing options...
premiso Posted January 22, 2009 Share Posted January 22, 2009 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. Link to comment https://forums.phpfreaks.com/topic/142007-loading-and-styling-external-content/#findComment-743583 Share on other sites More sharing options...
natie769 Posted January 22, 2009 Author Share Posted January 22, 2009 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. Link to comment https://forums.phpfreaks.com/topic/142007-loading-and-styling-external-content/#findComment-743603 Share on other sites More sharing options...
premiso Posted January 22, 2009 Share Posted January 22, 2009 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. Link to comment https://forums.phpfreaks.com/topic/142007-loading-and-styling-external-content/#findComment-743606 Share on other sites More sharing options...
natie769 Posted January 22, 2009 Author Share Posted January 22, 2009 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. Link to comment https://forums.phpfreaks.com/topic/142007-loading-and-styling-external-content/#findComment-743621 Share on other sites More sharing options...
premiso Posted January 22, 2009 Share Posted January 22, 2009 <?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. Link to comment https://forums.phpfreaks.com/topic/142007-loading-and-styling-external-content/#findComment-743648 Share on other sites More sharing options...
natie769 Posted January 22, 2009 Author Share Posted January 22, 2009 That loads, but when a link is clicked it goes to a 404 again. The url is then: http://www.myaddress.com/theendoftheexternaladdress Link to comment https://forums.phpfreaks.com/topic/142007-loading-and-styling-external-content/#findComment-743671 Share on other sites More sharing options...
premiso Posted January 22, 2009 Share Posted January 22, 2009 <?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. Link to comment https://forums.phpfreaks.com/topic/142007-loading-and-styling-external-content/#findComment-743777 Share on other sites More sharing options...
natie769 Posted January 23, 2009 Author Share Posted January 23, 2009 That worked perfectly! Thank you! But for some reason though it doesn't work with the site I'm trying to link....bummer! Must be something to do with the site, because it works great with any other site. Link to comment https://forums.phpfreaks.com/topic/142007-loading-and-styling-external-content/#findComment-743998 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.