Page 1 of 1

Super Include Function

Posted: 13/10/2009 8:37 pm
by mindphp
Super Include Function

Code: Select all

<?php

function inc($url,$method='GET'){
    preg_match('/^(((ht|f)tps?):\/\/(.+@)?([^\/]+))?([^\?]+)(\?.+)?/i',$url,$matches);
    $method=($method) ? strtoupper($method) : $method;
    $ssl=(!empty($matches[2]) && preg_match('/s$/i',$matches[2])) ? true : false;
    $domain=(!empty($matches[5])) ? $matches[5] : NULL;
    $path=(!empty($matches[6])) ? $matches[6] : NULL;
    $param=(!empty($matches[7])) ? substr($matches[7],1) : NULL;
    
    if(!$domain){
        if($param){
            foreach(explode('&',$param) as $request){
                list($key,$val)=explode('=',$request);
                if($method=='GET'){
                    $_GET[$key]=$val;
                }
                if($method=='POST'){
                    $_POST[$key]=$val;
                }
            }
        }
        require_once($path);
        if($param){
            foreach(explode('&',$param) as $request){
                list($key,$val)=explode('=',$request);
                if($method=='GET'){
                    unset($_GET[$key]);
                }
                if($method=='POST'){
                    unset($_POST[$key]);
                }
            }
        }
        return true;
    }
    
    else if($domain){
        if($param && $method=='POST' && function_exists('curl_init')){
            $requestUrl=substr($url,0,strpos($url,'?'));
            $ch=curl_init();
            curl_setopt($ch,CURLOPT_URL,$requestUrl);
            curl_setopt($ch,CURLOPT_POST,1);
            curl_setopt($ch,CURLOPT_POSTFIELDS,$param);
            curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);
            curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);

            $content=curl_exec($ch);
            curl_close ($ch);
            echo $content;
        }else{
            echo file_get_contents($url);
        }
        return true;
    }
}

?>

<?php
inc('/var/www/html/example.php');// Normal include
inc('/var/www/html/example.php?a=b&c=d');//GET Request
inc('/var/www/html/example.php?a=b&c=d','POST');//POST Request

inc('http://example.com/');//Other Domain
inc('http://example.com/example.php?a=b&c=d');//GET Request
inc('http://example.com/example.php?a=b&c=d','POST');//POST Request
?>