用于ajax跨域提交post或者get请求的代理程序

By | 2011年11月14日

局限性:1.服务器必须配置有cURL 2.增加一次服务器的请求

代码如下:

 <?php

/**
 * AJAX代理程序,用于跨域提交请求
 * 用于发送post或get请求
 * 只能被ajax请求所访问,直接访问将被忽略
 * 
 * @author 废墟 <r.anerg@gmail.com>
 * @link http://anerg.com
 */
ini_set("display_error", 0);
error_reporting(0);
date_default_timezone_set('Asia/Shanghai');
define('BASEPATH', dirname(__FILE__));

class ajaxproxy {

    public function __construct() {
        ob_start();
        header("Expires: -1");
        header("Cache-Control: no-store, private, post-check=0, pre-check=0, max-age=0", FALSE);
        header("Pragma: no-cache");
        header("Content-type: text/html; charset=utf-8");
        if (get_magic_quotes_gpc()) {
            $in = array(& $_GET, & $_POST, & $_COOKIE, & $_REQUEST);
            while (list($k, $v) = each($in)) {
                foreach ($v as $key => $val) {
                    if (!is_array($val)) {
                        $in[$k][$key] = stripslashes($val);
                        continue;
                    }
                    $in[] = & $in[$k][$key];
                }
            }
            unset($in);
        }
    }

    public function run() {
        if ($_SERVER['HTTP_X_REQUESTED_WITH'] === 'XMLHttpRequest') {
            $request_url = $this->build_url();
            if ($request_url !== FALSE) {
                $rs = $this->_curl($request_url, $_POST);
                if ($rs === FALSE) {
                    $this->log($request_url, 'AJAX_PROXY');
                    $out = array('error' => -2000, 'msg' => '系统连接超时!');
                    echo json_encode($out);
                } else {
                    echo $rs;
                }
            }
        }
    }

    private function build_url() {
        $app = trim($_GET['app']);
        $act = trim($_GET['act']);
        if (empty($app) || empty($act)) {
            return FALSE;
        } else {
            $args = $_GET;
            unset($args['app'], $args['act']);
            $uri = '';
            if (!empty($args)) {
                $uri .= '?';
                foreach ($args as $k => $v) {
                    $tmp[] = $k . '=' . $v;
                }
                $uri .= join('&', $tmp);
            }
            $request_url = 'http://' . $app . '.xs8.cn/ajax/' . $act . $uri;
            return $request_url;
        }
    }

    private function _curl($url, $post_data = array(), $second = 5) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_TIMEOUT, $second);
        curl_setopt($ch, CURLOPT_COOKIE, $_SERVER['HTTP_COOKIE']);
        if (!empty($post_data)) {
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post_data));
        }
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $content = curl_exec($ch);
        curl_close($ch);
        return $content;
    }

    private function log($log_data, $log_file) {
        $data = date("Y-m-d H:i:s") . " --> " . trim($log_data) . "n";
        $file = dirname(BASEPATH) . '/application/logs/' . $log_file . '_' . date("Y-m-d");
        error_log($data, 3, $file);
    }

}

$obj = new ajaxproxy();
$obj->run();
?>

 

发表评论

电子邮件地址不会被公开。 必填项已用*标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据