2

I would like to get an array from my JSON in php. This way I get the JSON string from URL in my android application:

JSONObject json = jParser.makeHttpRequest(url_all_user, "GET", paramstodb);

To receive [phone=123] in php I use this:

if (isset($_GET["phone"])) {
    $phone = $_GET['phone'];

That is working for one phonenumber, but now I need more than one phonenumber.

The data in Logcat (reported with "Log.d("to php: ", paramstodb.toString())" ) is displayed as:

to php:īš• [phone=[0127361744, 0132782422, 0137173813, 0142534646, 0123617637435, 013391339494, 01383375633, 013878942423, 013891748422, 01389487285, 014434354234, 01848481371, 018831789414, 021238133441231, 021371689411, 02183718454, 123, 456]]

How can I get all numbers in an array in php? This is not working so far:

if (isset($_GET["phone"])) {
    $phone = $_GET['phone'];
    $phpArray = json_decode($phone, true);

I hope you can help me again ;-)

Timitrov
  • 211
  • 1
  • 3
  • 14
  • take a look here and try this: http://stackoverflow.com/a/3395811/1173391 – Nickolaus Feb 01 '15 at 14:23
  • Show the result of `var_dump($phone);` and `var_dump($phpArray);`. It's not ideal to send that much data in the querystring, you might be losing some of it. Better to use a POST request. – MrCode Feb 01 '15 at 14:28
  • The JSON looks like this: { "phone": [ "123", "456", "789"]} But I can't get the values because it is no array in php (keys are missing). How can I get the values? – Timitrov Feb 01 '15 at 17:27

1 Answers1

0

If the JSON input to the PHP script really is this JSON

{ "phone": [ "123", "456", "789"] }

then PHP's json_decode should handle it without problems. You can try this code to see it's actually working and use it to detect where something goes wrong:

// original JSON to send from the client
$jsonString = '{ "phone": [ "123", "456", "789"] }';

// build a query string with the JSON to send
$queryString = "?" . http_build_query(array("phone" => $jsonString));
echo "Query string to send is: " . $queryString  . PHP_EOL;

// PHP side: this is not a real HTTP GET request, but to pretend we have
// got some data in, we'll use the same query string, parse it, and store it
// in $params
$incoming = parse_url($queryString, PHP_URL_QUERY);
parse_str($incoming, $params);

// now print contents of "phone" parameter
echo "URL parameter phone contains " . $params["phone"] . PHP_EOL;

// JSON-decode the "phone" parameter
var_dump(json_decode($params["phone"], true));

This should print:

Query string to send is: ?phone=%7B+%22phone%22%3A+%5B+%22123%22%2C+%22456%22%2C+%22789%22%5D+%7D
URL parameter phone contains { "phone": [ "123", "456", "789"] }
array(1) {
  'phone' =>
  array(3) {
    [0] =>
    string(3) "123"
    [1] =>
    string(3) "456"
    [2] =>
    string(3) "789"
  }
}

which shows the JSON decodes to a proper PHP array. An array of strings, to be precise, and not numbers as requested. Turning the strings into numbers in PHP will be easy to do, but maybe you could also make sure on the call site that you send numbers and not strings.

If your original code does not work, I guess the incoming data is either no properly encoded JSON or there is some magic escaping going on (magic quotes hell, should be turned off in today's PHP, but could be a reason for garbled script input).

To make sure your JSON data is not truncated and to also save you from potential URL-encoding issues, I also suggest sending the JSON via HTTP POST, not HTTP GET.

stj
  • 9,037
  • 19
  • 33