Home » Php » json – How to get params from response API Google via PHP

json – How to get params from response API Google via PHP

Posted by: admin February 25, 2020 Leave a comment

Questions:

I use PHP call API from Google, but the API response is a function. someone know how can I get params “results” in response. Thanks

G

How to&Answers:

I found the solution is API response is jsonp, so I use:

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);
}