Extract Jsonp Resultset in PHP

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

Parsing JSONP in PHP

If you need variable urls, it's a good idea to wrap the fetching into a function.

See the example below

function get_JSONP($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($ch);
curl_close($ch);

return $output;

}

function jsonp_decode($jsonp, $assoc = false) { // PHP 5.3 adds depth as third parameter to json_decode
if($jsonp[0] !== '[' && $jsonp[0] !== '{') {
$jsonp = substr($jsonp, strpos($jsonp, '('));
}
$jsonp = trim($jsonp); // remove trailing newlines
$jsonp = trim($jsonp,'()'); // remove leading and trailing parenthesis

return json_decode($jsonp, $assoc);

}

$url = "http://live.nhle.com/GameData/GCScoreboard/2016-01-18.jsonp";

$jsonp = get_JSONP($url);
$json = jsonp_decode($jsonp);

print_r($json);

Will output the $json object

stdClass Object
(
[games] => Array
(
[0] => stdClass Object
(
[usnationalbroadcasts] =>
[atcommon] => OILERS
[canationalbroadcasts] =>
[rl] =>
[ata] => EDM
[gcl] => 1
[ats] =>
[hta] => FLA
[htc] =>
[bs] => 7:30 PM
[htcommon] => PANTHERS
[id] => 2015020677
[atn] => EDMONTON
[hts] =>
[atc] =>
[gs] => 1
[bsc] =>
[htn] => FLORIDA
[gcll] => 1
)

[1] => stdClass Object
(
[usnationalbroadcasts] => NBCSN
[atcommon] => PENGUINS
[canationalbroadcasts] => TVAS, SNE, SNO, SNP
[rl] =>

To iterate over the contents

foreach($json->games as $game){
print $game->canationalbroadcasts . PHP_EOL;
}

How can i get the value of array in php?

I'm not yet able to write a comment, so i'll try to write it as an answer.
json_decode will not return a value because your json string is containing callback function response() which is not a valid json string.

You'll need to remove the callback function name response( and the trailing ).

example:

$array = json_decode(substr( $jsonp, 8, -1 ));
// $jsonp is your server jsonp response

Convert String to Json in PHP from GET Request

If you grab the results from the URL without the callback parameter, you can get the standard JSON format.

https://www.carqueryapi.com/api/0.3/?cmd=getYears

{ "Years": {"min_year":"1941", "max_year":"2017"} }

Converting Mysql result set to JSON prints same fields twice

Set fetchAll first argument to PDO::FETCH_ASSOC or PDO::FETCH_NUM. As by default it's set to PDO::FETCH_BOTH which gives you this output.

$result = $sql->fetchAll(PDO::FETCH_ASSOC);

More info about FETCH-parameters

Extract This Field From Json Array PHP

This ought to do the trick:

<?php

foreach ($row['ExternalIds'] as $val)
{
if ($val['ExternalService'] == 'eulerid') {
$externalId = $val['ExternalId'];
}
}

Multiple json from a database using php

I suppose you could group your database rows and comma-delimit them so that they are flat, then unpack/explode them as you iterate.

SELECT errors.error_code,
errors.issue,
GROUP_CONCAT(DISTINCT errors.cause) AS causes,
GROUP_CONCAT(DISTINCT errors.solution) AS solutions,
GROUP_CONCAT(DISTINCT products.product) AS products
FROM errors
JOIN products
ON errors.issue = products.issue
AND errors.error_code = products.error_code
WHERE errors.error_code = 'A01'
GROUP BY errors.error_code,
errors.issue;


Leave a reply



Submit