Reputation: 5618
In my mobile, angular app i try to send a JSON object to the server for further processing.
This is my JSON
$sessionsString = {"ojoijoj":[{"station":"AABB","height":80,"period":"20","gsh":"1.11","gsp":"20","gsd":"225","wsh":"1.11","wsp":"5.3","wsd":"270","dt":"2015-07-08T00:00:00.000Z","station2":"9410230","windStrength":"3.6","windDirection":"220","tideHeightInCM":115.06811989101},{"station":"CCBB","height":90,"period":"10","gsh":"1.7","gsp":"10","gsd":"207.5","wsh":"2.7","wsp":"9.9","wsd":"207.5","dt":"2015-07-16T21:00:00.000Z","station":"94102302","windStrength":"3.17","windDirection":"308.502","tideHeightInCM":83.986622073579}],"y4":[{"station":"DD","height":90,"period":"15","gsh":"1.11","gsp":"15","gsd":"225","wsh":"2.3","wsp":"5","wsd":"297.5","dt":"2015-07-04T19:00:00.000Z","station2":"9410230","windStrength":"1.5","windDirection":"280","tideHeightInCM":134.37293729373}]};`
I'm sending my JSON via
$http.post(requestURL, {'sessions':JSON.stringify(sessions)}).
success(function(data, status, headers, config) {
console.log('Success');
}).
error(function(data, status, headers, config) {
});
and figured through some other references that I have to get the data through
$params = json_decode(file_get_contents('php://input'), true);
to retrieve the parameters rather than with regular $_POST for some reason. (Found this in other posts, $_POST didnt work -> is this thread safe by the way?)
However, I want to parse the data now, and it doesn't work.
$sessionsString = $params['sessions'];
$sessions = json_decode($sessionsString,true);
$spotNames = array_keys($sessions);
array_keys
fails with no keys found. When I directly assign the above JSON string to $sessionString
it works fine. What's happening? I dont know how to debug this because it's a POST parameter that I can't fully see and the file read out in PHP could be a problem, too.
Thanks, EL
Upvotes: 0
Views: 125
Reputation: 171669
Dont stringify in $http
just pass in the object. angular will take care of converting to json
$http.post(requestURL, {sessions:sessions})...
Also the first json_decode
should now work without needing the second
Upvotes: 2
Reputation: 21
Looks to me like you are calling json_decode
twice. Not sure why $_POST
didn't work for you, but you can always use var_dump($params)
to see whats happening.
Upvotes: 0