I am trying to pass data from ios to server.
Php code :
<?php
$inputJSON = file_get_contents('php://input');
$data = json_decode($inputJSON, TRUE);
var_dump($data);
?>
This gives me string(0) ""
. I don't know whats the meaning of this.
echo $inputJSON; gives nothing
var_dump($inputJSON); returns string(0) ""
When i print the json-string it gives me a valid string
Please find the full codes of this problem in another question of mine
With God Grace solved my problem-
pass true to convert objects to associative arrays, so accessing numeric/associative array like this.
$var = $data[0]['key'];
Then we use a combination of numeric and associative array syntax to access the desired element in multidimensional array.
But if i trying to var_dump($data); is retun null.
Reference tutorial: http://www.dyn-web.com/tutorials/php-js/json/decode.php
fisrt you should check is your url is correct or not. If every thing is right but still its not showing the response use curl functionality such as alter native of file_get_content
I have created an example for you that what i am talking:-
1.My both file (php
code file and json
string file) in the same working directory.Check:- http://prntscr.com/apkbp9
2.json
string file content should be:-
[
{
"email" : "",
"Name" : "Ddd",
"contact2" : "",
"ontact1" : ""
},
{
"email" : "",
"Name" : "Ddd",
"contact2" : "",
"contact1" : ""
},
{
"email" : "",
"Name" : "Dddddr",
"contact2" : "",
"contact1" : ""
}
]
Check here:- http://prntscr.com/apkbx0
3.php
code file content:-
<?php
$inputJSON = file_get_contents('input.txt');
echo $inputJSON;
$data = json_decode($inputJSON, TRUE);
echo "<pre/>";print_r($data);
?>
Check here:- http://prntscr.com/apkc6x
Output on my browser:- http://prntscr.com/apkcoi
You can use CURL
like below:-
<?php
function url_get_contents ($Url) {
if (!function_exists('curl_init')){
die('CURL is not installed!');
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $Url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
$inputJSON = url_get_contents('php://input');
echo $inputJSON;
$data = json_decode($inputJSON, TRUE);
echo "<pre/>";print_r($data);
?>
For more reference about CURL
:- http://php.net/manual/en/book.curl.php
Note:- if both will not work, then first you need to save your json
data in a file with extension .txt
and then you can use both code given above, just by putting the full path of that text file.