如何获取未序列化的自定义字段(在Rest API中获取高级自定义字段)?

时间:2019-07-15 11:40:19

标签: php wordpress advanced-custom-fields

我有以下json响应:

AppMaterialModule

我正在尝试获取[ { "id": 128537, "date": "2019-06-28T09:41:50", "date_gmt": "2019-06-28T09:41:50", "guid": { "rendered": "http://example.com/ing2/?p=128537" }, "modified": "2019-07-11T14:12:47", "modified_gmt": "2019-07-11T14:12:47", "slug": "villa-ortobene", "status": "publish", "type": "post", "link": "http://example.com/ing2/villa-ortobene/", "title": { "rendered": "Villa Ortobene" }, "content": { "rendered": "", "protected": false }, "excerpt": { "rendered": "", "protected": false }, "author": 1, "featured_media": 0, "comment_status": "closed", "ping_status": "open", "sticky": false, "template": "", "format": "standard", "meta": [], "categories": [ 626, 520 ], "tags": [], "details": { "_edit_last": [ "1" ], "_edit_lock": [ "1562854460:1" ], "descrizione_lavoro": [ "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi fermentum magna eu neque sodales semper. Nulla eget purus turpis. Nulla ut pretium nulla. Mauris nec massa ac risus convallis aliquam vitae ac dolor. Cras eget leo quis massa auctor dignissim ut mollis tellus. Curabitur sollicitudin diam nisl, sed molestie mi finibus in.\r\n\r\nInterdum et malesuada fames ac ante ipsum primis in faucibus. Integer feugiat, mi eget varius aliquam, mauris diam interdum mauris, ac lobortis risus velit eu mauris.\r\n\r\nNulla vehicula felis bibendum facilisis dignissim. Morbi consequat a nisl ac pulvinar. Donec tempor tellus et urna rutrum, et pharetra est accumsan. Morbi porttitor et lectus vel convallis. Vivamus quam leo, maximus sed placerat eu, facilisis a lectus. Sed mollis sapien mollis egestas ullamcorper. Proin eu velit quis ex volutpat convallis. Vivamus dolor dui, scelerisque bibendum ligula in, fermentum ultricies lorem.\r\n\r\nVestibulum porttitor nulla in mattis porta. Integer lobortis rhoncus dolor placerat aliquam. Praesent interdum dui vitae tortor varius tincidunt. Ut efficitur egestas libero, sed gravida dolor gravida quis. Aenean lacinia, neque eu feugiat dictum, felis libero dignissim elit, eget laoreet purus odio in velit. Suspendisse massa nibh, pharetra aliquam risus eget, ultricies semper purus." ], "_descrizione_lavoro": [ "field_5d1370af6f74d" ], "location": [ "a:3:{s:7:\"address\";s:95:\"Avinguda de la Granvia de l’Hospitalet, 8, 08902 L'Hospitalet de Llobregat, Barcelona, Spagna\";s:3:\"lat\";s:17:\"41.36256133817761\";s:3:\"lng\";s:17:\"2.131976960327165\";}" ], "_location": [ "field_5d12254915703" ], "quando": [ "2016" ], "_quando": [ "field_5d18cee430b4e" ], "categories": [ "a:1:{i:0;s:3:\"627\";}" ], "_categories": [ "field_5d1226f4d6974" ], "partner": [ "a:1:{i:0;s:13:\"Sandra Yannis\";}" ], "_partner": [ "field_5d124f89a4906" ], "collaboratori": [ "a:2:{i:0;s:10:\"Fran White\";i:1;s:10:\"Sam Jumper\";}" ], "_collaboratori": [ "field_5d13736937e7f" ], "importo": [ "1200000" ], "_importo": [ "field_5d1227e726e32" ], "galleria": [ "a:4:{i:0;s:6:\"128345\";i:1;s:6:\"128346\";i:2;s:6:\"128344\";i:3;s:6:\"128343\";}" ], "_galleria": [ "field_5d135d5440503" ], "home_immagine": [ "128542" ], "_home_immagine": [ "field_5c7c00fdec7f7" ], "progetti_preview": [ "128540" ], "_progetti_preview": [ "field_5c7c04c5b54a4" ], "codice_lavoro": [ "0F122" ], "_codice_lavoro": [ "field_5d122a75f0cc2" ], "id_opera": [ "EG09" ], "_id_opera": [ "field_5d137a87bddeb" ], "tipologia": [ "" ], "_tipologia": [ "field_5d137a998884f" ], "assegna_le_categorie_0_nome_categoria": [ "Ingegneria" ], "_assegna_le_categorie_0_nome_categoria": [ "field_5d14a7895ac17" ], "assegna_le_categorie_0_costo": [ "275000" ], "_assegna_le_categorie_0_costo": [ "field_5d14a79d5ac18" ], "assegna_le_categorie": [ "2" ], "_assegna_le_categorie": [ "field_5d14a76d5ac16" ], "tipologia_lavoro": [ "A01" ], "_tipologia_lavoro": [ "field_5d137a998884f" ], "il_committente": [ "a:1:{i:0;s:17:\"Comune di sassari\";}" ], "_il_committente": [ "field_5d1628244adfa" ], "status": [ "In corso" ], "_status": [ "field_5d162ba7fb989" ], "servizi": [ "a:1:{i:0;s:13:\"Progettazione\";}" ], "_servizi": [ "field_5d162cbaa4a2d" ], "assegna_le_categorie_1_nome_categoria": [ "Edilizia" ], "_assegna_le_categorie_1_nome_categoria": [ "field_5d14a7895ac17" ], "assegna_le_categorie_1_costo": [ "328000" ], "_assegna_le_categorie_1_costo": [ "field_5d14a79d5ac18" ], "anno": [ "2015" ], "_anno": [ "field_5d18cee430b4e" ], "mese": [ "1" ], "_mese": [ "field_5d18fee45fd32" ], "_wp_old_slug": [ "villa-ortobene__trashed" ], "latitude": [ "41.36256133817761" ], "longitude": [ "2.131976960327165" ] }, "acf": { "descrizione_lavoro": "<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi fermentum magna eu neque sodales semper. Nulla eget purus turpis. Nulla ut pretium nulla. Mauris nec massa ac risus convallis aliquam vitae ac dolor. Cras eget leo quis massa auctor dignissim ut mollis tellus. Curabitur sollicitudin diam nisl, sed molestie mi finibus in.</p>\n<p>Interdum et malesuada fames ac ante ipsum primis in faucibus. Integer feugiat, mi eget varius aliquam, mauris diam interdum mauris, ac lobortis risus velit eu mauris.</p>\n<p>Nulla vehicula felis bibendum facilisis dignissim. Morbi consequat a nisl ac pulvinar. Donec tempor tellus et urna rutrum, et pharetra est accumsan. Morbi porttitor et lectus vel convallis. Vivamus quam leo, maximus sed placerat eu, facilisis a lectus. Sed mollis sapien mollis egestas ullamcorper. Proin eu velit quis ex volutpat convallis. Vivamus dolor dui, scelerisque bibendum ligula in, fermentum ultricies lorem.</p>\n<p>Vestibulum porttitor nulla in mattis porta. Integer lobortis rhoncus dolor placerat aliquam. Praesent interdum dui vitae tortor varius tincidunt. Ut efficitur egestas libero, sed gravida dolor gravida quis. Aenean lacinia, neque eu feugiat dictum, felis libero dignissim elit, eget laoreet purus odio in velit. Suspendisse massa nibh, pharetra aliquam risus eget, ultricies semper purus.</p>\n", "location": { "address": "Avinguda de la Granvia de l’Hospitalet, 8, 08902 L'Hospitalet de Llobregat, Barcelona, Spagna", "lat": "41.36256133817761", "lng": "2.131976960327165" }, "categories": [ 627 ], "partner": [ "Sandra Yannis" ], "collaboratori": [ "Fran White", "Sam Jumper" ], "importo": "1200000", "galleria": [ { "ID": 128345, "id": 128345, "title": "villa1", "filename": "villa1.jpg", "filesize": 78350, "url": "http://example.com/ing2/wp-content/uploads/2019/03/villa1.jpg", "link": "http://example.com/ing2/villa1/", "alt": "", "author": "1", "description": "", "caption": "", "name": "villa1", "status": "inherit", "uploaded_to": 0, "date": "2019-03-05 11:43:03", "modified": "2019-03-05 11:43:03", "menu_order": 0, "mime_type": "image/jpeg", "type": "image", "subtype": "jpeg", "icon": "http://example.com/ing2/wp-includes/images/media/default.png", "width": 1000, "height": 667, "sizes": { "thumbnail": "http://example.com/ing2/wp-content/uploads/2019/03/villa1-150x150.jpg", "thumbnail-width": 150, "thumbnail-height": 150, "medium": "http://example.com/ing2/wp-content/uploads/2019/03/villa1-280x187.jpg", "medium-width": 280, "medium-height": 187, "medium_large": "http://example.com/ing2/wp-content/uploads/2019/03/villa1-768x512.jpg", "medium_large-width": 768, "medium_large-height": 512, "large": "http://example.com/ing2/wp-content/uploads/2019/03/villa1.jpg", "large-width": "1000", "large-height": "667" } }, { "ID": 128346, "id": 128346, "title": "villa2", "filename": "villa2.jpg", "filesize": 55980, "url": "http://example.com/ing2/wp-content/uploads/2019/03/villa2.jpg", "link": "http://example.com/ing2/villa2/", "alt": "", "author": "1", "description": "", "caption": "", "name": "villa2", "status": "inherit", "uploaded_to": 0, "date": "2019-03-05 11:43:04", "modified": "2019-04-03 12:07:52", "menu_order": 0, "mime_type": "image/jpeg", "type": "image", "subtype": "jpeg", "icon": "http://example.com/ing2/wp-includes/images/media/default.png", "width": 1000, "height": 667, "sizes": { "thumbnail": "http://example.com/ing2/wp-content/uploads/2019/03/villa2-150x150.jpg", "thumbnail-width": 150, "thumbnail-height": 150, "medium": "http://example.com/ing2/wp-content/uploads/2019/03/villa2-280x187.jpg", "medium-width": 280, "medium-height": 187, "medium_large": "http://example.com/ing2/wp-content/uploads/2019/03/villa2-768x512.jpg", "medium_large-width": 768, "medium_large-height": 512, "large": "http://example.com/ing2/wp-content/uploads/2019/03/villa2.jpg", "large-width": "1000", "large-height": "667" } }, { "ID": 128344, "id": 128344, "title": "villa3", "filename": "villa3.jpg", "filesize": 67396, "url": "http://example.com/ing2/wp-content/uploads/2019/03/villa3.jpg", "link": "http://example.com/ing2/villa3/", "alt": "", "author": "1", "description": "", "caption": "", "name": "villa3", "status": "inherit", "uploaded_to": 0, "date": "2019-03-05 11:41:49", "modified": "2019-03-05 11:41:49", "menu_order": 0, "mime_type": "image/jpeg", "type": "image", "subtype": "jpeg", "icon": "http://example.com/ing2/wp-includes/images/media/default.png", "width": 744, "height": 445, "sizes": { "thumbnail": "http://example.com/ing2/wp-content/uploads/2019/03/villa3-150x150.jpg", "thumbnail-width": 150, "thumbnail-height": 150, "medium": "http://example.com/ing2/wp-content/uploads/2019/03/villa3-313x187.jpg", "medium-width": 313, "medium-height": 187, "medium_large": "http://example.com/ing2/wp-content/uploads/2019/03/villa3.jpg", "medium_large-width": 744, "medium_large-height": 445, "large": "http://example.com/ing2/wp-content/uploads/2019/03/villa3.jpg", "large-width": "744", "large-height": "445" } }, { "ID": 128343, "id": 128343, "title": "villa4", "filename": "villa4.jpg", "filesize": 93285, "url": "http://example.com/ing2/wp-content/uploads/2019/03/villa4.jpg", "link": "http://example.com/ing2/villa4/", "alt": "", "author": "1", "description": "", "caption": "", "name": "villa4", "status": "inherit", "uploaded_to": 0, "date": "2019-03-05 11:41:48", "modified": "2019-03-05 11:41:48", "menu_order": 0, "mime_type": "image/jpeg", "type": "image", "subtype": "jpeg", "icon": "http://example.com/ing2/wp-includes/images/media/default.png", "width": 1000, "height": 743, "sizes": { "thumbnail": "http://example.com/ing2/wp-content/uploads/2019/03/villa4-150x150.jpg", "thumbnail-width": 150, "thumbnail-height": 150, "medium": "http://example.com/ing2/wp-content/uploads/2019/03/villa4-252x187.jpg", "medium-width": 252, "medium-height": 187, "medium_large": "http://example.com/ing2/wp-content/uploads/2019/03/villa4-768x571.jpg", "medium_large-width": 768, "medium_large-height": 571, "large": "http://example.com/ing2/wp-content/uploads/2019/03/villa4.jpg", "large-width": "1000", "large-height": "743" } } ], "home_immagine": { "ID": 128542, "id": 128542, "title": "villa2-aspect-ratio-x", "filename": "villa2-aspect-ratio-x-3.jpg", "filesize": 48677, "url": "http://example.com/ing2/wp-content/uploads/2019/06/villa2-aspect-ratio-x-3.jpg", "link": "http://example.com/ing2/villa-ortobene/villa2-aspect-ratio-x-4/", "alt": "", "author": "1", "description": "", "caption": "", "name": "villa2-aspect-ratio-x-4", "status": "inherit", "uploaded_to": 128537, "date": "2019-06-28 09:45:34", "modified": "2019-06-28 09:45:40", "menu_order": 0, "mime_type": "image/jpeg", "type": "image", "subtype": "jpeg", "icon": "http://example.com/ing2/wp-includes/images/media/default.png", "width": 1000, "height": 429, "sizes": { "thumbnail": "http://example.com/ing2/wp-content/uploads/2019/06/villa2-aspect-ratio-x-3-150x150.jpg", "thumbnail-width": 150, "thumbnail-height": 150, "medium": "http://example.com/ing2/wp-content/uploads/2019/06/villa2-aspect-ratio-x-3-436x187.jpg", "medium-width": 436, "medium-height": 187, "medium_large": "http://example.com/ing2/wp-content/uploads/2019/06/villa2-aspect-ratio-x-3-768x329.jpg", "medium_large-width": 768, "medium_large-height": 329, "large": "http://example.com/ing2/wp-content/uploads/2019/06/villa2-aspect-ratio-x-3.jpg", "large-width": "1000", "large-height": "429" }, "original_image": { "ID": 128346, "id": 128346, "title": "villa2", "filename": "villa2.jpg", "filesize": 55980, "url": "http://example.com/ing2/wp-content/uploads/2019/03/villa2.jpg", "link": "http://example.com/ing2/villa2/", "alt": "", "author": "1", "description": "", "caption": "", "name": "villa2", "status": "inherit", "uploaded_to": 0, "date": "2019-03-05 11:43:04", "modified": "2019-04-03 12:07:52", "menu_order": 0, "mime_type": "image/jpeg", "type": "image", "subtype": "jpeg", "icon": "http://example.com/ing2/wp-includes/images/media/default.png", "width": 1000, "height": 667, "sizes": { "thumbnail": "http://example.com/ing2/wp-content/uploads/2019/03/villa2-150x150.jpg", "thumbnail-width": 150, "thumbnail-height": 150, "medium": "http://example.com/ing2/wp-content/uploads/2019/03/villa2-280x187.jpg", "medium-width": 280, "medium-height": 187, "medium_large": "http://example.com/ing2/wp-content/uploads/2019/03/villa2-768x512.jpg", "medium_large-width": 768, "medium_large-height": 512, "large": "http://example.com/ing2/wp-content/uploads/2019/03/villa2.jpg", "large-width": "1000", "large-height": "667" } } }, "progetti_preview": { "ID": 128540, "id": 128540, "title": "2019-03-03_5c7c0b184ec71_villa2-aspect-ratio-x", "filename": "2019-03-03_5c7c0b184ec71_villa2-aspect-ratio-x.jpg", "filesize": 22343, "url": "http://example.com/ing2/wp-content/uploads/2019/06/2019-03-03_5c7c0b184ec71_villa2-aspect-ratio-x.jpg", "link": "http://example.com/ing2/villa-ortobene/2019-03-03_5c7c0b184ec71_villa2-aspect-ratio-x-5/", "alt": "", "author": "1", "description": "", "caption": "", "name": "2019-03-03_5c7c0b184ec71_villa2-aspect-ratio-x-5", "status": "inherit", "uploaded_to": 128537, "date": "2019-06-28 09:42:38", "modified": "2019-06-28 09:42:47", "menu_order": 0, "mime_type": "image/jpeg", "type": "image", "subtype": "jpeg", "icon": "http://example.com/ing2/wp-includes/images/media/default.png", "width": 515, "height": 482, "sizes": { "thumbnail": "http://example.com/ing2/wp-content/uploads/2019/06/2019-03-03_5c7c0b184ec71_villa2-aspect-ratio-x-150x150.jpg", "thumbnail-width": 150, "thumbnail-height": 150, "medium": "http://example.com/ing2/wp-content/uploads/2019/06/2019-03-03_5c7c0b184ec71_villa2-aspect-ratio-x-200x187.jpg", "medium-width": 200, "medium-height": 187, "medium_large": "http://example.com/ing2/wp-content/uploads/2019/06/2019-03-03_5c7c0b184ec71_villa2-aspect-ratio-x.jpg", "medium_large-width": 515, "medium_large-height": 482, "large": "http://example.com/ing2/wp-content/uploads/2019/06/2019-03-03_5c7c0b184ec71_villa2-aspect-ratio-x.jpg", "large-width": "515", "large-height": "482" }, "original_image": { "ID": 128299, "id": 128299, "title": "2019-03-03_5c7c0b184ec71_villa2", "filename": "2019-03-03_5c7c0b184ec71_villa2.jpg", "filesize": 55980, "url": "http://example.com/ing2/wp-content/uploads/2019/03/2019-03-03_5c7c0b184ec71_villa2.jpg", "link": "http://example.com/ing2/2019-03-03_5c7c0b184ec71_villa2/", "alt": "", "author": "1", "description": "", "caption": "", "name": "2019-03-03_5c7c0b184ec71_villa2", "status": "inherit", "uploaded_to": 0, "date": "2019-03-03 17:12:57", "modified": "2019-03-03 17:12:57", "menu_order": 0, "mime_type": "image/jpeg", "type": "image", "subtype": "jpeg", "icon": "http://example.com/ing2/wp-includes/images/media/default.png", "width": 1000, "height": 667, "sizes": { "thumbnail": "http://example.com/ing2/wp-content/uploads/2019/03/2019-03-03_5c7c0b184ec71_villa2-150x150.jpg", "thumbnail-width": 150, "thumbnail-height": 150, "medium": "http://example.com/ing2/wp-content/uploads/2019/03/2019-03-03_5c7c0b184ec71_villa2-280x187.jpg", "medium-width": 280, "medium-height": 187, "medium_large": "http://example.com/ing2/wp-content/uploads/2019/03/2019-03-03_5c7c0b184ec71_villa2-768x512.jpg", "medium_large-width": 768, "medium_large-height": 512, "large": "http://example.com/ing2/wp-content/uploads/2019/03/2019-03-03_5c7c0b184ec71_villa2.jpg", "large-width": "1000", "large-height": "667" } } }, "codice_lavoro": "0F122", "id_opera": "EG09", "assegna_le_categorie": [ { "nome_categoria": "Ingegneria", "costo": "275000" }, { "nome_categoria": "Edilizia", "costo": "328000" } ], "tipologia_lavoro": "A01", "il_committente": [ "Comune di sassari" ], "status": "In corso", "servizi": [ "Progettazione" ], "anno": "2015", "mese": "1" }, "_links": { "self": [ { "href": "http://example.com/ing2/wp-json/wp/v2/posts/128537" } ], "collection": [ { "href": "http://example.com/ing2/wp-json/wp/v2/posts" } ], "about": [ { "href": "http://example.com/ing2/wp-json/wp/v2/types/post" } ], "author": [ { "embeddable": true, "href": "http://example.com/ing2/wp-json/wp/v2/users/1" } ], "replies": [ { "embeddable": true, "href": "http://example.com/ing2/wp-json/wp/v2/comments?post=128537" } ], "version-history": [ { "count": 34, "href": "http://example.com/ing2/wp-json/wp/v2/posts/128537/revisions" } ], "predecessor-version": [ { "id": 128630, "href": "http://example.com/ing2/wp-json/wp/v2/posts/128537/revisions/128630" } ], "wp:attachment": [ { "href": "http://example.com/ing2/wp-json/wp/v2/media?parent=128537" } ], "wp:term": [ { "taxonomy": "category", "embeddable": true, "href": "http://example.com/ing2/wp-json/wp/v2/categories?post=128537" }, { "taxonomy": "post_tag", "embeddable": true, "href": "http://example.com/ing2/wp-json/wp/v2/tags?post=128537" } ], "curies": [ { "name": "wp", "href": "https://api.w.org/{rel}", "templated": true } ] } } ] 的值

我尝试过

anno

但是我得到var_dump($response["details"]["anno"]);

看起来数据已序列化,所以我尝试了

NULL

然后

$data = unserialize($response["details"]["anno"][0]);

但是我什么也没得到。基本上,我需要获取所有那些未序列化字段的值,但是我已经堆积了。

就像下面的序列化一样,我需要获取地址或坐标

print_r($data);

基本上,我需要能够从"location": [ "a:3:{s:7:\"address\";s:95:\"Avinguda de la Granvia de l’Hospitalet, 8, 08902 L'Hospitalet de Llobregat, Barcelona, Spagna\";s:3:\"lat\";s:17:\"41.36256133817761\";s:3:\"lng\";s:17:\"2.131976960327165\";}" ], 抓取任何简单数据

3 个答案:

答案 0 :(得分:1)

根据您的回答,似乎$response变量正在保留wp_remote_get函数调用的响应。它返回了http请求数据-标头,正文等。 Json本身是原始body的{​​{1}}属性。

您需要使用$response函数。 首先,解码json响应。然后,您可以访问其属性。

json_decode

输出:

$jsonResponse = $response['body'];
$data = json_decode($jsonResponse, true);
var_dump($data[0]['details']['anno'][0]);

答案 1 :(得分:0)

这是一个数组,必须指向索引0处的firt元素 试试这个:

var_dump($response[0]["details"]["anno"][0]);

答案 2 :(得分:0)

根据您的答案:

$response = wp_remote_get(get_home_url()."2/wp-json/wp/v2/posts?include=".$userPostsExternal);

$data = json_decode($response['body'], true);

var_dump($data[0]["details"]["anno"][0]);

说明:

wp_remote_get是内部wordpress方法,如此处https://codex.wordpress.org/Function_Reference/wp_remote_get所述 它返回一个包含两个元素的数组:HTTP响应headersbody。 json数据位于body元素中。由于主体为json格式,因此您需要使用json_encode函数进行解析。第二个true参数定义解析后的json字符串将作为数组返回。 json数据是一个包含单个元素的数组,这就是为什么您需要使用$data[0]来获取确切的响应数据的原因。