Extract JSONP Resultset in PHP

Right, it is JSON with padding. You have to remove the function name (and parenthesis) and then you can parse the JSON with json_decode.

I once wrote a function for that:

function jsonp_decode($jsonp, $assoc = false) { // PHP 5.3 adds depth as third parameter to json_decode
    if($jsonp[0] !== '[' && $jsonp[0] !== '{') { // we have JSONP
       $jsonp = substr($jsonp, strpos($jsonp, '('));
    }
    return json_decode(trim($jsonp,'();'), $assoc);
}

Usage:

$data = jsonp_decode($response);

DEMO

Leave a Comment