0

I want to create an array, json_encode it, then send it off via echo. The array needs to be created from the following server response I'm getting from an external API:

someName([{"lat":"35.7804015","lon":"-78.6390779"}])

I want to do something like this:

$lat = someName['lat']; // this doesn't work
$lon = someName['lon']; // this doesn't work

$data = array('lat'=> $lat, 'lon'=> $lon);

echo json_encode($data);

The problem is that I'm not getting the data from "someName". What am I doing wrong?

Chris Paterson
  • 1,129
  • 5
  • 21
  • 31
  • Looks like the api is returning jsonp response. – palanik Mar 01 '14 at 19:03
  • That's `JSONP`, not `JSON`. It can be directly decoded by `json_decode`. You have to pull `JSON` from this `JSONP` response manually – hindmost Mar 01 '14 at 19:05
  • Checkout [extracting jsonp in php](http://stackoverflow.com/questions/5081557/extract-jsonp-resultset-in-php/5081588#5081588) – palanik Mar 01 '14 at 19:14

3 Answers3

1

You are dealing with JSONP here. It's basically a whole function call instead of raw data, so you have to extract the needed part from the string:

$response = 'someName([{"lat":"35.7804015","lon":"-78.6390779"}])';
$jsonString = substr($response, 10, -2); // 10 = length of 'someName(['

$dataArray = json_decode($jsonString, true);

echo $dataArray['lat']; // 35.7804015
echo $dataArray['lat']; // -78.6390779

So now you have your data in an array and can access it by key as you were trying in your example:

print_r($dataArray);

Output:

Array
(
    [lat] => 35.7804015
    [lon] => -78.6390779
)
Community
  • 1
  • 1
Quasdunk
  • 14,944
  • 3
  • 36
  • 45
0

A server response like that is JSONP. You access an API like that from the client in a specific way that requires knowledge of JSONP internals. The reason to use JSONP is to get around cross-origin restrictions.

A shortcut is to use a library like jQuery which will abstract JSONP for you. The ajax method has an easy-to-use JSONP dataType. Here's an example:

$.ajax({
    type: 'GET',
    url: 'https://someapiendpoint.com/v1/',
    dataType: 'jsonp',
    success: function(data) {
        console.log(data);
    },
    error: function(e) {
        console.log(e.message);
    }
});

Your data will automatically be available in the 'data' variable in the 'success' callback.

I encourage you to look into the JSONP internals after your current project is done. It's quite an interesting workaround and there are many more things you can do with it.

edj
  • 23
  • 3
0

That looks like jsonp response which is really meant to circumvent SOP on client side JavaScript environments.
You should check the api to see if it supports a JSON response and use json_decode.
If there isn't you'll have to extract the data from the response.

$json = preg_replace('/(.+\()|(\)$)/', '', $responseString);
$data = json_decode($json, true);
echo json_encode(array('lat'=> $data[0]['lat'], 'lon'=> $data[0]['lon']));

http://codepad.org/CIg6i8tx

Musa
  • 96,336
  • 17
  • 118
  • 137