Pulling data in JSON format from URL - php

Hi i have been trying to pull data in JSON format. Here is my code
$resourse_url = 'http://www.livepicly.com/app/api.php?method=list_vendor_name';
$json_data = file_get_contents($resourse_url);
$json_output = json_decode($json_data, TRUE);
$vendor = $json_output['vendor_name'][1];
echo "<pre>";
print_r($vendor);
exit(1);
However when i run the code it does not return anything. When i try to access the source URL via firefox, it asks me where to save the JSON data as *.php, however when i access the source URL from Chrome it displays the JSON data as normal.
What do i have to do here? Can anyone point me to a solution? Thx

running the returned json on http://jsonformatter.curiousconcept.com/
{
"result":[
{
"vendor_id":"726",
"vendor_name":"Scusa"
},
{
"vendor_id":"519",
"vendor_name":"Emilie French Restaurant and Bar"
},
{
"vendor_id":"482",
"vendor_name":"Cassis French Fine Dining"
},
{
"vendor_id":"435",
"vendor_name":"Asuka Japanase Dining"
},
{
"vendor_id":"12050",
"vendor_name":"Taipan"
},
{
"vendor_id":"12061",
"vendor_name":"Social House"
},
{
"vendor_id":"12103",
"vendor_name":"Harum Manis Indonesian Restaurant"
},
{
"vendor_id":"12193",
"vendor_name":"Nanny\'s Pavillion (Central Park)"
},
{
"vendor_id":"12272",
"vendor_name":"Bistro Baron"
},
{
"vendor_id":"20704",
"vendor_name":"Kitchenette (Central Park)"
},
{
"vendor_id":"21217",
"vendor_name":"Kitchenette (Plaza Indonesia)"
},
{
"vendor_id":"29859",
"vendor_name":"Momento Restaurant And Bar"
},
{
"vendor_id":"31055",
"vendor_name":"Tortuga Kitchen And Bar"
},
{
"vendor_id":"31056",
"vendor_name":"Tuck And Chug"
},
{
"vendor_id":"31060",
"vendor_name":"AUROZ Gourmet Grill"
}
]
}
it found invalid characters on this line
"vendor_name":"Nanny\'s Pavillion (Central Park)"

Related

How to get values from json object to php array

I am writing a php file for an application that allows the user to print a project report (with fpdf library).
The aim is to get datas from a db and then to build the PDF file dynamically.
The datas are stored in json.
It is ok for almost all datas, but there are some that I can't reach.
Here is an example :
First of all I already did this :
$Array['roof_coordinates'] = json_decode($Array['roof_coordinates'], false);
To get the number "nb" of "A523500" I'm doing like that :
$Array['roof_coordinates']->results->packingList->total->A523500->nb;
What am I doing wrong ?
"roofCoordinates": {
"results": {
"packingList": {
"total": {
"A523500": {
"ref": "STRA523500",
"nb": 16
},
"A523120": {
"ref": "STRA523120",
"nb": 0
},
"A522100": {
"ref": "STRA522100",
"nb": 8
},
},
}
},
}
And I tried to pass "true" to json_decode to convert objects to associative array but it doesn't seems to work...
Any help will be great !
Thank you in advance ;)
Make sure you are accessing the resulting data structure correctly, it should work whether you decode to an array or an object.
<?php
$json = <<<END
{
"roofCoordinates": {
"results": {
"packingList": {
"total": {
"A523500": {
"ref": "STRA523500",
"nb": 16
},
"A523120": {
"ref": "STRA523120",
"nb": 0
},
"A522100": {
"ref": "STRA522100",
"nb": 8
}
}
}
}
}
}
END;
$arrayResults = json_decode($json, true);
$nbFromArray = $arrayResults['roofCoordinates']['results']['packingList']['total']['A523500']['nb'];
$stdClassResults = json_decode($json);
$nbFromStdClass = $stdClassResults->roofCoordinates->results->packingList->total->A523500->nb;
assert($nbFromArray==16, 'Value should equal 16');
assert($nbFromArray==$nbFromStdClass, 'Values from either json_decode method should be equal');
echo 'From array: '.$nbFromArray.PHP_EOL;
echo 'From stdClass: '.$nbFromStdClass.PHP_EOL;

Getting error 404 when post data from local storage

I want to retrieve data from local storage and save it to the database. But when I'm trying to post, It gives an error 404. I know the file exist because when I remove this line :
formData.append('data',array);
my post is work. However, It works on localhost not on live server.
Whats wrong with this code.
Data from localstorage :
{
"mylist":"name of list",
"list": {
"category":[ {
"name":"Category 1",
"items":[ {
"name": "Category item list 1, "amount": "0"
}
]
}
,
{
"name":"Category 2",
"items":[ {
"name": "Category item list 2, "amount": "0"
}
]
}
]
}
}
React js trigger post :
function savepackList(){
const array = localStorage.getItem('list');
let formData = new FormData();
formData.append('type','savelist');
formData.append('data',array); //removing this it does not give a 404 error
axios.post('/request.php',formData)
.then(function (response) {
console.log(response.data);
})
.catch(function (error) {
console.log(error);
});return;
}
PHP code :
if($_POST['type']=='savelist'){
$obj = json_decode($_POST['data'],true);
echo json_encode($obj);
}

how to retrieve json data in sequence

i have simple json results like below
{
"code": 200,
"image": "https://example.com/image.jpg",
"result": [
{
"url": "https://example.com/1",
"label": "MP4"
},
{
"url": "https://example.com/2",
"label": "FLV"
},
{
"url": "https://example.com/3",
"label": "MP3"
},
{
"url": "https://example.com/4",
"label": "AVI"
},
{
"url": "https://example.com/5",
"label": "WMV"
}
]
}
as you can see, that there are so many different label paths, and sometimes the sequence of labels is not like the above, it's always changing (random), and I'm trying to get the MP3 label part but can not.
My question is, how to take the sequence of json which has MP3 label?
I have tried with the script below
$uri = json_decode(file_get_contents('https://example.com/json.json'),TRUE);
echo $uri['result'][2]['url'];
but as described above, the position of the MP3 label is always changing, is there a way to overcome it?
Try following code:
$nodesWithMp3Labels = array_map(function($a) {
if (strtolower($a["label"]) == "mp3") {
return $a;
}
}, $uri['result']);
Read more about array_map
With array_map, there will be some blank values if label is not mp3.
Another solution using simple foreach loop:
$nodesWithMp3Labels = [];
foreach ($uri['result'] as $a) {
if (strtolower($a["label"]) == "mp3") {
$nodesWithMp3Labels[] = $a;
}
}
You should use array_filter to get all the results with MP3 label:
$uri = json_decode(file_get_contents('https://example.com/json.json'), true);
$mp3 = array_filter($uri['result'], function($item) {
return $item['label'] === 'MP3';
})

FPDF: How to send JSON data to FPDF to generate PDF document in php

I have installed FPDF in my project, and want to generate a PDF file that contains data in a table format. I want to send JSON formatted data to FPDF. How to do this?
My JSON data is this:
{
"data": [
{
"ttfdgdf": "Input1"
},
{
"number": "12"
},
{
"gfgfgrg": "Sagar"
},
{
"bfghngfh": "12323445"
},
{
"mvbmnbm": "Hi this is sagar coming from satara\t\t\t\t"
},
{
"1425278796425303": "on"
},
{
"1425278796425605": "on"
},
{
"lkl": "Hiiii"
},
{
"lklkl": "1:12 AM"
}
]
}

Getting random output json

I trying to get a youtube channel banner from the youtube api.
But this is very weird.
I use this code:
$json1 = file_get_contents("http://gdata.youtube.com/feeds/api/partners/$channel/branding/default?key=[api-key]&alt=json");
$data1 = json_decode($json1, true);
$banner = $data1['entry']['yt$option'][15]['$t'];
echo $banner;
Now the weird thing, when i refresh te page i get everytime a new value. but sometimes it will stay one value and thats what i need but after some thime the value is changing...
This is the json code:
{
"version":"1.0",
"encoding":"UTF-8",
"entry":{
"xmlns":"http://www.w3.org/2005/Atom",
"xmlns$yt":"http://gdata.youtube.com/schemas/2007",
"id":{
"$t":"http://gdata.youtube.com/feeds/api/partners/rebasenetwork/branding/default"
},
"published":{
"$t":"2014-02-28T22:08:37.000Z"
},
"updated":{
"$t":"2014-02-28T22:08:37.000Z"
},
"category":[
{
"scheme":"http://schemas.google.com/g/2005#kind",
"term":"http://gdata.youtube.com/schemas/2007#branding"
}
],
"link":[
{
"rel":"self",
"type":"application/atom+xml",
"href":"http://gdata.youtube.com/feeds/api/partners/rebasenetwork/branding/default"
},
{
"rel":"edit",
"type":"application/atom+xml",
"href":"http://gdata.youtube.com/feeds/api/partners/rebasenetwork/branding/default"
}
],
"yt$option":[
{
"$t":"0",
"name":"channel.banner.image_height.int"
},
{
"$t":"http://i1.ytimg.com/u/Yd24MzwIk2gLiv79JNPGSA/channels4_mobile_banner_hd.jpg?v=52d6ad2b",
"name":"channel.banner.mobile.hd.image.url"
},
{
"$t":"http://i1.ytimg.com/u/Yd24MzwIk2gLiv79JNPGSA/channels4_tablet_banner.jpg?v=52d6ad2b",
"name":"channel.banner.tablet.medium.image.url"
},
{
"$t":"https://lh6.googleusercontent.com/-6xCc8OBY5Qk/UtatKX2WY3I/AAAAAAAAAAY/6-vpTOgd0yU/w854-fcrop64=1,00000000ffffffff/channels4_banner.jpg",
"name":"channel.banner.tv.low.image.url"
},
{
"$t":"https://lh6.googleusercontent.com/-6xCc8OBY5Qk/UtatKX2WY3I/AAAAAAAAAAY/6-vpTOgd0yU/w1280-fcrop64=1,00000000ffffffff/channels4_banner.jpg",
"name":"channel.banner.tv.medium.image.url"
},
{
"$t":"#000000",
"name":"channel.global.color"
},
{
"$t":"http://i1.ytimg.com/u/Yd24MzwIk2gLiv79JNPGSA/channels4_banner.jpg?v=52d6ad2b",
"name":"channel.banner.image.url"
},
{
"$t":"http://i1.ytimg.com/u/Yd24MzwIk2gLiv79JNPGSA/channels4_mobile_banner_low.jpg?v=52d6ad2b",
"name":"channel.banner.mobile.low.image.url"
},
{
"$t":"True",
"name":"channel.modules.show_comments.bool"
},
{
"$t":"https://lh6.googleusercontent.com/-6xCc8OBY5Qk/UtatKX2WY3I/AAAAAAAAAAY/6-vpTOgd0yU/w2120-fcrop64=1,00000000ffffffff/channels4_banner.jpg",
"name":"channel.banner.tv.image.url"
},
{
"$t":"http://i1.ytimg.com/u/Yd24MzwIk2gLiv79JNPGSA/channels4_tablet_banner_hd.jpg?v=52d6ad2b",
"name":"channel.banner.tablet.hd.image.url"
},
{
"$t":"Welcome to our network Channel. We help Youtubers to earn money and grow with their channel. Being famous on youtube is one of the things you dream about, isn't it? Let us help make your dreams true!\n\nWith us you can choose your own partnership, You also select what you want to earn from your youtube channel! Are you ready to start growing? Partner Now!",
"name":"channel.global.description.string"
},
{
"$t":"http://i1.ytimg.com/u/Yd24MzwIk2gLiv79JNPGSA/channels4_mobile_banner_medium_hd.jpg?v=52d6ad2b",
"name":"channel.banner.mobile.medium_hd.image.url"
},
{
"$t":"https://lh6.googleusercontent.com/-6xCc8OBY5Qk/UtatKX2WY3I/AAAAAAAAAAY/6-vpTOgd0yU/w1920-fcrop64=1,00000000ffffffff/channels4_banner.jpg",
"name":"channel.banner.tv.high.image.url"
},
{
"$t":"http://i1.ytimg.com/u/Yd24MzwIk2gLiv79JNPGSA/channels4_mobile_banner.jpg?v=52d6ad2b",
"name":"channel.banner.mobile.medium.image.url"
},
{
"$t":"http://i1.ytimg.com/u/Yd24MzwIk2gLiv79JNPGSA/channels4_mobile_banner.jpg?v=52d6ad2b",
"name":"channel.banner.mobile.image.url"
},
{
"$t":"True",
"name":"channel.related_channels.show.bool"
},
{
"$t":"KQvIpdiWrSI",
"name":"channel.unsubscribed_trailer.video_id.string"
},
{
"$t":"http://i1.ytimg.com/u/Yd24MzwIk2gLiv79JNPGSA/channels4_tablet_banner_extra_hd.jpg?v=52d6ad2b",
"name":"channel.banner.tablet.extra_hd.image.url"
},
{
"$t":"http://i1.ytimg.com/u/Yd24MzwIk2gLiv79JNPGSA/channels4_tablet_banner_low.jpg?v=52d6ad2b",
"name":"channel.banner.tablet.low.image.url"
},
{
"$t":"True",
"name":"channel.modules.moderate_comments.bool"
},
{
"$t":"Network partner partnership rebase network pewdiepie",
"name":"channel.global.keywords.string"
},
{
"$t":"Rebase Network",
"name":"channel.global.title.string"
},
{
"$t":"http://i1.ytimg.com/u/Yd24MzwIk2gLiv79JNPGSA/channels4_mobile_banner_extra_hd.jpg?v=52d6ad2b",
"name":"channel.banner.mobile.extra_hd.image.url"
}
]
}
}
This is the one i need(just the url):
{
"$t":"http://i1.ytimg.com/u/Yd24MzwIk2gLiv79JNPGSA/channels4_tablet_banner_extra_hd.jpg?v=52d6ad2b",
"name":"channel.banner.tablet.extra_hd.image.url"
},
Can someone help me please? i really don't understand why it's changing the value every refresh..
You can loop through the results and print out the one where the name matches channel.banner.mobile.extra_hd.image.url
$data1 = json_decode($json1, true);
foreach ($data1['entry']['yt$option'] AS $banner) {
if ($banner['name'] == 'channel.banner.mobile.extra_hd.image.url') {
print $banner['$t'];
}
}

Categories