-1

I'm trying to receive an array via a cURL POST request, and I'm having an extrange issue.

Long story short: This is an api controller we're in, and when I json_encode a response...

echo json_encode($_POST['keys']); return;

The result (at the calling end of the api communication):

array(361) {
  [0]=>
  string(26) "some+urlencoded+string"
  [1]=>
  string(14) "and+some+other"
  ...
}

and so on. So the api receives my argument array no problem.

Then in the api I try a foreach loop to urldecode each string, to have those values in some other array:

$myArray = array();

foreach ($_POST['keys'] as $key => $value)
{
    $myArray[$key] = urldecode($value);
    // Or $myArray[] = urldecode($value); -- same result
}

echo json_encode($myArray); return;

And this is the result:

NULL

What am I doing wrong?

Thanks in advance :)

==================================================================================

EDIT: The problem seems to be that, in the api controller (that lives in a yii application) urledecode does not work. Neither does utf8_decode, nor base64_decode. At least they do not work inside a foreach loop, anyway. Why don't they work? Beats me. I'm still stuck.

==================================================================================

EDIT 2: I've made some progress in the isolation of the problem, asked another Q here at SO. Sorry for this, might as well be closed.

PHP (CI) cURL passed multidimensional array does not behave as one (Can't loop it)

Community
  • 1
  • 1
Manuel Gómez
  • 103
  • 1
  • 9

1 Answers1

1

I don't think your problem is here... are you sure your $_POST['keys'] is ok when you foreach it ? you should try to echo something in your loop, or print_r($_POST['keys']) just before.

I tried this :

$_POST['keys'] = Array('some+urlencoded+string','and+some+other') ;
$myArray = array();

foreach ($_POST['keys'] as $key => $value)
    $myArray[$key] = urldecode($value);

echo '<pre>' ;
var_dump($_POST['keys']) ;
echo json_encode($_POST['keys']);
echo "\n" ;
echo json_encode($myArray);
echo '</pre>' ;

wich render this, looked pretty correct :

array(2) {
  [0]=>
  string(22) "some+urlencoded+string"
  [1]=>
  string(14) "and+some+other"
}
["some+urlencoded+string","and+some+other"]

["some urlencoded string","and some other"]

Make print_r on $_POST['keys'] and on $myArray, i think your loop is just not working.

Pierre Granger
  • 1,993
  • 2
  • 15
  • 21
  • First of all, thanks for taking the time to respond. I remind you that this is an api that I'm calling from another application, and I can only var_dump the $data = callApi(array('keys' => $keys)); in the original application (the one that calls the api(). That's why I can only have one echo json_encode(), and that is what I see back. – Manuel Gómez Feb 07 '14 at 11:08
  • Well i can't tell you more... are your sure your foreach is looping a real array var ? can you still var_dump the variable you try to foreach ? Even if there is a problem you should not have "null" as long as you really enter your foreach loop. after json_encode have you tried to print_r of sizeof your $myArray ? – Pierre Granger Feb 07 '14 at 11:28
  • What does var_dump($value) and var_dump(urlencode($value)) in your loop ? Maybe your $value is not a regular string wich can be encoded ? Have you tried urlencode((string)$value) ? – Pierre Granger Feb 07 '14 at 12:39