How to Extract JSONP Resultsets in PHP?

Susan Sarandon
Release: 2024-10-30 05:09:02
Original
252 people have browsed it

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>
Copy after login

Usage:

<code class="php">$data = jsonp_decode($response);</code>
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!