开发者

Retrieving Data From external Domain

I am creating a analytic project. My goal 开发者_如何学编程is to give the owner of x-domain a small amount of javascript includes from my site. That gives me the ability trace their mouse movements. I got the tracing down, All I need to do is send the data back to my server so it can be stored in my DB. my problem is the data is too large to send through getJSON.

Remember.. I can't use $.Post, or any kind of XMLhttp request because my domain and x-domain are REMOTE. And Browser don't permit that.. I can only use getJSON.

Since that doesn't work, I was told to setup a proxy. Well from what I've learned, a proxy only works for the server that has the proxy setup. Not for the server that is trying to send me the data

I know this is possible, cause ive seen it. anyone have any ideas ?? Is iframes good for what I am trying to do ?? Does anyone have any resources to share ??

Thanks alot


You can have your JavaScript create an iframe and a form, then have the form post into the iframe. You can position it off screen to make is hidden. For instance:

function post_my_data(json){

    var f = document.createElement('form');
    f.action = 'http://www.my-domain.com/receive.php';
    f.method = 'post';
    f.target = 'posttarget';

    var i = document.createElement('input');
    i.type = 'hidden';
    i.name = 'json';
    i.value = json;

    f.appendChild(i);

    var ifr = document.createElement('iframe');
    ifr.name = 'posttarget';
    ifr.style.display = 'absolute';
    ifr.style.left = '-1000px';

    document.body.appendChild(ifr);
    document.body.appendChild(f);

    f.submit();
}


Have you considered using something like JSONP?


Split your data in chunks so that getJSON would work. You could implement data queuing, so that the producer keeps filling a queue with data, and the consumer on your domain gets it in smaller chunks with getJSON. It won't be real - time, but you could try it and see if you're ok with the performance.


You can use javascript to talk to flash and have flash do the cross-domain part see http://www.xml.com/pub/a/2006/06/28/flashxmlhttprequest-proxy-to-the-rescue.html

I'm not clear on why you can't use a proxy. The javascript in the browser posts to a script running on x-domain and that script then posts the exact same info to your domain using curl or similar.


Perhaps you can better understand it then I can. I am good with php, but not to great with sending out different headers and stuff. How it works is you post a ajax post to the proxy file. the proxy file is located on a outside server

         $.ajax({
       type: "POST",
       url: "http://mywebsite.com/ajax-proxy.php",
       data: 'csurl=www.google.com',
       error: function(e) {console.log(e);}, 
       success: function(msg){console.log(msg)}

    });

You also got to pass the csurl which is the url that the proxy forwards you to. in this example I used google. but what I would normal use as the csurl is the directory to where I will store the ajax data

In the proxy file, there is a

$valid_requests = array()

In that array, you state all the urls you want the proxy to approve of. in this example you put www.google.com (Note: has to be exactly the same as csurl parameter or it wont work)

Below is the proxy file

  <?php
/**
 * AJAX Cross Domain (PHP) Proxy 0.6
 *    by Iacovos Constantinou (http://www.iacons.net)
 * 
 * Released under CC-GNU GPL
 */

/**
 * Enables or disables filtering for cross domain requests.
 * Recommended value: true, for security reasons
 */
define('CSAJAX_FILTERS', true);


/**
 * A set of valid cross domain requests
 */
$valid_requests = array(
    'www.google.com'
);

/*** STOP EDITING HERE UNLESS YOU KNOW WHAT YOU ARE DOING ***/

// identify request headers
$request_headers = array();
foreach ( $_SERVER as $key=>$value ) {
    if( substr($key, 0, 5) == 'HTTP_' ) {
        $headername = str_replace('_', ' ', substr($key, 5));
        $headername = str_replace(' ', '-', ucwords(strtolower($headername)));
        $request_headers[$headername] = $value;
    }
}

// identify request method, url and params
$request_method = $_SERVER['REQUEST_METHOD'];
$request_params = ( $request_method == 'GET' ) ? $_GET : $_POST;
$request_url    = urldecode($request_params['csurl']);
$p_request_url  = parse_url($request_url);
unset($request_params['csurl']);

// ignore requests for proxy :)
if ( preg_match('!'. $_SERVER['SCRIPT_NAME'] .'!', $request_url) || empty($request_url) ) {
    exit;
}

// check against valid requests
if ( CSAJAX_FILTERS ) {
    $parsed     = $p_request_url;
    $check_url  = isset($parsed['scheme']) ? $parsed['scheme'] .'://' : '';
    $check_url .= isset($parsed['user']) ? $parsed['user'] . ($parsed['pass'] ? ':'. $parsed['pass']:'') .'@' : '';
    $check_url .= isset($parsed['host']) ? $parsed['host'] : '';
    $check_url .= isset($parsed['port']) ? ':'.$parsed['port'] : '';
    $check_url .= isset($parsed['path']) ? $parsed['path'] : '';
    if ( !in_array($check_url, $valid_requests) ) {
        exit;
    }
}

// append query string for GET requests
if ( $request_method == 'GET' && count($request_params) > 0 && ( !array_key_exists('query', $p_request_url) || empty($p_request_url['query']) ) ) {
    $request_url .= '?'. http_build_query($request_params);
}

// let the request begin
$ch = curl_init($request_url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $request_headers);         // (re-)send headers
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                 // return response
curl_setopt($ch, CURLOPT_HEADER, true);                         // enabled response headers

// add post data for POST requests
if ( $request_method == 'POST' ) {
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($request_params));
}

// retrieve response (headers and content)
$response = curl_exec($ch);
curl_close($ch);

// split response to header and content
list($response_headers, $response_content) = preg_split('/(\r\n){2}/', $response, 2);

// (re-)send the headers
$response_headers = preg_split('/(\r\n){1}/', $response_headers);
foreach ( $response_headers as $key => $response_header )
    if ( !preg_match('/^(Transfer-Encoding):/', $response_header) ) 
        header($response_header);

// finally, output the content
print($response_content);
?>

Again, if I put http://mywebsite.com/ajax-proxy.php?csurl=www.google.com from within my website. it works fine. or even put it in the url. works fine. but if you call it from an outsite server using ajax post. doesnt work.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜