Home  >  Article  >  Backend Development  >  How to Extract JSONP Resultsets in PHP?

How to Extract JSONP Resultsets in PHP?

Susan Sarandon
Susan SarandonOriginal
2024-10-30 05:09:02253browse

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] !== '[' &amp;&amp; $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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn