开发者

load part of an external site using curl?

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!


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).


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;


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜