views:

261

answers:

1

Ive been tryin to solve this for a long time and now know why its not possible. The url

http://twittercounter.com/api/?username=Anand_Dasgupta&output=json&results=3

returns a json but when i append a "&callback=get" along with it,it doesnt specify the callback wrapper function.

So the only solution now is to build a wrapper manually round the json data.

My question is how do i do that. Is there some code already existing in php/javascript that i can change according to my specs.

Any advice will be appreciated.

Thank You

Anand

+1  A: 

Well the purpose of JSONP is to wrap the JSON (which will be evaluated as JavaScript on the client side) into a callback that only the client requesting the data knows. This prevents the client from executing unwanted JavaScript code. Without the callback ou will have the same origin policy problem (which JSONP solves), so you can only request tot he URL the script came from.

Basically you will have to attach the callback with PHP, meaning on the server side, with a proxy script. The script retrieves the data from the other URL and wraps it into a callback:

<?php
    // Don't know on the fly how to request data from another URL in PHP, but it's easy to find out
    $response = request_url('http://twittercounter.com/api/?username=Anand%5FDasgupta&amp;output=json&amp;results=3');
    echo $_GET['callback'] . '(' . $response . ')';
?>
Daff