php proxy for jquery ajax cross site request

来源:互联网 发布:黑客最喜欢的linux系统 编辑:程序博客网 时间:2024/05/18 01:19
<?phpdefine('AUTO_LOGIN', true);define('PLATFORM_URL', 'http://10.64.68.12:6543');function socketRedirect(){    try     {        if ( ! isset($_SERVER['PATH_INFO']) )         {            $response_code = 404;            $header_status = sprintf("Status Code: %d", $response_code);            header($header_status, true, $response_code);            return;        }        $url = PLATFORM_URL . $_SERVER['PATH_INFO'];        list($resp_code, $body) = JsonPost($url, GetRowPostData());                $header_status = sprintf("Status Code: %d", $resp_code);        header($header_status, true, $resp_code);                echo $body;    }    catch ( Exception $ex )     {        $response_code = 500;        $header_status = sprintf("Status Code: %d", $response_code);        header($header_status, true, $response_code);    }    }function GetRowPostData(){    return file_get_contents('php://input');}/* POST JSON encoded version of $input to $url *//* By Tim Hastings, http://www.nonhostile.com/howto-http-post-json-using-php.asp */function JsonPost($url, $data) {    list($notsused, $hostportres) = explode("://", $url, 2);    list($hostport, $resource) = explode("/", $hostportres, 2);    list($host, $port) = explode(":", $hostport, 2);    $resource = '/' . $resource;               $content_length = strlen($data);    $requestMethod = $_SERVER["REQUEST_METHOD"];        $fp = fsockopen($host, $port);    fputs($fp, "$requestMethod $resource HTTP/1.1\r\n");    fputs($fp, "Content-Type: application/json\r\n");    fputs($fp, "Content-Length: $content_length\r\n");    fputs($fp, "Connection: close\r\n\r\n");    fputs($fp, $data, $content_length);    $http_response = stream_get_contents($fp);    fclose($fp);    list($headers, $body) = explode("\r\n\r\n", $http_response, 2);        $header_array = explode("\r\n", $headers);    $http_response = $header_array[0];    $resp_sections = explode(" ", $http_response);    $resp_code = $resp_sections[1];        return array($resp_code, $body);}function main() {        socketRedirect();    return;}main();?>