发布跨域AJAX

时间:2014-12-11 18:42:27

标签: ajax cross-domain

我试图向外部网站发送POST请求,从目前为止的iv读取由于相同的原始策略而无法实现。但我也读过代理可以绕过这个。

如果我无法访问外部网站,这可能是全部吗?我似乎无法澄清这一点。

我只想发送一个AJAX POST并获得响应,就像我使用 Chrome的高级REST客户端时一样。

2 个答案:

答案 0 :(得分:1)

使用

设置标题
Access-Control-Allow-Origin: http://foo.example

答案 1 :(得分:0)

问题中的任何其他noobies

略微修改了这个: https://github.com/eslachance/php-transparent-proxy

现在接受发布网址:

<?php
if(!function_exists('apache_request_headers')) {
// Function is from: http://www.electrictoolbox.com/php-get-headers-sent-from-browser/
    function apache_request_headers() {
        $headers = array();
        foreach($_SERVER as $key => $value) {
            if(substr($key, 0, 5) == 'HTTP_') {
                $headers[str_replace(' ', '-', ucwords(str_replace('_', ' ', strtolower(substr($key, 5)))))] = $value;
            }
        }
        return $headers;
    }
}
// Figure out requester's IP to shipt it to X-Forwarded-For
$ip = '';
if (!empty($_SERVER['HTTP_CLIENT_IP'])) { 
    $ip = $_SERVER['HTTP_CLIENT_IP'];
    //echo "HTTP_CLIENT_IP: ".$ip;
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
    $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
    //echo "HTTP_X_FORWARDED_FOR: ".$ip;
} else {
    $ip = $_SERVER['REMOTE_ADDR'];
    //echo "REMOTE_ADDR: ".$ip;
}

//
preg_match('@^(?:http://)?([^/]+)@i', $_SERVER['HTTP_REFERER'], $matches);
$host = $matches[1];
preg_match('/[^.]+\.[^.]+$/', $host, $matches);
$domainName = "{$matches[0]}";
//
//writelog($_POST);

$method = $_SERVER['REQUEST_METHOD'];
$desturl;
    // parse the given URL
    if($method == "POST") {
        $desturl = $_POST['url'];
        unset($_POST['url']);
    }
    else if ($method == "GET") {
        $desturl = $_GET['url'];
        unset($_GET['url']);
    }


$response = proxy_request($desturl, ($method == "GET" ? $_GET : $_POST), $method);
$headerArray = explode("\r\n", $response[header]);
foreach($headerArray as $headerLine) {
    header($headerLine);
}
echo $response[content];


function proxy_request($url, $data, $method) {
// Based on post_request from http://www.jonasjohn.de/snippets/php/post-request.htm
    global $ip;
    // Convert the data array into URL Parameters like a=b&foo=bar etc.


    //$data = http_build_query($data);

    $data = json_encode($data, JSON_FORCE_OBJECT);
    writelog($data);
    $datalength = strlen($data);





    $url = parse_url($url);
    //echo $url;
    if ($url['scheme'] != 'http') { 
        die('Error: Only HTTP request are supported !');
    }

    // extract host and path:
    $host = $url['host'];
    $path = $url['path'];

    // open a socket connection on port 80 - timeout: 30 sec
    $fp = fsockopen($host, 80, $errno, $errstr, 30);

    if ($fp){

        $out="";
        if($method == "POST") {
            $out ="POST $path HTTP/1.1\r\n";
        } else {
            $out ="GET $path?$data HTTP/1.1\r\n";
        }

        //Todo Get headers and forward them...

        $requestHeaders = apache_request_headers();

        while ((list($header, $value) = each($requestHeaders))) {
            /*if($header !== "Connection" && $header !== "Host" && $header !== "Content-Length" 
                && $header !== "Content-Type"
                && $header !== "Origin"
                && $header !== "Referer"
                && $header !== "X-Requested-With"
                && $header !== "Accept-Encoding"
                ) {
               // $out.= "$header: $value\r\n";
                writelog("$header: $value\r\n");
            }*/
            if($header == "Cookie" && $header == "User-Agent" ) {
                 $out.= "$header: $value\r\n";
                //writelog("$header: $value\r\n");
            }
        }
        $out.= "Host: $host\r\n";
        $out.= "Content-Type: application/json; charset=UTF-8\r\n";
        $out.= "Content-Length: $datalength\r\n";
        $out.= "Connection: Close\r\n\r\n";
        $out.= $data;
        fwrite($fp, $out);


        $result = ''; 
        while(!feof($fp)) {
            // receive the results of the request
            $result .= fgets($fp, 128);

        }
    }
    else { 
        return array(
            'status' => 'err', 
            'error' => "$errstr ($errno)"
        );
    }

    // close the socket connection:
    fclose($fp);

    // split the result header from the content
    $result = explode("\r\n\r\n", $result, 2);

    $header = isset($result[0]) ? $result[0] : '';
    $content = isset($result[1]) ? $result[1] : '';


    // return as structured array:
    return array(
        'status' => 'ok',
        'header' => $header,
        'content' => $content
    );
}
?>