php - Converting json to jsonp -
i created account on 500px.com, , use api: http://developer.500px.com/
but looks cant return jsonp, json.
is there way make php file can post url -> make convert response api json jsonp, can handle on client side?
i havent been writing php in long time, appreciated. hope idea, otherwise ill elaborate. thanks
sure can. difference between json , jsonp jsonp wrapped function name;
{ "x": "hello" } // json foo({ "x": "hello" }); // jsonp.
in it's simplest form can end this;
<?php echo $_get['callback']; ?>(<?php echo file_get_contents($_get['endpoint']); ?>);
and you'd expect clients use this;
http://yourserver.com/proxy.php?endpoint=https%3a%2f%2fapi.500px.com%2fv1%2foauth%2fauthorize&callback=foo
note encoded url , addition of callback parameter know function invoke.
of course, you'll need valid input; check existance of parameters, check endpoint
passed isn't malicious etc. might want cache responses on server stop reaching limits imposed on api.
something more resilient against malicious input like;
// checks existence, , checks input isn't array function getasstring($param) { if (is_null($_get[$param]) || !is_string($_get[$param])) { return ''; } return $_get[$param]; } $endpoint = getasstring('endpoint'); // check callback alpha-numeric characters $callback = preg_replace('/[^\w\d]/', '', getasstring('callback')); // check endpoint 500px api $is_api = (strpos($endpoint, 'https://api.500px.com') === 0); if (strlen($callback) > 0 && $is_api) { echo $callback . '(' . file_get_contents($endpoint) . ')' } else { // handle error }
Comments
Post a Comment