views:

87

answers:

4

How can include CodeIgniter content in a regular PHP page on the same server but not part of the CI app?

For example I'm am trying to load a header from CI into Wordpress. Whats the best way to include a CI controller (eg; index.php/mycontroller/header/) on the same server?

A: 

Have you considered using an iframe in your Wordpress page?

<iframe src="http://my-site-url/index.php/mycontroller/header"&gt;&lt;/iframe&gt;

It might be the simplest solution.

Stephen Curran
A: 

When you are trying to wrap content with HTML from an external source, this can easily be achieved by placing HTML comments (or other recognizable tags) in the target site, then using PHP to split/explode the content.

I have used this method to create several micro-sites for MSN Money which has in. Then I would simply use:

list($header_html) = explode('<!-- Header -->', file_get_contents($url));

It was slightly more complex than that, involving caching and all sorts of other madness, but at its base that is the method to use if WordPress will allow it.

Phil Sturgeon
+1  A: 

Use PHP's built-in file_get_contents(). Just make sure to use the full HTTP path, not a relative path. Example:

<?php
  file_get_contents('http://your.server.com/codeigniter-path/controller/');

That should do the trick.

njbair
+1  A: 

From http://codeigniter.com/forums/viewthread/88635/

This is overkill, as file_get_contents($url) or similar, would be better. However, it may work for your situation:

$CI_INDEX = '/path/to/your/codeigniter/index.php';
$path = '/controller/method';
$_SERVER['PATH_INFO'] = $path;
$_SERVER['REQUEST_URI'] = $path;

chdir(dirname($CI_INDEX));
ob_start();
require($CI_INDEX);
$output = ob_get_contents();
ob_end_clean();
die($output);
Favio