Home > Article > Backend Development > How to Extract JSONP Resultsets in PHP?
Extracting JSONP Resultset in PHP
PHP can retrieve data from JSONP URLs, even though JSONP returns a callback function. To do so, remove the function name and parentheses from the response and parse the remaining JSON using json_decode().
For convenience, a PHP function called jsonp_decode() can be used to streamline the process:
<code class="php">function jsonp_decode($jsonp, $assoc = false) { if($jsonp[0] !== '[' && $jsonp[0] !== '{') { $jsonp = substr($jsonp, strpos($jsonp, '(')); } return json_decode(trim($jsonp,'();'), $assoc); }</code>
Usage:
<code class="php">$data = jsonp_decode($response);</code>
This function simplifies the extraction of the JSONP resultset, making it accessible in a PHP variable.
The above is the detailed content of How to Extract JSONP Resultsets in PHP?. For more information, please follow other related articles on the PHP Chinese website!