Using json response in PHP - php

I am trying to use one object from the many provided in my json request.
Trying to obtain only the country name from the data that is given.
$location = file_get_contents('http://freegeoip.net/json/'.$_SERVER['REMOTE_ADDR']);
echo $location;
The above code gives me the following string:
{"ip":"x.xx.xx.x","country_code":"FR","country_name":"France","region_code":"A2","region_name":"Bretagne","city":"Brest","zipcode":"","latitude":xxxx,"longitude":xxxx,"metro_code":"","area_code":""}
Any help would be greatly appreciated!

$a = json_decode($location);
echo $a->country_name;
You might also want to have a look on this. http://www.php.net/manual/en/function.json-decode.php

Look for manual json_decode
$tmp = json_decode($location);
echo $tmp->country_code

Related

PHP json decoder string

anyone can help to decode this?
var_dump
sring(56441) "{"success":{"message":"PDF \u0444\u0430\u0439\u043b\u0430 \u0435 \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0430\u043d \u0443\u0441\u043f\u0435\u0448\u043d\u043e!","name":"invoice-0000000001.pdf","data":"JVBERi0xLjQKMSAwIG9iago8PAovVGl0bGUgKP7\/BCQEMAQ6BEIEQwRABDAAICEWADAAMAAwADAAMAAwADAAMA","type":"application\/pdf"}}"
I need to get "data" from this code.
<?php
$data='{"success":{"message":"PDF \u0444\u0430\u0439\u043b\u0430 \u0435 \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0430\u043d \u0443\u0441\u043f\u0435\u0448\u043d\u043e!","name":"invoice-0000000001.pdf","data":"JVBERi0xLjQKMSAwIG9iago8PAovVGl0bGUgKP7\/BCQEMAQ6BEIEQwRABDAAICEWADAAMAAwADAAMAAwADAAMA","type":"application\/pdf"}}';
$data=json_decode($data,true);
echo '<pre>';
print_r($data['success']['data']);
It's a simple json that you need to make it an array and access it as a normal array. The output is the data field you asked:
JVBERi0xLjQKMSAwIG9iago8PAovVGl0bGUgKP7/BCQEMAQ6BEIEQwRABDAAICEWADAAMAAwADAAMAAwADAAMA
Well, you only need to json_decode() that string and you have your result:
<?php
$input = '{"success":{"message":"PDF \u0444\u0430\u0439\u043b\u0430 \u0435 \u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0430\u043d \u0443\u0441\u043f\u0435\u0448\u043d\u043e!","name":"invoice-0000000001.pdf","data":"JVBERi0xLjQKMSAwIG9iago8PAovVGl0bGUgKP7\/BCQEMAQ6BEIEQwRABDAAICEWADAAMAAwADAAMAAwADAAMA","type":"application\/pdf"}}';
$output = json_decode($input);
print_r($output->success->data);
JVBERi0xLjQKMSAwIG9iago8PAovVGl0bGUgKP7/BCQEMAQ6BEIEQwRABDAAICEWADAAMAAwADAAMAAwADAAMA

extract specific data from array in mysql database using php

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 Using ($_GET['url']) and then displaying that json data?

<?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.

create an empty object in JSON with PHP?

i have create json from an array but how we can make an empty json
$jsonrows['paymentmethods']['CC']=array()
json_encode($jsonrows['paymentmethods']['CC']=array())
currently output is like this
"CC":[]
what i need is
"CC":{}
please help me with this
Use a class instead of an array:
var_dump(json_encode(new StdClass()));
Try This
$cc['CC'] = new stdClass ;
echo json_encode($jsonrows['paymentmethods']=$cc);
Output is
{"CC":{}}
For code readability, I use $emptyObject = (object)[];
In the context of your example:
$jsonrows['paymentmethods']['CC']=array();
echo json_encode($jsonrows['paymentmethods']);
yields the undesired: {"CC":[]}
$jsonrows['paymentmethods']['CC']=(object)array();
echo json_encode($jsonrows['paymentmethods']);
gives what you want: {"CC":{}}

How to parse JSON string in php

I'm getting below JSON response:
[{"startDate":"2012-07-12 11:21:38 +0530","totalTime":0},{"startDate":"2012-07-11 11:27:33 +0530","totalTime":0},{"startDate":"2012-07-16 18:38:37 +0530","totalTime":0},{"startDate":"2012-07-17 14:18:32 +0530","totalTime":0}]
i want make array of start date and totalTime, i have used these two lines but it wont work $obj, please suggest..
$obj = json_decode($dateTimeArr);
$dateAr = $obj->{'startDate'};
As everyone said, and you did - use json_decode.
$dateArrays = json_decode($dateTimeArr, true); // decode JSON to associative array
foreach($dateArrays as $dateArr){
echo $dateArr['startDate'];
echo $dateArr['totalTime'];
}
In future, if you are unsure what type or structure of data is in the variable, do var_dump($var) and it will print type of variable and its content.
It is very easy:
$Arr = json_decode($JSON, true);
json_decode() will give you nested PHP types you can then descend to retrieve your data.
use json_decode($json_response,true) to convert json to Array
Guess what you are looking for is json_decode()
Check out http://php.net/manual/en/function.json-decode.php for the inner workings

Categories