When I use var_dump($_POST), I get this:
array(2) {
["param"]=>
string(327) "{"username":"asd","password":"asdasd","language":"7"}"
}
I need to get username. I've tried it this:
$arr = json_decode($_POST['param'], true);
echo $arr["username"];
But it doesn't work.
Anyone know where my error is and what the right way to get element username is?
Please check the edited answer:
$arr = json_decode($_POST['param'],true);
echo $arr['username'];
Your jason encoded string seems to be inside the param element, Try this:
$arr = json_decode($par['param'], true);
echo $arr["username"];
Related
This is the data that i need to extract like example profile_contact_numbers
so the output will be +639466276715
how can i do it in php code??
any help will do regards
a:2:
{s:23:"profile_contact_numbers";s:13:"+639466276715";s:16:"profile_position";s:7:"Courier";}
I'm not sure 100% this can go into an array but try the unserialize function
$json_resp = {your values};
$array[] = unserialize($json_resp);
To check if it has gone into an array print_r on $array.
Read this link if the code above doesn't work
http://php.net/manual/en/function.unserialize.php
I have managed to fix it
$serialized = array(unserialize('a:2:{s:23:"profile_contact_numbers";s:13:"+639466276715";s:16:"profile_position";s:7:"Courier";}'));
var_dump($serialized);
use code :
$var = preg_split('["]','{s:23:"profile_contact_numbers";s:13:"+639466276715";s:16:"profile_position";s:7:"Courier";}');
echo $var[1].'='.$var[3]; // profile_contact_numbers=+639466276715
echo $var[5].'='.$var[7]; // profile_position=Courier
<?php
function get_stuff()
{
($_GET['http://****RemovedForSecurityPurposes****.repairshopr.com/api/v1/tickets?api_key=****RemovedForSecurityPurposes****']);
echo $_GET;
}
get_stuff();
?> /* Yes, Pointless closing tag, I'm cool like that. */
For some reason when I run this code the output I'm getting is "Array" and I can't figure out why? I know it is an array that I'm getting from the URL but I thought it would just print in whatever format its in? Am I missing something?
Thanks In advance!
You cannot pass website URL to $_GET.
You should use file_get_contents()
$content = file_get_contents('http://YOUR_URL');
If you have valid json then you can convert it to an array.
$data = json_decode($content, true);
then print it,
echo '<pre>'; print_r($data);
The Message of OP when I run this code the output I'm getting is
"Array".
You need to use file_get_contents to read a file from a remote server. If your file response an array then you have to use print_r or var_export or var_dump. Or if your file response is a string(json) then you need to store it in a variable and apply a decode method.
function get_stuff(){
$response = file_get_contents('http://****RemovedForSecurityPurposes****.repairshopr.com/api/v1/tickets?api_key=****RemovedForSecurityPurposes****');
print_r($response); // if array
$arr = json_decode($response); // decode of json
print_r($arr);
}
get_stuff();
I think you will understand what i mean. Let me know if you are useful or need some help.
I am trying to get only the refresh_token field from the text file using file_get _contents. please anyone solve this.
{"access_token":"XXXX","token_type":"bearer","expires_in":3600,"refresh_token":"XXXX"}
For json manipulation you should use json_decode
$str= file_get_contents(some.txt);
$array = json_decode($content);
echo $array->access_token;
Thats a json string
$content = file_get_contents([...]);
$arr = json_decode($content);
echo $arr->access_token;
I am trying to output some json data from a json file via php but it does not seem to work. I tried this:
<?php
$jsonFile = file_get_contents('dataset/dataset.json');
$data = json_decode($jsonFile, true);
echo $data->{'data'}[0]->{'letter'}
?>
The json file is following:
{
"data":[
{
"letter":"A",
"blocks":{
"1":"0",
"2":"0",
"3":"0",
"4":"0",
"5":"0"
}
}
]}
Basically it should output the letter "A" but it outputs nothing. What did I do wrong?
Thanks
P.S. I tried to do it like here: How to process JSON in PHP? but it does not work.
After json_decode($jsonFile, true) your data is in array. So you should not access using object. Access data by array index. Try this..
echo $data['data'][0]['letter'];
More about json_decode()
This says, you get an array (the true parameter):
$data = json_decode($jsonFile, true);
You can see this if you do this:
print_r($data);
Try this:
echo $data['data'][0]['letter'];
This snippet works fine:
$url="Http://www.youtube.com/watch?v=upenR6n7xWY&feature=BFa&list=PL88ACC6CB00DC2B44&index=4";
$parsed_url=parse_url($url);
echo "<br><br><pre>";
print_r(parse_url($url));
echo "</pre>";
echo $parsed_url['query'];
But when I add the below:
echo "<br><br><pre>";
$parsed_str=parse_str($parsed_url['query']);
print_r($parsed_str);
echo "</pre>";
Nothing happens. I suspect parse_str() isn't working as it should. Any ideas what I might be doing wrong?
If you want to have the result of parse_str() in an array, pass the array as the second argument:
parse_str($parsed_url['query'], $parsed_str);
var_dump($parsed_str);
I'm going to assume that the user inputs the URL. If so, do not use parse_str without a second argument!. Doing so would result in a security risk where the user can overwrite arbitrary variables with the value of their choice.
parse_str() doesn't return anything. It populates variables.
For example, if you have a query string of $query = "param=1&test=2"
after
parse_str($query);
you can check var_dump($param) and var_dump($test) - those two variables would be created for you.
Basically, parse_str converts
v=upenR6n7xWY&feature=BFa&list=PL88ACC6CB00DC2B44&index=4
To
$v = "upenR6n7xWY"
$feature = "BFa"
$list = "PL88ACC6CB00DC2B44"
$index = 4