I have been googling for an hour on this and thought I would see if someone had a quick answer.
I have a site running on a hosted cms and have just created a wordpress blog running on a subdomain on a new server. I do not have access to source code of hosted (business catalyst) CMS.
I would like to load the CMS navigation in to my wordpress theme and was considering using jQuery but ajax seems to have a same domain policy for loading content. From what I have read so far the way around this is with a php proxy loading the external content. Without diving in to this I can see there being a problem with the navigation links having relative urls to the original domain so they will not work on blog.domain.com. I do not want to have to create redirects for all of the possible urls on the new server as the CMS navigation will be updating regularly.
How can I load the navigation contents using php to create the proxy but automatically change the relative url's to absolute urls to the original domain? + there will be one url in the navigation that will need to stay the same and tha开发者_Go百科t is the one pointing to the subdomain blog.domain.com
Thanks in advance for your help.
How about using iframe instead of ajax? Then you no need to deal with the crossdomain problem and relative path.
Hi Found this great article on HTML Parsing and Screen Scraping with the Simple HTML DOM Library:
http://net.tutsplus.com/tutorials/php/html-parsing-and-screen-scraping-with-the-simple-html-dom-library/
I have managed to parse the navigation by targeting the div id.
Now I have 1 last issue. The urls are realtive to the other server. How can I add the full absolute url: www.domain.com in front of all relative urls? Here's my code so far:
<?php
include_once('simple_html_dom.php');
$html = file_get_html('http://www.domain.com/');
foreach($html->find('div#cat_514340_divs') as $e)
echo $e->innertext . '<br>';
?>
Ok, I'm going to answer my own questions (s) in case someone else runs in to the same scenario.
Simple HTML DOM Library will let you parse the navigation by targeting the div id:
http://net.tutsplus.com/tutorials/php/html-parsing-and-screen-scraping-with-the-simple-html-dom-library/
You can then parse your navigation directly in to your php template:
<?php
include_once('simple_html_dom.php');
$html = file_get_html('http://www.domain.com/');
foreach($html->find('div#cat_514340_divs') as $e)
echo $e->innertext;
?>
And add absolute urls to each href with jQuery:
jQuery(document).ready(function($) {
/* Add absolute urls to top navigation */
$('#nav a').each(function() {
var href = $(this).attr('href');
$(this).attr('href', 'http://www.originalsitedomain.com' + href);
});
});
精彩评论