{"id":332,"date":"2023-02-07T12:12:08","date_gmt":"2023-02-07T12:12:08","guid":{"rendered":"https:\/\/devdocs.resales-online.com\/index.php\/docs\/new-development-updates-api\/example\/"},"modified":"2023-02-17T12:24:39","modified_gmt":"2023-02-17T12:24:39","slug":"example","status":"publish","type":"docs","link":"https:\/\/devdocs.resales-online.com\/index.php\/docs\/new-development-updates-api\/example\/","title":{"rendered":"PHP Script Example"},"content":{"rendered":"\n<p>This is an example PHP script to call the <code>\/iam\/me<\/code> API endpoint. Here, we store the secret key in a file and cache the access token (in files, too) so that we don&#8217;t need to request new one every time the script is run.<\/p>\n\n\n\n<div class=\"hcb_wrap\"><pre class=\"prism line-numbers lang-php\" data-lang=\"PHP\"><code>&lt;?php\nconst API_ROOT = &quot;http:\/\/api.propq.com&quot;;\nconst CLIENT_ID = 5;\n\nconst FILE_ACCESS_TOKEN = &quot;access_token&quot;;\nconst FILE_SECRET = &quot;secret.txt&quot;;\n\n\/\/ Get the client secret from an external configuration file\n\/\/ Here we use a text file, you can use environment variables or any\n\/\/ configuration storage mechanism.\n\/\/ The most important thing to remember is do not store the secret\n\/\/ key in the code files\n\n$clientSecret = trim(file_get_contents(FILE_SECRET));\n\n\/\/ An access token is valid for 30 days. We can save it in files or database\n\/\/ so that we don&#39;t need to acquire new tokens every requests\n\/\/\n\/\/ In this example, we first try to get an access token from the cached file\n\/\/ before request a new one.\n$accessToken = &quot;&quot;;\nif (file_exists(FILE_ACCESS_TOKEN)) {\n  $accessToken = trim(file_get_contents(FILE_ACCESS_TOKEN));\n} else {\n  \/\/ If there is no access token cached, we request a new one from the\n  \/\/ token end point\n  $tokenEndPoint = API_ROOT . &quot;\/oauth\/token&quot;;\n  $params = [\n  &quot;grant_type&quot; =&gt; &quot;client_credentials&quot;,\n  &quot;client_id&quot; =&gt; CLIENT_ID,\n  &quot;client_secret&quot; =&gt; $clientSecret\n  ];\n\n  $ch = curl_init();\n  curl_setopt($ch, CURLOPT_URL, $tokenEndPoint);\n  curl_setopt($ch, CURLOPT_POST, 1);\n  curl_setopt($ch, CURLOPT_POSTFIELDS, $params);\n  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);\n  $output = curl_exec($ch);\n  curl_close ($ch);\n  $json = json_decode($output, true);\n  $accessToken = $json[&#39;access_token&#39;];\n\n  file_put_contents(FILE_ACCESS_TOKEN, $accessToken);\n}\n\n\/\/ Now, with access token we can call other APIs.\n\/\/ The example below calling the `\/iam\/me` end point to get the information of\n\/\/ the current user.\n$url = API_ROOT . &quot;\/api\/v1\/iam\/me&quot;;\n$ch = curl_init();\ncurl_setopt($ch, CURLOPT_URL, $url);\ncurl_setopt($ch, CURLOPT_RETURNTRANSFER, true);\ncurl_setopt($ch, CURLOPT_HTTPHEADER, [\n&#39;Content-Type: application\/json&#39;,\n&#39;Authorization: Bearer &#39; . $accessToken\n]);\n$output = curl_exec($ch);\ncurl_close ($ch);\n$json = json_decode($output, true);\n\n\/\/ Display the json output\nvar_dump($json);\n\n?&gt;<\/code><\/pre><\/div>\n","protected":false},"featured_media":0,"parent":326,"menu_order":4,"comment_status":"closed","ping_status":"closed","template":"","doc_tag":[],"class_list":["post-332","docs","type-docs","status-publish","hentry"],"comment_count":0,"_links":{"self":[{"href":"https:\/\/devdocs.resales-online.com\/index.php\/wp-json\/wp\/v2\/docs\/332","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devdocs.resales-online.com\/index.php\/wp-json\/wp\/v2\/docs"}],"about":[{"href":"https:\/\/devdocs.resales-online.com\/index.php\/wp-json\/wp\/v2\/types\/docs"}],"replies":[{"embeddable":true,"href":"https:\/\/devdocs.resales-online.com\/index.php\/wp-json\/wp\/v2\/comments?post=332"}],"version-history":[{"count":4,"href":"https:\/\/devdocs.resales-online.com\/index.php\/wp-json\/wp\/v2\/docs\/332\/revisions"}],"predecessor-version":[{"id":361,"href":"https:\/\/devdocs.resales-online.com\/index.php\/wp-json\/wp\/v2\/docs\/332\/revisions\/361"}],"up":[{"embeddable":true,"href":"https:\/\/devdocs.resales-online.com\/index.php\/wp-json\/wp\/v2\/docs\/326"}],"next":[{"title":"Response code list","link":"https:\/\/devdocs.resales-online.com\/index.php\/docs\/new-development-updates-api\/response-code\/","href":"https:\/\/devdocs.resales-online.com\/index.php\/wp-json\/wp\/v2\/docs\/331"}],"prev":[{"title":"Get development status","link":"https:\/\/devdocs.resales-online.com\/index.php\/docs\/new-development-updates-api\/get-development-status\/","href":"https:\/\/devdocs.resales-online.com\/index.php\/wp-json\/wp\/v2\/docs\/343"}],"wp:attachment":[{"href":"https:\/\/devdocs.resales-online.com\/index.php\/wp-json\/wp\/v2\/media?parent=332"}],"wp:term":[{"taxonomy":"doc_tag","embeddable":true,"href":"https:\/\/devdocs.resales-online.com\/index.php\/wp-json\/wp\/v2\/doc_tag?post=332"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}