I'm trying to display a JSON Array in php
JSON:
"members":[
{
"username":"Abstract",
"status":"online",
"bot":true,
"nick":"ViralBot \ud83d\ude0b",
"avatar_url":"https://cdn.discordapp.com/avatars/133718676741292033/59b492151d2c352f3ac5e249e4f04a82.jpg",
"avatar":"59b492151d2c352f3ac5e249e4f04a82",
"discriminator":"0612",
"id":"133718676741292033"
},
{
"username":"Aethex",
"status":"online",
"bot":true,
"game":{
"name":"aethex.xyz | -help"
},
"avatar_url":"https://cdn.discordapp.com/avatars/150300454708838401/bca569aaf0e1093ec8103e7bd3bedfb7.jpg",
"avatar":"bca569aaf0e1093ec8103e7bd3bedfb7",
"discriminator":"0394",
"id":"150300454708838401"
},
{
"username":"AIRHORN SOLUTIONS",
"status":"online",
"bot":true,
"nick":"MLG AIRHORNZ!",
"game":{
"name":"airhornbot.com"
},
"avatar_url":"https://cdn.discordapp.com/avatars/159800228088774656/96be9927ca947a75021db568485612fc.jpg",
"avatar":"96be9927ca947a75021db568485612fc",
"discriminator":"6723",
"id":"159800228088774656"
},
{
"username":"BuddyGang",
"status":"online",
"game":{
"name":"Custom Widgets API"
},
"avatar_url":"https://cdn.discordapp.com/avatars/97172171259904000/089e84734ea5ca86d20292abcca0982f.jpg",
"avatar":"089e84734ea5ca86d20292abcca0982f",
"discriminator":"1704",
"id":"97172171259904000"
},
{
"username":"danclay",
"status":"dnd",
"nick":"danclay \ud83d\ude09",
"avatar_url":"https://cdn.discordapp.com/avatars/97147476531757056/fa460980203d7838c8481c93c0361f6d.jpg",
"avatar":"fa460980203d7838c8481c93c0361f6d",
"discriminator":"2051",
"id":"97147476531757056"
},
{
"username":"dragongod100",
"status":"idle",
"avatar_url":"https://cdn.discordapp.com/avatars/96716994136514560/5393bc18ab9d7ddf19b6fe54575d93a7.jpg",
"avatar":"5393bc18ab9d7ddf19b6fe54575d93a7",
"discriminator":"5291",
"id":"96716994136514560"
},
{
"username":"Hax0nWax0ff",
"status":"idle",
"avatar_url":"https://cdn.discordapp.com/avatars/125738028793593856/ccf68e818fa44e342f60030a89a0fd53.jpg",
"avatar":"ccf68e818fa44e342f60030a89a0fd53",
"discriminator":"7691",
"id":"125738028793593856"
},
{
"username":"iiPsionic",
"status":"idle",
"avatar_url":"https://cdn.discordapp.com/avatars/142014347038818304/ba4bba83436f227933004694b4c184f2.jpg",
"avatar":"ba4bba83436f227933004694b4c184f2",
"discriminator":"6034",
"id":"142014347038818304"
},
{
"username":"RH1-N0",
"status":"online",
"bot":true,
"nick":"RHINO ATTACK!",
"avatar_url":"https://cdn.discordapp.com/avatars/135288293548883969/fe0070998d6c9378edf8243e3267f950.jpg",
"avatar":"fe0070998d6c9378edf8243e3267f950",
"discriminator":"6993",
"id":"135288293548883969"
},
{
"username":"WildBot",
"status":"online",
"bot":true,
"nick":"Cra-Cra Bot \ud83d\ude31",
"avatar_url":"https://cdn.discordapp.com/avatars/110462073074388992/f8143e56615e2107d9cc1ef35c0dfa9e.jpg",
"avatar":"f8143e56615e2107d9cc1ef35c0dfa9e",
"discriminator":"3942",
"id":"110462073074388992"
}
]
I want to be able to display it in numeric order of the "position" variable, with the avatar positioned next to the username then the game text.
HTML layout:
<img src="AVATAR_URL" width="50%"> USERNAME - <small>GAME</small><br>
try this code bro, hope it helps
$arr=json_decode($data,true);
// to prevent error undefined index
if(isset($arr['members'])) {
foreach($arr['members'] as $val){
$gmtxt=isset($val['game']['name'])?$val['game']['name']:"";
echo '<img src="'.$val['avatar_url'].'" width="50%"> '.$val['username'].' - <small>'.$gmtxt.'</small><br>';
}
}
Here the code:-
$data='{
"members": [
{
"username": "Abstract",
"status": "online",
"bot": true,
"nick": "ViralBot 😋",
"avatar_url": "https://cdn.discordapp.com/avatars/133718676741292033/59b492151d2c352f3ac5e249e4f04a82.jpg",
"avatar": "59b492151d2c352f3ac5e249e4f04a82",
"discriminator": "0612",
"id": "133718676741292033"
},
{
"username": "Aethex",
"status": "online",
"bot": true,
"game": {
"name": "aethex.xyz | -help"
},
"avatar_url": "https://cdn.discordapp.com/avatars/150300454708838401/bca569aaf0e1093ec8103e7bd3bedfb7.jpg",
"avatar": "bca569aaf0e1093ec8103e7bd3bedfb7",
"discriminator": "0394",
"id": "150300454708838401"
},
{
"username": "AIRHORN SOLUTIONS",
"status": "online",
"bot": true,
"nick": "MLG AIRHORNZ!",
"game": {
"name": "airhornbot.com"
},
"avatar_url": "https://cdn.discordapp.com/avatars/159800228088774656/96be9927ca947a75021db568485612fc.jpg",
"avatar": "96be9927ca947a75021db568485612fc",
"discriminator": "6723",
"id": "159800228088774656"
},
{
"username": "BuddyGang",
"status": "online",
"game": {
"name": "Custom Widgets API"
},
"avatar_url": "https://cdn.discordapp.com/avatars/97172171259904000/089e84734ea5ca86d20292abcca0982f.jpg",
"avatar": "089e84734ea5ca86d20292abcca0982f",
"discriminator": "1704",
"id": "97172171259904000"
},
{
"username": "danclay",
"status": "dnd",
"nick": "danclay 😉",
"avatar_url": "https://cdn.discordapp.com/avatars/97147476531757056/fa460980203d7838c8481c93c0361f6d.jpg",
"avatar": "fa460980203d7838c8481c93c0361f6d",
"discriminator": "2051",
"id": "97147476531757056"
},
{
"username": "dragongod100",
"status": "idle",
"avatar_url": "https://cdn.discordapp.com/avatars/96716994136514560/5393bc18ab9d7ddf19b6fe54575d93a7.jpg",
"avatar": "5393bc18ab9d7ddf19b6fe54575d93a7",
"discriminator": "5291",
"id": "96716994136514560"
},
{
"username": "Hax0nWax0ff",
"status": "idle",
"avatar_url": "https://cdn.discordapp.com/avatars/125738028793593856/ccf68e818fa44e342f60030a89a0fd53.jpg",
"avatar": "ccf68e818fa44e342f60030a89a0fd53",
"discriminator": "7691",
"id": "125738028793593856"
},
{
"username": "iiPsionic",
"status": "idle",
"avatar_url": "https://cdn.discordapp.com/avatars/142014347038818304/ba4bba83436f227933004694b4c184f2.jpg",
"avatar": "ba4bba83436f227933004694b4c184f2",
"discriminator": "6034",
"id": "142014347038818304"
},
{
"username": "RH1-N0",
"status": "online",
"bot": true,
"nick": "RHINO ATTACK!",
"avatar_url": "https://cdn.discordapp.com/avatars/135288293548883969/fe0070998d6c9378edf8243e3267f950.jpg",
"avatar": "fe0070998d6c9378edf8243e3267f950",
"discriminator": "6993",
"id": "135288293548883969"
},
{
"username": "WildBot",
"status": "online",
"bot": true,
"nick": "Cra-Cra Bot 😱",
"avatar_url": "https://cdn.discordapp.com/avatars/110462073074388992/f8143e56615e2107d9cc1ef35c0dfa9e.jpg",
"avatar": "f8143e56615e2107d9cc1ef35c0dfa9e",
"discriminator": "3942",
"id": "110462073074388992"
}
]
}';
$arr=json_decode($data,true);
foreach($arr['members'] as $val){
$gmtxt=isset($val['game']['name'])?$val['game']['name']:"";
echo '<img src="'.$val['avatar_url'].'" width="50%"> '.$val['username'].' - <small>'.$gmtxt.'</small><br>';
}
Related
this is my response
i need to read first line id value & url of sandbox
{
"id": "chg_g5Y25220190539Ob590811651",
"object": "charge",
"live_mode": false,
"api_version": "V2",
"method": "CREATE",
"status": "INITIATED",
"amount": 1.000,
"currency": "KWD",
"threeDSecure": false,
"card_threeDSecure": false,
"save_card": false,
"statement_descriptor": "Sample",
"description": "Test DESC",
"transaction":
{
"timezone": "UTC+03:00",
"created": "1573191592651",
"url": "https://sandbox.payments.tap.company/test_gosell/v2/payment/response.aspx?tap_chg=PsV8hZdr78kEIAOZl9XL1B5u3dXe%2bBQ%2b3Hrd%2fempxYs%3d&sess=noWi606EI%2bM%3d&token=PsV8hZdr78kEIAOZl9XL1B5u3dXe%2bBQ%2bTyYdQy9iGpS6aAXwKOqAdw%3d%3d",
"expiry": { "period": 30, "type": "MINUTE" },
"asynchronous": false
},
"reference": { "transaction": "txn_0001", "order": "ord_0001" },
"response": { "code": "100", "message": "Initiated" },
"receipt": { "email": true, "sms": true },
"customer": { "first_name": "yxy", "last_name": "ttt", "email": "xyx#xyz.com" },
"source": { "object": "source", "id": "src_card" },
"redirect": { "status": "PENDING", "url": "http://localhost:81/School/" },
"post": { "status": "PENDING", "url": "http://localhost:81/School/" } }
assuming you have your response from curl in a variable called $resp
$resp_decoded = json_decode($resp,TRUE);
print($resp_decoded['id']);
print($resp_decoded['transaction']['url']);
So I have
{
"members": [
{
"username": "John",
"status": "offline",
"avatar_url": "...",
"id": "830232882252102064"
},
{
"username": "Momo",
"status": "online",
"avatar_url": "...",
"id": "259137993351102464"
}
]
}
How do I count (in php) how many users are offline and how many users are online and return them into a value like $memonline and $memoffline.
Here in below code we are getting values of all statuses and then we are counting values of all statuses.
Try this code snippet here
<?php
ini_set('display_errors', 1);
$string='{
"channels": [
{
"position": 13,
"id": "304700935878213642",
"name": "20KBPS"
},
{
"position": 12,
"id": "304700895978061835",
"name": "50KBPS"
},
{
"position": 11,
"id": "304701193261809672",
"name": "70KBPS"
},
{
"position": 10,
"id": "304701326288224256",
"name": "90KBPS"
},
{
"position": 1,
"id": "304699877621891072",
"name": "=================="
},
{
"position": 9,
"id": "304700570592346114",
"name": "=================="
},
{
"position": 4,
"id": "304701407221514240",
"name": "=================="
},
{
"position": 14,
"id": "304700808883339264",
"name": "=================="
},
{
"position": 2,
"id": "304700525939523584",
"name": "Channel 1"
},
{
"position": 3,
"id": "304700547426942976",
"name": "Channel 2"
},
{
"position": 0,
"id": "304692483973971990",
"name": "General Channel"
},
{
"position": 6,
"id": "304701637446991873",
"name": "Private/2"
},
{
"position": 5,
"id": "304701480605319178",
"name": "Private/2"
},
{
"position": 7,
"id": "304701680010788866",
"name": "Private/3"
},
{
"position": 8,
"id": "304701738999611394",
"name": "Private/3"
},
{
"position": 15,
"id": "304700776691793921",
"name": "Trash Bin"
}
],
"instant_invite": null,
"id": "304692483973971989",
"members": [
{
"username": "Dyno",
"status": "online",
"bot": true,
"game": {
"name": "dynobot.net | ?help"
},
"avatar_url": "https://cdn.discordapp.com/avatars/155149108183695360/5aeb68c29b56b3d92eddb6f46df5051c.jpg",
"avatar": "5aeb68c29b56b3d92eddb6f46df5051c",
"discriminator": "3861",
"id": "155149108183695360"
},
{
"username": "Momo",
"status": "online",
"bot": true,
"game": {
"name": "$help | $info"
},
"avatar_url": "https://cdn.discordapp.com/avatars/259137993351102464/a3005ab7aff3eb829fecf375931a76f1.jpg",
"avatar": "a3005ab7aff3eb829fecf375931a76f1",
"discriminator": "4649",
"id": "259137993351102464"
},
{
"username": "Ronny Dark",
"status": "online",
"avatar_url": "https://cdn.discordapp.com/avatars/152855546574143492/6d9b98972ca6f0308be4dd2aec5aaca3.jpg",
"avatar": "6d9b98972ca6f0308be4dd2aec5aaca3",
"discriminator": "1480",
"id": "152855546574143492"
}
],
"name": "Server name"
}';
$array=json_decode($string,true);
$result=array_column($array["members"],"status");
$members=array_count_values($result);
echo isset($members["offline"]) ? $members["offline"] : 0;
I'm into a project where I'm trying to get information from a json response (Again), that I got from google+.
I need to return an array after populating it with some information from the json response.
The [modified] json response is thus:
{
"items": [
{
"kind": "plus#activity",
"etag": "\"gLJf7Ldsgfsdgsgsf9ES9mEc/LFcp0tW7Ffgfsdgdf4Yfgdff3Z8\"",
"title": "first title",
"published": "2015-08-13T12:23:36.316Z",
"updated": "2015-08-13T12:23:36.316Z",
"id": "z12njfgogxirwdsoihosdhgjsdghusdn",
"url": "https://plus.google.com/+ABCBEHGHIJ/posts/8QZBMRjHVMF",
"actor": {
"id": "210973863758690009009",
"displayName": "a name",
"url": "https://plus.google.com/210973863758690009009",
"image": {
"url": "https://lh4.googleusercontent.com/-ihfjbhjbsduj/AAAfgdagAAAI/AAAgdfgfdAPQ/oBXvfgdgdf1aeLQ/photo.jpg?sz=50"
},
"verification": {
"adHocVerified": "UNKNOWN_VERIFICATION_STATUS"
}
},
"verb": "post",
"object": {
"objectType": "note",
"actor": {
"verification": {
"adHocVerified": "UNKNOWN_VERIFICATION_STATUS"
}
},
"content": " \u003ca rel=\"nofollow\" class=\"ot-hashtag\" href=\"https://plus.google.com/s/%oko\"\u003es\u003c/a\u003e\ufeff",
"url": "https://plus.google.com/+ABCBEHGHIJ/posts/8QZiUyuyuobsvASKKHVMF",
"replies": {
"totalItems": 0,
"selfLink": "https://www.googleapis.com/plus/v1/activities/z12ZiUyuyuobsvASKKcxqnn/comments"
},
"plusoners": {
"totalItems": 0,
"selfLink": "https://www.googleapis.com/plus/v1/activities/z12njfgogZiUyuyuobsvASKKcxqnn/people/plusoners"
},
"resharers": {
"totalItems": 0,
"selfLink": "https://www.googleapis.com/plus/v1/activities/z12njfZiUyuyuobsvASKKqnn/people/resharers"
},
"attachments": [
{
"objectType": "photo",
"displayName": "Photo 1",
"id": "210973863758690009009.6618960307328673257603073286732572860914",
"content": "123",
"url": "https://plus.google.com/photos/21096189603073286732572860914009/albums/616189603073286732572860914457/66189603073286732572860914385860914",
"image": {
"url": "https://lh5.googleusercontent.com/-2dgjFRZCII8/VcyMOOCHLTI/BAAHYGHDSJH/0XtjUD1perY/w506-h750/11017878_1655020691394612_1878283374465430482_n.jpg",
"type": "image/jpeg"
},
"fullImage": {
"url": "https://lh5.googleusercontent.com/-2dgjFRSDSDD8/VcyMSDSDSI/BAAHYGHDSJHDSIKA/0XtjIUYDIGIDGerY/11017878_1655020DSDKHSJ612_1878283374SDSDSD30482_n.jpg",
"type": "image/jpeg",
"height": 540,
"width": 540
}
}
]
},
"provider": {
"title": "Google+"
},
"access": {
"kind": "plus#acl",
"description": "Public",
"items": [
{
"type": "public"
}
]
}
},
{
"kind": "plus#activity",
"etag": "\"gLJf7LwN3wOpLHXk4IeQ9ES9mEc/TUMFP7Wc8QTGatBg1vGSEbjPPDE\"",
"title": "second title",
"published": "2015-08-13T11:57:49.262Z",
"updated": "2015-08-13T11:57:49.262Z",
"id": "z1iohyfsdugsjhockob0n232vjiz5xancxqnn",
"url": "https://plus.google.com/+ABCBEHGHIJ/posts/4DVBTreNkyA",
"actor": {
"id": "210973863758690009009",
"displayName": "a name",
"url": "https://plus.google.com/210973863758690009009",
"image": {
"url": "https://lh4.googleusercontent.com/-ihfjbhjbsduj/BAAHYGHDSJH/BAAHYGHDSJH/oBXvDSDASAeLQ/photo.jpg?sz=50"
},
"verification": {
"adHocVerified": "UNKNOWN_VERIFICATION_STATUS"
}
},
"verb": "post",
"object": {
"objectType": "note",
"actor": {
"verification": {
"adHocVerified": "UNKNOWN_VERIFICATION_STATUS"
}
},
"content": "07012345678\ufeff",
"url": "https://plus.google.com/+ABCBEHGHIJ/posts/4DVBiodjjdmknaayA",
"replies": {
"totalItems": 0,
"selfLink": "https://www.googleapis.com/plus/v1/activities/pfjknsfubsnjbsb0n232vjiz5xancxqnn/comments"
},
"plusoners": {
"totalItems": 0,
"selfLink": "https://www.googleapis.com/plus/v1/activities/pfjknsfubsnjbsb0n232vjiz5xancxqnn/people/plusoners"
},
"resharers": {
"totalItems": 0,
"selfLink": "https://www.googleapis.com/plus/v1/activities/pfjknsfubsnjbsb0n232vjiz5xancxqnn/people/resharers"
},
"attachments": [
{
"objectType": "photo",
"displayName": "Photo 2",
"id": "210973863758690009009.6837572367923789379",
"content": "456",
"url": "https://plus.google.com/photos/210973863758690009009/albums/6947862082892799114865/69843868408496745035842",
"image": {
"url": "https://lh3.googleusercontent.com/-Tvib82iMpsk/VcyGKh3UGAI/ADLJSKNSHJBSDNB AAQM/cyhYX3SifDY/w506-h750/1024-2006_1011_093752.jpg",
"type": "image/jpeg"
},
"fullImage": {
"url": "https://lh3.googleusercontent.com/-Tvijihjsdbnjgsdupsk/VcfjhjfsbjgsugAI/ApkdlnsdjkbjsdAQM/cyhYX3SifDY/w1024-h768/1024-2006_1011_093752.jpg",
"type": "image/jpeg",
"height": 768,
"width": 1024
}
}
]
},
"provider": {
"title": "Google+"
},
"access": {
"kind": "plus#acl",
"description": "Public",
"items": [
{
"type": "public"
}
]
}
},
{
"kind": "plus#activity",
"etag": "\"gLJfnjsdjnvdsvhsdIeQ9EpoifkjnfmEc/P4ihjsadiadakhlbhvdshvfXan4\"",
"title": "third title",
"published": "2015-08-12T22:57:22.010Z",
"updated": "2015-08-12T22:57:22.010Z",
"url": "https://plus.google.com/+ABCBEHGHIJ/posts/iiuhfdjhfidpnbvcW",
"actor": {
"displayName": "a name",
"url": "https://plus.google.com/210973863758690009009",
"image": {
"url": "https://lh4.googleusercontent.com/-ihfjbhjbsduj/BAJDHJDSIDKAI/PODSIOUSDIKAAPQ/oBXvlp1aeLQ/photo.jpg?sz=50"
},
"verification": {
"adHocVerified": "UNKNOWN_VERIFICATION_STATUS"
}
},
"verb": "post",
"object": {
"objectType": "note",
"actor": {
"verification": {
"adHocVerified": "UNKNOWN_VERIFICATION_STATUS"
}
},
"content": " \u003ca rel=\"nofollow\" class=\"ot-hashtag\" href=\"https://plus.google.com/s/IUYDHJDSUGDSHGY\"\u003eIUYDHJDSUGDSHGY\u003c/a\u003e \ufeff",
"url": "https://plus.google.com/+ABCBEHGHIJ/posts/iukdjkdowqvcW",
"replies": {
"totalItems": 0,
"selfLink": "https://www.googleapis.com/plus/v1/activities/uihfjbsjkbsfdnfffdfdpokrouh3quig/comments"
},
"plusoners": {
"totalItems": 0,
"selfLink": "https://www.googleapis.com/plus/v1/activities/uihfjbsjkbsfdnfffdfdpokrouh3quig/people/plusoners"
},
"resharers": {
"totalItems": 0,
"selfLink": "https://www.googleapis.com/plus/v1/activities/uihfjbsjkbsfdnfffdfdpokrouh3quig/people/resharers"
},
"attachments": [
{
"objectType": "photo",
"displayName": "Photo 3",
"content": "789",
"url": "https://plus.google.com/photos/210973863758690009009/albums/6182262671805531297/618654563857564534922",
"image": {
"url": "https://lh3.googleusercontent.com/-uihfjbsdhgsdilop/VcvPJmJ0bUI/wERRDFFAAAAN8/X6J9fHJVBFDVSFVSak/w506-h750/PicsArt_1438914428134.jpg",
"type": "image/jpeg"
},
"fullImage": {
"url": "https://lh3.googleusercontent.com/-uihfjbsdhgsdilop/VcvPJmJ0bUI/wERRDFFAAAAN8/XyghFGF2TgBak/w562-h562/PicsArt_1438914428134.jpg",
"type": "image/jpeg",
"height": 562,
"width": 562
}
}
]
},
"provider": {
"title": "Google+"
},
"access": {
"kind": "plus#acl",
"description": "Public",
"items": [
{
"type": "public"
}
]
}
},
{
"kind": "plus#activity",
"etag": "\"gLJioyuhgdugudsgugc/UGHAoodpuskhjhgsdjdbY0\"",
"title": "fourth title",
"published": "2011-09-26T17:13:14.309Z",
"updated": "2011-09-26T17:13:14.309Z",
"id": "z12odlnbgmavzdh4k04cgfhxvwrouh3quig",
"url": "https://plus.google.com/+ABCBEHGHIJ/posts/R9zieiuhdjghdgBFu",
"actor": {
"id": "210973863758690009009",
"displayName": "a name",
"url": "https://plus.google.com/210973863758690009009",
"image": {
"url": "https://lh4.googleusercontent.com/-ihfjbhjbsduj/AKHDSJHSDGAAI/AUIDHIJGDSHGHAPQ/oBXvlp1aeLQ/photo.jpg?sz=50"
},
"verification": {
"adHocVerified": "UNKNOWN_VERIFICATION_STATUS"
}
},
"verb": "post",
"object": {
"objectType": "note",
"actor": {
"verification": {
"adHocVerified": "UNKNOWN_VERIFICATION_STATUS"
}
},
"content": "UIHUIGG FJGF GU",
"url": "https://plus.google.com/+ABCBEHGHIJ/posts/R9zStuw9BFu",
"replies": {
"totalItems": 0,
"selfLink": "https://www.googleapis.com/plus/v1/activities/oilzdh4k04cgpooiyuewfouh3quig/comments"
},
"plusoners": {
"totalItems": 1,
"selfLink": "https://www.googleapis.com/plus/v1/activities/oilzdh4k04cgpooiyuewfouh3quig/people/plusoners"
},
"resharers": {
"totalItems": 0,
"selfLink": "https://www.googleapis.com/plus/v1/activities/zkjhsdsfgiooi;oilzdh4k04cgpooiyuewfouh3quig/people/resharers"
}
},
"provider": {
"title": "Google+"
},
"access": {
"kind": "plus#acl",
"description": "Public",
"items": [
{
"type": "public"
}
]
}
},
{
"kind": "plus#activity",
"etag": "\"oilzdh4k04cgpooiyuewfouh3quig/oilzdh4k04cgpooiyuewfouh3quig\"",
"title": "fifth title",
"published": "2011-09-26T17:09:23.712Z",
"updated": "2011-09-26T17:09:23.712Z",
"id": "z13msxkxfnjjvp04p04cgfhxvwrouh3quig",
"url": "https://plus.google.com/+ABCBEHGHIJ/posts/RvXF5ufy23n",
"actor": {
"id": "210973863758690009009",
"displayName": "a name",
"url": "https://plus.google.com/210973863758690009009",
"image": {
"url": "https://lh4.googleusercontent.com/-ihfjbhjbsduj/AAAAAAAAAAI/AAAAAAAAAPQ/oilzdh4k04cgpooiyuewfouh3quig/photo.jpg?sz=50"
},
"verification": {
"adHocVerified": "UNKNOWN_VERIFICATION_STATUS"
}
},
"verb": "post",
"object": {
"objectType": "note",
"actor": {
"verification": {
"adHocVerified": "UNKNOWN_VERIFICATION_STATUS"
}
},
"content": "witout 'ME' it's jst AWESO....\ufeff",
"url": "https://plus.google.com/+ABCBEHGHIJ/posts/RvXF5ufy23n",
"replies": {
"totalItems": 0,
"selfLink": "https://www.googleapis.com/plus/v1/activities/oilzdh4k04cgpooiyuewfouh3quig/comments"
},
"plusoners": {
"totalItems": 0,
"selfLink": "https://www.googleapis.com/plus/v1/activities/oilzdh4k04cgpooiyuewfouh3quig/people/plusoners"
},
"resharers": {
"totalItems": 0,
"selfLink": "https://www.googleapis.com/plus/v1/activities/gLJfoiojkdwjkshdikhdhisEc/people/resharers"
}
},
"provider": {
"title": "Google+"
},
"access": {
"kind": "plus#acl",
"description": "Public",
"items": [
{
"type": "public"
}
]
}
},
{
"kind": "plus#activity",
"etag": "\"gLJfoiojkdwjkshdikhdhisEc/gLJfoiojkdwjkshdikhdhisEc\"",
"title": "sixth title",
"published": "2011-09-26T17:07:05.897Z",
"updated": "2011-09-26T17:07:05.897Z",
"id": "gLJfoiojkdwjkshdikhdhisEc",
"url": "https://plus.google.com/+ABCBEHGHIJ/posts/gLJfoiojkdwjkshdikhdhisEc",
"actor": {
"id": "210973863758690009009",
"displayName": "a name",
"url": "https://plus.google.com/210973863758690009009",
"image": {
"url": "https://lh4.googleusercontent.com/-ihfjbhjbsduj/AASKJAKHJGASQAPQ/AASKJAKHJGASQAPQ/gLJfoiojkdwjkshdikhdhisEc/photo.jpg?sz=50"
},
"verification": {
"adHocVerified": "UNKNOWN_VERIFICATION_STATUS"
}
},
"verb": "post",
"object": {
"objectType": "note",
"actor": {
"verification": {
"adHocVerified": "UNKNOWN_VERIFICATION_STATUS"
}
},
"content": "dere's an xception 2 evry rule, xcept dis 1..\ufeff",
"url": "https://plus.google.com/+ABCBEHGHIJ/posts/N6QTZt2XvSR",
"replies": {
"totalItems": 0,
"selfLink": "https://www.googleapis.com/plus/v1/activities/gLJfoiojkdwjkshdikhdhisEc/comments"
},
"plusoners": {
"totalItems": 0,
"selfLink": "https://www.googleapis.com/plus/v1/activities/gLJfoiojkdwjkshdikhdhisEc/people/plusoners"
},
"resharers": {
"totalItems": 0,
"selfLink": "https://www.googleapis.com/plus/v1/activities/gLJfoiojkdwjkshdikhdhisEc/people/resharers"
}
},
"provider": {
"title": "Google+"
},
"access": {
"kind": "plus#acl",
"description": "Public",
"items": [
{
"type": "public"
}
]
}
}
]
}
this is what I'm doing:
function google_pluss() {
$file_dir = strtolower(realpath(APPPATH . '..' . DIRECTORY_SEPARATOR . 'assets'));
$json_file = $file_dir . DIRECTORY_SEPARATOR . 'stackoverflow' . '.json'; // I have the file locally...
$json_data = json_decode(file_get_contents($json_file));
$data = array();
if (isset($json_data)) {
$x = 0;
foreach ($json_data->items as $item) {
if (isset($item->object->attachments)) {
foreach ($item->object->attachments as $attachment) {
$data['number'][$x] = $attachment->content;
$data['image'][$x] = $attachment->fullImage->url;
}
$data['title'][$x] = $item->title;
$x++;
}
}
}
print_r($data);
}
this is the output I get:
Array
(
[number] => Array
(
[0] => 123
[1] => 456
[2] => 789
)
[image] => Array
(
[0] => https://lh5.googleusercontent.com/-2dgjFRSDSDD8/VcyMSDSDSI/BAAHYGHDSJHDSIKA/0XtjIUYDIGIDGerY/11017878_1655020DSDKHSJ612_1878283374SDSDSD30482_n.jpg
[1] => https://lh3.googleusercontent.com/-Tvijihjsdbnjgsdupsk/VcfjhjfsbjgsugAI/ApkdlnsdjkbjsdAQM/cyhYX3SifDY/w1024-h768/1024-2006_1011_093752.jpg
[2] => https://lh3.googleusercontent.com/-uihfjbsdhgsdilop/VcvPJmJ0bUI/wERRDFFAAAAN8/XyghFGF2TgBak/w562-h562/PicsArt_1438914428134.jpg
)
[title] => Array
(
[0] => first title
[1] => second title
[2] => third title
)
)
But, this is the output I actually need:
Array
(
[0] => Array
(
[number] => 123
[image] => https://lh5.googleusercontent.com/-2dgjFRSDSDD8/VcyMSDSDSI/BAAHYGHDSJHDSIKA/0XtjIUYDIGIDGerY/11017878_1655020DSDKHSJ612_1878283374SDSDSD30482_n.jpg
[title] => first title
)
[1] => Array
(
[number] => 456
[image] => https://lh3.googleusercontent.com/-Tvijihjsdbnjgsdupsk/VcfjhjfsbjgsugAI/ApkdlnsdjkbjsdAQM/cyhYX3SifDY/w1024-h768/1024-2006_1011_093752.jpg
[title] => second title
)
[2] => Array
(
[number] => 789
[image] => https://lh3.googleusercontent.com/-uihfjbsdhgsdilop/VcvPJmJ0bUI/wERRDFFAAAAN8/XyghFGF2TgBak/w562-h562/PicsArt_1438914428134.jpg
[title] => third title
)
)
I'd really appreciate if someone shows me how to achieve this, thank you.
btw, I'm using the $x variable and incrementing it, to stop the loop where the array doesn't contain attachments. I'd be glad too, if shown how to do this neater.
At first glance, I would say change
foreach ($item->object->attachments as $attachment) {
$data['number'][$x] = $attachment->content;
$data['image'][$x] = $attachment->fullImage->url;
}
to
foreach ($item->object->attachments as $attachment) {
$data[$x]['number'] = $attachment->content;
$data[$x]['image'] = $attachment->fullImage->url;
}
I have a JSON from telegram, In php How can find that if it is a text do something, but if its photo, audio or ... do something else. this part is located after date, how can I access it?
I tried by access it by index like ['message'][1] but I got error.
{
"ok": true,
"result": [
{
"update_id": 30213598,
"message": {
"message_id": 41,
"from": {
"id": 102768333,
"first_name": "QWr_1",
"username": "john"
},
"chat": {
"id": 108768733,
"first_name": "QWr_1",
"username": "john"
},
"date": 1439233778,
"text": "this is a test"
}
},
{
"update_id": 30213599,
"message": {
"message_id": 46,
"from": {
"id": 108768733,
"first_name": "QWr_1",
"username": "john"
},
"chat": {
"id": 108768733,
"first_name": "QWr_1",
"username": "john"
},
"date": 1439234126,
"photo": [
{
"file_id": "AgADBAAD4acxG92tewa8uRLrpBBo-h1icjAABCohC6S9uPdhViIAAgI",
"file_size": 1923,
"width": 90,
"height": 90
},
{
"file_id": "AgADBAAD4acxG92tewa8uRLrpBBo-h1icjAABOT4Q5CJoFOVVSIAAgI",
"file_size": 24105,
"width": 320,
"height": 320
},
{
"file_id": "AgADBAAD4acxG92tewa8uRLrpBBo-h1icjAABK2WgDnJ74jlVCIAAgI",
"file_size": 57650,
"width": 640,
"height": 640
}
]
}
},
You need to use json_decode function.
I use foreach loop then echo $item['message']['message_id'] to display message_id in following example.
Try Example
$json = <<<JSON
{
"ok": true,
"result": [
{
"update_id": 30213598,
"message": {
"message_id": 41,
"from": {
"id": 102768333,
"first_name": "QWr_1",
"username": "john"
},
"chat": {
"id": 108768733,
"first_name": "QWr_1",
"username": "john"
},
"date": 1439233778,
"text": "this is a test"
}
},
{
"update_id": 30213599,
"message": {
"message_id": 46,
"from": {
"id": 108768733,
"first_name": "QWr_1",
"username": "john"
},
"chat": {
"id": 108768733,
"first_name": "QWr_1",
"username": "john"
},
"date": 1439234126,
"photo": [
{
"file_id": "AgADBAAD4acxG92tewa8uRLrpBBo-h1icjAABCohC6S9uPdhViIAAgI",
"file_size": 1923,
"width": 90,
"height": 90
},
{
"file_id": "AgADBAAD4acxG92tewa8uRLrpBBo-h1icjAABOT4Q5CJoFOVVSIAAgI",
"file_size": 24105,
"width": 320,
"height": 320
},
{
"file_id": "AgADBAAD4acxG92tewa8uRLrpBBo-h1icjAABK2WgDnJ74jlVCIAAgI",
"file_size": 57650,
"width": 640,
"height": 640
}
]
}
}
]
}
JSON;
$json_data = json_decode($json, true);
foreach($json_data['result'] as $item)
{
if(isset($item['message']['text']))
{
echo $item['message']['text']."<br />";
}
if(isset($item['message']['photo']))
{
foreach($item['message']['photo'] as $photo)
{
echo "file_id: ".$photo['file_id']."<br />";
echo "file_size: ".$photo['file_size']."<br />";
}
}
}
Im working with Laravel 5.1 and have a small issue.
I am sending a JSON response from Instagram's API for most popular media from my controller and want to grab that response in my view with ajax and show it on page to the user without loading the entire page.
I am trying to grab the username and display it in my view to the user.
Right now nothing happens and the console does not show anything, it is empty.
This is my code:
CONROLLER:
public function PopularPics(City $city){
try{
$jsonData = $city->getMostPopularPics();
return response()->json($jsonData);
}catch(\Exception $ex){
return $ex->getCode();
}
}
VIEW
<ul id="theData">
</ul>
<script>
$(function(){
$("#JRequest").click(function(e){
e.preventDefault();
$.ajax({
type: "GET",
url: "popular",
dataType:"json",
success:function(items){
$.each(items.data,function(i,item){
console.log(item.user.username);
});
}
});
});
});
</script>
INSTAGRAM JSON
{
"data": [{
"type": "image",
"users_in_photo": [],
"filter": "Gotham",
"tags": [],
"comments": { ... },
"caption": {
"created_time": "1296656006",
"text": "ãã¼ãâ¥ã¢ããªå§ãã¦ä½¿ã£ã¦ã¿ãã(^^)",
"from": {
"username": "cocomiin",
"full_name": "",
"type": "user",
"id": "1127272"
},
"id": "26329105"
},
"likes": {
"count": 35,
"data": [{
"username": "mikeyk",
"full_name": "Kevin S",
"id": "4",
"profile_picture": "..."
}, {...subset of likers...}]
},
"link": "http://instagr.am/p/BV5v_/",
"user": {
"username": "cocomiin",
"full_name": "Cocomiin",
"profile_picture": "http://distillery.s3.amazonaws.com/profiles/profile_1127272_75sq_1296145633.jpg",
"id": "1127272"
},
"created_time": "1296655883",
"images": {
"low_resolution": {
"url": "http://distillery.s3.amazonaws.com/media/2011/02/01/34d027f155204a1f98dde38649a752ad_6.jpg",
"width": 306,
"height": 306
},
"thumbnail": {
"url": "http://distillery.s3.amazonaws.com/media/2011/02/01/34d027f155204a1f98dde38649a752ad_5.jpg",
"width": 150,
"height": 150
},
"standard_resolution": {
"url": "http://distillery.s3.amazonaws.com/media/2011/02/01/34d027f155204a1f98dde38649a752ad_7.jpg",
"width": 612,
"height": 612
}
},
"id": "22518783",
"location": null
},
{
"type": "video",
"videos": {
"low_resolution": {
"url": "http://distilleryvesper9-13.ak.instagram.com/090d06dad9cd11e2aa0912313817975d_102.mp4",
"width": 480,
"height": 480
},
"standard_resolution": {
"url": "http://distilleryvesper9-13.ak.instagram.com/090d06dad9cd11e2aa0912313817975d_101.mp4",
"width": 640,
"height": 640
},
"users_in_photo": null,
"filter": "Vesper",
"tags": [],
"comments": {
"data": [{
"created_time": "1279332030",
"text": "Love the sign here",
"from": {
"username": "mikeyk",
"full_name": "Mikey Krieger",
"id": "4",
"profile_picture": "http://distillery.s3.amazonaws.com/profiles/profile_1242695_75sq_1293915800.jpg"
},
"id": "8"
},
{
"created_time": "1279341004",
"text": "Chilako taco",
"from": {
"username": "kevin",
"full_name": "Kevin S",
"id": "3",
"profile_picture": "..."
},
"id": "3"
}],
"count": 2
},
"caption": null,
"likes": {
"count": 1,
"data": [{
"username": "mikeyk",
"full_name": "Mikeyk",
"id": "4",
"profile_picture": "..."
}]
},
"link": "http://instagr.am/p/D/",
"user": {
"username": "kevin",
"full_name": "Kevin S",
"profile_picture": "...",
"id": "3"
},
"created_time": "1279340983",
"images": {
"low_resolution": {
"url": "http://distilleryimage2.ak.instagram.com/11f75f1cd9cc11e2a0fd22000aa8039a_6.jpg",
"width": 306,
"height": 306
},
"thumbnail": {
"url": "http://distilleryimage2.ak.instagram.com/11f75f1cd9cc11e2a0fd22000aa8039a_5.jpg",
"width": 150,
"height": 150
},
"standard_resolution": {
"url": "http://distilleryimage2.ak.instagram.com/11f75f1cd9cc11e2a0fd22000aa8039a_7.jpg",
"width": 612,
"height": 612
}
},
"id": "3",
"location": null
},
...]
}