tags:

views:

46

answers:

3

I'm creating a site for a university where we have to include the university's main navigation at the top of the page. Rather than re-create their navigation, I want to load it dynamically.

First I thought I could use an iframe and just trim it to the navigation.

However, when a link is clicked it opens in that frame, when i want it in a new window.

So, my new plan is to pull in the nav with curl (or another way if someone has a suggestion..). Whats the best way to go about this? Fairly new at curl so I'd love some code.

Thanks!

A: 

If you want to retrieve it using cURL, you can use this example from cURL's website: http://curl.haxx.se/libcurl/php/examples/simpleget.html.

Alternatively, you could use PHP's readfile function to read a file (local or external) directly into the page or the file_get_contents function to load it into a variable (then you can manipulate it before outputting it to the page).

jake33
Sorry i should say that I dont have access to the code on this server, I would need to strip it off the html site.
Wes
OK, then you can use the [readfile function](http://php.net/manual/en/function.readfile.php) to read a remote file into the page. This will be simpler and maybe even quicker than cURL. Also, you can use the [file_get_contents function](http://php.net/manual/en/function.file-get-contents.php) to read the URL into a variable instead of outputting it directly onto the page.
jake33
Updated answer.
jake33
A: 

I recently did this hack on a client's existing site.

Create a "layout view" in the university site that contains the header and nav you want. Make the body empty except for a flag that will tell your parser script where to insert the content.

<div class="nav">
    <!-- ... -->
</div>
<div class="body">
    <!-- Below is the flag -->
    {content} 
</div>

Use the following code to import the layout HTML, insert the page HTML into it, then output it

// Variables
$contentForLayout = '<p>This is the page content</p>'
$pageTitle = 'Test Page';
$layoutUrl = 'http://'.$_SERVER['HTTP_HOST'].'/unilayout'; // AFAIK, cURL like the host to be there

// Get layout HTML
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $layoutUrl);
curl_setopt($ch, CURLOPT_HEADER, 0);
ob_start();
curl_exec($ch);
curl_close($ch);
$layoutHtml = ob_get_contents();
ob_end_clean();

// Insert your page HTML into the layout HTML
$layoutHtml = str_replace('{content}', $contentForLayout, $layoutHtml);

// Insert your page title into the layout HTML
$layoutHtml = preg_replace('%<title>[^<]*</title>%si', '<title>'.h($pageTitle).'</title>', $layoutHtml);

// Output
print $layoutHtml;
mattalexx
A: 

You can try using this library http://github.com/shuber/curl and parse the navigation out of the curl response body using regex or an html parser

Sean Huber