I have the PHP which should get the JSON object from the postcodes api (http://postcodes.io/) however it does not seem to be working.
if(isset($_POST["postcode"])){
$json = file_get_contents('api.postcodes.io/postcodes/'.$_POST["postcode"]);
$json_data = json_decode($json, true);
echo "My token: ". $json_data["status"];
}
the $json_data["status"]; does not echo anything.
for example when $_POST["postcode"] equals "IP12 2UH" the below should be returned:
{
"status": 200,
"result": {
"postcode": "IP12 2UH",
"quality": 1,
"eastings": 633715,
"northings": 253024,
"country": "England",
"nhs_ha": "East of England",
"longitude": 1.41299500121177,
"latitude": 52.126073695307,
"parliamentary_constituency": "Suffolk Coastal",
"european_electoral_region": "Eastern",
"primary_care_trust": "Suffolk",
"region": "East of England",
"lsoa": "Suffolk Coastal 007G",
"msoa": "Suffolk Coastal 007",
"nuts": null,
"incode": "2UH",
"outcode": "IP12",
"admin_district": "Suffolk Coastal",
"parish": "Suffolk Coastal",
"admin_county": "Suffolk",
"admin_ward": "Rendlesham",
"ccg": "NHS Ipswich and East Suffolk",
"codes": {
"admin_district": "E07000205",
"admin_county": "E10000029",
"admin_ward": "E05007216",
"parish": "E04009449",
"ccg": "E38000086"
}
}
}
You need to add http:// in the url
Related
I have json, and i want write boolen of vpn to file.
{
"ip": "113.169.100.228",
"security": {
"vpn": false,
"proxy": false,
"tor": false
},
"location": {
"city": "",
"region": "",
"country": "Vietnam",
"continent": "Asia",
"region_code": "",
"country_code": "VN",
"continent_code": "AS",
"latitude": "16.0020",
"longitude": "105.9984",
"time_zone": "Asia/Vientiane",
"locale_code": "en",
"metro_code": "",
"is_in_european_union": false
},
"network": {
"network": "113.160.0.0/11",
"autonomous_system_number": "AS45899",
"autonomous_system_organization": "VNPT Corp"
}
}
How to write "vpn": false to php?
I use , it don't write to to file data.html
<?php
$status = $details_3->security->vpn;
$fp = #fopen('data.html', "a");
if (!$fp) {
echo 'error';
}
else
{
fwrite($fp, $status);
fwrite($fp, "\r\n");
fclose($fp);
}
?>
Please help me, when i echo $status , it can show, but don't write to file
When you convert true and false to strings, they become "1" and "". If you want to write something else, you have to do the conversion yourself, e.g.
fwrite($fp, $status ? "true" : "false");
When I am converting json_encode; It is converting it in correct format, but when I am storing it in cookie; format get changed. I want to store in cookie as it is.
JSON STRING:
{
"ID": "0",
"basicAddress": {
"ID": "0",
"Line1": "327 S Main St",
"Line2": "",
"Line3": "",
"City": "Fitzgerald",
"ZipCode": "31750",
"StateProv": "",
"Country": "",
"stateProv": "GA"
},
"Name": "",
"Latitude": "31.7114886",
"Longitude": "-83.25471970000001",
"IsPreferred": "false"
}
AFTER STORING IN COOKIE:
%7B%22ID%22%3A%220%22%2C%22basicAddress%22%3A%7B%22ID%22%3A%220%22%2C%22Line1%22%3A%22327+S+Main+St%22%2C%22Line2%22%3A%22%22%2C%22Line3%22%3A%22%22%2C%22City%22%3A%22Fitzgerald%22%2C%22ZipCode%22%3A%2231750%22%2C%22StateProv%22%3A%22%22%2C%22Country%22%3A%22%22%2C%22stateProv%22%3A%22GA%22%7D%2C%22Name%22%3A%22%22%2C%22Latitude%22%3A%2231.7114886%22%2C%22Longitude%22%3A%22-83.25471970000001%22%2C%22IsPreferred%22%3A%22false%22%7D
I found solution and it's working absolutely correct as i want
$address={
"ID": "0",
"basicAddress": {
"ID": "0",
"Line1": "327 S Main St",
"Line2": "",
"Line3": "",
"City": "Fitzgerald",
"ZipCode": "31750",
"StateProv": "",
"Country": "",
"stateProv": "GA"
},
"Name": "",
"Latitude": "31.7114886",
"Longitude": "-83.25471970000001",
"IsPreferred": "false"
};
cookie_expire_time=30000;
header("Set-Cookie: address=$address; Domain=.example.com;Path=/; Max-Age=".cookie_expire_time.";");
It is correct behaviour for setcookie to URL-encode the value. You can use setrawcookie to avoid that automatic encoding, but then you need to ensure yourself that your cookies are correctly formed to be HTTP compliant.
I have a file data.json that looks like
{
"18.01": [{
"name": "Title 1",
"id": "0",
"content": {
"html": "This should be 18.01 video 1",
"link": "https://www.youtube.com/embed/Dkm8Hteeh6M"
},
}, {
"name": "test_video_2",
"id": "1",
"content": {
"html": "This should be 18.01 video 2",
"link": "https://www.youtube.com/embed/KWafZrA5OBc"
},
}],
"18.02": [{
"name": "test_video_3",
"id": "0",
"content": {
"html": "This should be 18.02 video 3",
"link": "https://www.youtube.com/embed/EMELKZhM2-g"
},
}, {
"name": "test_video_4",
"id": "1",
"content": {
"html": "This should be 18.02 video 4",
"link": "https://www.youtube.com/embed/TkWmK-cplV4"
},
}]
}
Then I have a php file in the same folder that looks like:
<?php
//Decode JSON
$json_data = json_decode(file_get_contents('data.json'), TRUE);
//Print data
print_r($json_data);
?>
But when I load this php file in my browser I get a blank page. My php service is running (other php works fine), and if I print just the file that I'm loading that displays fine as a string.
Edit: If you're getting this error remember to validate your JSON unlike me...
The above JSON is syntactically wrong. The valid JSON is :
{
"18.01": [
{
"name": "Title 1",
"id": "0",
"content": {
"html": "This should be 18.01 video 1",
"link": "https://www.youtube.com/embed/Dkm8Hteeh6M"
}
},
{
"name": "test_video_2",
"id": "1",
"content": {
"html": "This should be 18.01 video 2",
"link": "https://www.youtube.com/embed/KWafZrA5OBc"
}
}
],
"18.02": [
{
"name": "test_video_3",
"id": "0",
"content": {
"html": "This should be 18.02 video 3",
"link": "https://www.youtube.com/embed/EMELKZhM2-g"
}
},
{
"name": "test_video_4",
"id": "1",
"content": {
"html": "This should be 18.02 video 4",
"link": "https://www.youtube.com/embed/TkWmK-cplV4"
}
}
]
}
Trailing commas are not allowed in a JSON, like at line 8 in the JSON string mentioned with the question
Furthermore, to validate JSON, you could use this nice website
I am trying to extract the different image URLs from the Instagram media feed:
<?php
$insta = file_get_contents('https://www.instagram.com/apple/media/');
$json = json_decode($insta, true);
foreach($json->items->images as $instaimage){
echo $instaimage['url']['standard_resolution'];
}
?>
Warning: Invalid argument supplied for foreach()
Output from https://www.instagram.com/apple/media:
{"items": [{"id": "1595195862853610235_5821462185", "code": "BYjRi-Aj-r7", "user": {"id": "5821462185", "full_name": "apple", "profile_picture": "https://scontent-bru2-1.cdninstagram.com/t51.2885-19/s150x150/20635165_1942203892713915_5464937638928580608_a.jpg", "username": "apple"}, "images": {"thumbnail": {"width": 150, "height": 150, "url": "https://scontent-bru2-1.cdninstagram.com/t51.2885-15/s150x150/e35/21149363_134897580459546_4211564004683808768_n.jpg"}, "low_resolution": {"width": 320, "height": 320, "url": "https://scontent-bru2-1.cdninstagram.com/t51.2885-15/s320x320/e35/21149363_134897580459546_4211564004683808768_n.jpg"}, "standard_resolution": {"width": 640, "height": 640, "url": "https://scontent-bru2-1.cdninstagram.com/t51.2885-15/s640x640/sh0.08/e35/21149363_134897580459546_4211564004683808768_n.jpg"}}, "created_time": "1504382187", "caption": {"id": "17887744828075147", "text": "\u201cI will do almost anything to get the shot I envision...
Hope this one will be helpful. There are few issues with your code.
1. json_decode($insta,true) will give you an array instead of an object, because here you are passing second parameter as true.
2. $json->items contains array of objects of items instead of single items key.
$insta = file_get_contents('https://www.instagram.com/apple/media/');
$json = json_decode($insta);
foreach($json->items as $instaimage)
{
echo $instaimage->images->standard_resolution->url;
echo PHP_EOL;
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
Here's my string which I've captured from an API using CURL:
{
"ip": "8.8.8.8",
"hostname": "google-public-dns-a.google.com",
"city": "Mountain View",
"region": "California",
"country": "US",
"loc": "37.3860,-122.0838",
"org": "AS15169 Google Inc.",
"postal": "94040"
}
How do I get the country code? This is what I've been trying and it just returns the entire json string:
$json = json_decode($json_raw, true);
$country = $json['country'];
Since your value in in an object, use -> to access.
<?php
$a = '{
"ip": "8.8.8.8",
"hostname": "google-public-dns-a.google.com",
"city": "Mountain View",
"region": "California",
"country": "US",
"loc": "37.3860,-122.0838",
"org": "AS15169 Google Inc.",
"postal": "94040"
}';
$decode = json_decode($a);
echo $decode->country;// this line
See demo here
As Surace said, even this should work fine
<?php
$a = '{
"ip": "8.8.8.8",
"hostname": "google-public-dns-a.google.com",
"city": "Mountain View",
"region": "California",
"country": "US",
"loc": "37.3860,-122.0838",
"org": "AS15169 Google Inc.",
"postal": "94040"
}';
$decode = json_decode($a,true);
echo $decode['country'];
See demo here
as you decode json to php format it returns result as object so you have to use object syn text to access its properties.
replace
$decode = json_decode($a,true);
$country = $json['country'];
with
$decode = json_decode($a);
$country = $json->country;
I could be missing something but I see no reason for your code is not working for you.
You can use json_decode with the true option like you did to use the returned value as an array.
<?php
$json_raw = '{
"ip": "8.8.8.8",
"hostname": "google-public-dns-a.google.com",
"city": "Mountain View",
"region": "California",
"country": "US",
"loc": "37.3860,-122.0838",
"org": "AS15169 Google Inc.",
"postal": "94040"
}';
$json = json_decode($json_raw, true);
echo $country = $json['country'];
You can check here: https://eval.in/483744