{"id":3363,"date":"2020-02-06T13:21:19","date_gmt":"2020-02-06T12:21:19","guid":{"rendered":"https:\/\/solidt.eu\/site\/?p=3363"},"modified":"2020-02-06T14:05:19","modified_gmt":"2020-02-06T13:05:19","slug":"wordpress-plugin-call-to-external-api","status":"publish","type":"post","link":"https:\/\/solidt.eu\/site\/wordpress-plugin-call-to-external-api\/","title":{"rendered":"WordPress plugin call to external API"},"content":{"rendered":"\n<p><a href=\"https:\/\/wordpress.stackexchange.com\/a\/266728\">https:\/\/wordpress.stackexchange.com\/a\/266728<\/a><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"php\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\n\/*\n    Template Name: My Api Page\n*\/\nfunction get_from_api()\n{\n    $key = 'my_custom_api_data2';\n\t$cache_seconds = 60;\n\t\n    $cached = \\wp_cache_get($key);\n    if (!empty($cached)) {\n        return $cached;\n    }\n    $response = \\wp_remote_get('https:\/\/jsonplaceholder.typicode.com\/users\/');\n\tif ( is_array( $response ) &amp;&amp; ! is_wp_error( $response ) ) {\n    \t$headers = $response['headers']; \/\/ array of http header lines\n    \t$body = $response['body']; \/\/ use the content\n\t\t$data = json_decode($body);\n\t\t\\wp_cache_set($key, $data, '', $cache_seconds);\n        return $data;\n\t}\n\treturn [];\n}\n\ntry\n{\n\t$users = get_from_api();\n    foreach ($users as $key => $user) {\n        echo \"{$user->name}&lt;br>\";\n    }\n} catch(\\Throwable $th) {\n\techo $th->message;\n}\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>https:\/\/wordpress.stackexchange.com\/a\/266728<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-3363","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/3363","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/comments?post=3363"}],"version-history":[{"count":3,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/3363\/revisions"}],"predecessor-version":[{"id":3370,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/3363\/revisions\/3370"}],"wp:attachment":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/media?parent=3363"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/categories?post=3363"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/tags?post=3363"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}