Всем привет, вот ВК присылает JSON вроде такого:
{
"type": "message_new",
"object": {
"date": 1543246468,
"from_id": 515828454,
"id": 133013,
"out": 0,
"peer_id": 515828454,
"text": "",
"conversation_message_id": 2361,
"fwd_messages": [],
"important": false,
"random_id": 0,
"attachments": [
{
"type": "photo",
"photo": {
"id": 456239327,
"album_id": -15,
"owner_id": 515828454,
"sizes": [
{
"type": "m",
"url": "https:\/\/sun9-6.userapi.com\/c540100\/v540100328\/6fa71\/dHtVkQA6VWY.jpg",
"width": 118,
"height": 130
},
{
"type": "o",
"url": "https:\/\/sun9-5.userapi.com\/c540100\/v540100328\/6fa73\/UK6ebeydZ14.jpg",
"width": 130,
"height": 144
},
{
"type": "p",
"url": "https:\/\/sun9-1.userapi.com\/c540100\/v540100328\/6fa74\/Dw2Lmcufbrc.jpg",
"width": 200,
"height": 221
},
{
"type": "q",
"url": "https:\/\/sun9-6.userapi.com\/c540100\/v540100328\/6fa75\/MoDZzCcBkrQ.jpg",
"width": 320,
"height": 353
},
{
"type": "r",
"url": "https:\/\/sun9-3.userapi.com\/c540100\/v540100328\/6fa76\/fVo81F2GeeI.jpg",
"width": 480,
"height": 530
},
{
"type": "s",
"url": "https:\/\/sun9-4.userapi.com\/c540100\/v540100328\/6fa70\/2eBzwe4T8kc.jpg",
"width": 68,
"height": 75
},
{
"type": "x",
"url": "https:\/\/sun9-4.userapi.com\/c540100\/v540100328\/6fa72\/1_V4Eu_MF7w.jpg",
"width": 480,
"height": 530
}
],
"text": "",
"date": 1542804864,
"access_key": "74b9f9163c1a2f93ac"
}
}
],
"is_hidden": false
},
"group_id": 173657849
}
вопрос, как из JSON такого рода, доставать ссылку на фотографию, найбольшего размера?
Можете попробовать так։
$json = json_decode('{"type":"message_new","object":{"date":1543246468,"from_id":515828454,"id":133013,"out":0,"peer_id":515828454,"text":"","conversation_message_id":2361,"fwd_messages":[],"important":false,"random_id":0,"attachments":[{"type":"photo","photo":{"id":456239327,"album_id":-15,"owner_id":515828454,"sizes":[{"type":"m","url":"https:\/\/sun9-6.userapi.com\/c540100\/v540100328\/6fa71\/dHtVkQA6VWY.jpg","width":118,"height":130},{"type":"o","url":"https:\/\/sun9-5.userapi.com\/c540100\/v540100328\/6fa73\/UK6ebeydZ14.jpg","width":130,"height":144},{"type":"p","url":"https:\/\/sun9-1.userapi.com\/c540100\/v540100328\/6fa74\/Dw2Lmcufbrc.jpg","width":200,"height":221},{"type":"q","url":"https:\/\/sun9-6.userapi.com\/c540100\/v540100328\/6fa75\/MoDZzCcBkrQ.jpg","width":320,"height":353},{"type":"r","url":"https:\/\/sun9-3.userapi.com\/c540100\/v540100328\/6fa76\/fVo81F2GeeI.jpg","width":480,"height":530},{"type":"s","url":"https:\/\/sun9-4.userapi.com\/c540100\/v540100328\/6fa70\/2eBzwe4T8kc.jpg","width":68,"height":75},{"type":"x","url":"https:\/\/sun9-4.userapi.com\/c540100\/v540100328\/6fa72\/1_V4Eu_MF7w.jpg","width":480,"height":530}],"text":"","date":1542804864,"access_key":"74b9f9163c1a2f93ac"}}],"is_hidden":false},"group_id":173657849}');
$image = $json->object->attachments[0]->photo->sizes;
$key = array_keys($image, max($image))[0];
$bigImage = $image[$key]->url;
print_r($bigImage);
/*
Самый большой формат
https://sun9-4.userapi.com/c540100/v540100328/6fa72/1_V4Eu_MF7w.jpg
*/
На самом деле там как понимаю "type": "m" и т.п. означают размеры и можно брать не вычислениями, а по типу. Курите мануал, предполагаю что "type": "x" (XL) это самая большая.
Но предложу и свой вариант кода с сортировкой (мне кажется он правильнее чем у @Arsen):
$json = json_decode('{"type":"message_new","object":{"date":1543246468,"from_id":515828454,"id":133013,"out":0,"peer_id":515828454,"text":"","conversation_message_id":2361,"fwd_messages":[],"important":false,"random_id":0,"attachments":[{"type":"photo","photo":{"id":456239327,"album_id":-15,"owner_id":515828454,"sizes":[{"type":"m","url":"https:\/\/sun9-6.userapi.com\/c540100\/v540100328\/6fa71\/dHtVkQA6VWY.jpg","width":118,"height":130},{"type":"o","url":"https:\/\/sun9-5.userapi.com\/c540100\/v540100328\/6fa73\/UK6ebeydZ14.jpg","width":130,"height":144},{"type":"p","url":"https:\/\/sun9-1.userapi.com\/c540100\/v540100328\/6fa74\/Dw2Lmcufbrc.jpg","width":200,"height":221},{"type":"q","url":"https:\/\/sun9-6.userapi.com\/c540100\/v540100328\/6fa75\/MoDZzCcBkrQ.jpg","width":320,"height":353},{"type":"r","url":"https:\/\/sun9-3.userapi.com\/c540100\/v540100328\/6fa76\/fVo81F2GeeI.jpg","width":480,"height":530},{"type":"s","url":"https:\/\/sun9-4.userapi.com\/c540100\/v540100328\/6fa70\/2eBzwe4T8kc.jpg","width":68,"height":75},{"type":"x","url":"https:\/\/sun9-4.userapi.com\/c540100\/v540100328\/6fa72\/1_V4Eu_MF7w.jpg","width":480,"height":530}],"text":"","date":1542804864,"access_key":"74b9f9163c1a2f93ac"}}],"is_hidden":false},"group_id":173657849}',true);
$image = $json['object']['attachments'][0]['photo']['sizes'];
usort($image, function($a,$b){return ($b['width']-$a['width']);});
$bigImage=$image[0]['url'];
print_r($bigImage); die();
Оборудование для ресторана: новинки профессиональной кухонной техники
Частный дом престарелых в Киеве: комфорт, забота и профессиональный уход
Как можно сделать чтобы на лендинге надпись менялась в зависимости от запроса в поисковой системеНужно создавать отдельную страницу под...
Всем привет, еще не сильна в PHP, вот я сделала вот такой простенький скрипт для рассылки по своей группе ВК: