Loading CI controller in PHP on the same server - php

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?

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

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.

Have you considered using an iframe in your Wordpress page?
<iframe src="http://my-site-url/index.php/mycontroller/header"></iframe>
It might be the simplest solution.

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.

Related

What is the function of using php for site links?

I am working on a site and the builders have used a mix of php and html for links. For example:
<li>Variable Speed Drives</li>
<li>Corrosion Resistant Baseplates</li>
and
<li>MP Repair</li>
<li>MTA Repair</li>
The php is referenced in another file in this way:
<?php
$pdf_link = "../pdf/";
$external_pdf_link = "../../pdf/";
$video_link = "../video/";
$external_video_link = "../../video/";
?>
My concern is not knowing the function of the php, other than it being a placeholder, and given that the links work both ways, I don't want to break something because I am clueless to its purpose.
In doing my due diligence researching, I ran across this post, which is close, but still no cigar, Add php variable inside echo statement as href link address?. All of the research seems to be about how rather than why. This is the site, and they only used it for the "Downloads" links: http://magnatexpumps.com/
Thank you...
B
There is no right way. They are just different.
Let's forget the PHP for a while. If you have this link in a page:
<a href='about.html'/>About</a>
What will happen? The browser will change the URL of the document. If you are at the root of the site like: "www.example.com", will redirect to "www.example.com/about.html". If you are in a URL like "www.example.com/news/index.html" will redirect you to "www.example.com/new/about". That's why sometimes it is useful to have a variable before, to force a full path URL.
Another case of URL variable interpolation is when you have different systems running in the same url. In this case, you will have to append the system name in order to get to where you want. If you don't know where your application will run if it will run on the doc root, or in a subfolder, use a variable to indicate the base path.

How can I scrape a page and extract all linked resources' urls in php?

I'm actually wondering if there's some library or code available to do this with. Essentially, all I need to do is scrape a page with PHP, including it's CSS files, JavaScript, and images, and replace those URL's in the code with the URL of a local copy.
Any help or links to info on the subject would be really appreciated. Thank you.
SimpleHTMLDom - see the examples on the homepage for an easy method to extract and loop through links
$new_page_content = str_replace( $old_url, $new_url, $page_content );
PHP Manual: str_replace()

Is there any method to replace iframe?

I just meet an important problem as I have a lot of sources from another domain which means if I use iframe, I have to resize all of these contents. But the problem is that I can not modify or insert code to source web page.
Because of these, I would like to ask if there are any other solutions to skip useing iframe?
Tips: I need the entire contents (include images, css and so on) from the url. Not part of the contents.
Quick idea - you could try to write a proxy on server side and serve the content in the iframe, but with src pointing to the proxy page instead of the real page (i.e. controlled with passed parameters)
PHP's include function might be the best place to start. Is it just the markup you're unable to change, or are you able to use PHP and JavaScript?
EDIT:
Try using include('http://www.google.com') to include a URL in your page without using an iFrame. Any non-absolute directory references in the code (like <img src='/img.png'> will not display or load correctly.
If you need to fix up these references and don't have the ability to change the markup itself, you can use the file_get_contents function and modify things like this:
$page = file_get_contents('http://www.google.com');
$page = preg_replace('/(href|src)=([\'"])\//',"$1=$2http://google.com/",$page);
echo $page;
Make an AJAX call using the JSONP data type to return the contents of the other page. You would need to make modifications to the code on both domains though to make this work. http://en.wikipedia.org/wiki/JSONP
Modified answer of #beanland, my /proxy.php file, with caching:
$host = parse_url($_GET['url'], PHP_URL_HOST);
$dir = $_SERVER[DOCUMENT_ROOT].'/cache_proxy/'.$host;
if(!is_dir($dir))
mkdir($dir);
$filepath = $dir.'/'.md5($_GET['url']);
if(is_file($filepath)){
include($filepath);
}else{
$page = file_get_contents($_GET['url']);
$page = preg_replace('/(a href)=[\'\"](http.*)[\'\"]/', '$1="http://buy/proxy.php?url=$2"', $page);
$page = preg_replace('/(a href)=[\'"][^http](.*)[\'"]/', '$1="http://buy/proxy.php?url=http://'.$host.'/$2"', $page);
$page = preg_replace('/(href|src)=[\'"][^http+](.*)[\'"]/', '$1="http://'.$host.'/$2"', $page);
file_put_contents($filepath, $page);
echo $page;
}
First replace all <a href=""> links to your proxy, then replace all relative <img src="/path..."> etc. to absolute <img src="http://...">
jQuery Load into a div http://api.jquery.com/load/

Image upload - Return URL

Hello I build a script that does image uploading and resizing and it all works well, but how can I get the URL from image afterwards? I don't want my Image Source in HTML be like "../img/cat/1.png/" I want it to be like "http://MyIP/img/cat/1.png" I understand that I can just make a variable like $myHost = "http://blabla.com"; and add strip the ".." at the beginning but then it's not so good if I want to use it on other site because I need to replace this all the time. Maybe there is any other way?
You will have to use some kind of solution like what you yourself have mentioned. You can use also:
$host = $_SERVER['HTTP_HOST']
But it is not 100% reliable because of very different PHP configurations that can occur on different hosting services, and such.
Put your $myHost variable's content into a configuration file that you load up whenever you start your application. If you need to deploy the application on another server and domain and etc, just change the configuration. This is the most common way to deal with this issue.
I'm not sure if this is what you're looking for, but I think that you should explore the content of $_SERVER array (e.g. $_SERVER['HTTP_HOST']).

Storing the HTML output from a local PHP file into a string using file_get_contents

There is a header.php file and it contains some php codes that return HTML.
I know I can use require, include to echo the results, but what I want to do is to store its processed output string into a variable.
In a page, I used:
$headerHTML=file_get_contents('header.php');
Then I got the PHP code output rather than the processed HTML output.
I know adding http:// would help.
But I prefer to keep using relative path, how can I tell the function to treat the php file correctly?
Note: I would like to continue to use this statement file_get_contents rather than using ob_start() if possible.
I'd rather use require() wrapped inside ob_start() and ob_get_clean(). I am sure there is nothing wrong with this approach.
Don't use eval() - it's evil!
Use the relative local path an automatically map it to a absolute URL.
If URL wrappers are enabled and you want the output of header.php (and you don't want to keep session state) you could use $headerHTML=file_get_contents('http://yourdomain.tld/path/to/header.php');, though why you would want to do such a thing eludes me. Are you sure you're not trying to do something that could easily be solved by using templates and caching?
You can check http://in2.php.net/manual/en/function.eval.php#56641, hope it helps.

Categories