{"id":306,"date":"2014-11-16T10:24:12","date_gmt":"2014-11-16T10:24:12","guid":{"rendered":"https:\/\/www.autorouter.eu\/wiki\/?page_id=306"},"modified":"2016-09-24T13:04:39","modified_gmt":"2016-09-24T13:04:39","slug":"php-api-sample-with-curl","status":"publish","type":"page","link":"https:\/\/www.autorouter.aero\/wiki\/api\/php-api-sample-with-curl\/","title":{"rendered":"PHP API sample with cURL"},"content":{"rendered":"<p>This sample uses the PHP builtin cURL library to perform REST calls and authenticates using OAuth 2.0.<\/p>\n<pre>#!\/usr\/bin\/php\r\n&lt;?php\r\n\r\n$serverURL = \"https:\/\/api.autorouter.aero\/v1.0\";\r\n$token = auth();\r\nif ($token === null)\r\n   exit;\r\n\r\n$cl = curl_init();\r\ncurl_setopt($cl, CURLOPT_RETURNTRANSFER, true);\r\n\/* uncomment the next line in case you don't have the required SSL certificates *\/\r\n\/\/ curl_setopt($cl, CURLOPT_SSL_VERIFYPEER, false);\r\n\r\n\/* show the token and add it to our future requests*\/\r\nvar_dump($token);\r\ncurl_setopt($cl, CURLOPT_HTTPHEADER, array(\"Authorization: Bearer \" . $token['access_token']));\r\n\r\n\/*\r\n * Get the API version\r\n *\/\r\ncurl_setopt($cl, CURLOPT_URL, \"$serverURL\/system\/version\");\r\n$response = curl_exec($cl);\r\nprint_r($response);\r\necho PHP_EOL;\r\n\r\n\/*\r\n * Get list of aircraft templates\r\n *\/\r\ncurl_setopt($cl, CURLOPT_URL, \"$serverURL\/aircraft\/templates\");\r\n$response = curl_exec($cl);\r\nprint_r($response);\r\ncurl_close($cl);\r\n\r\n\/**\r\n * Get an authentication token\r\n *\/\r\nfunction auth()\r\n{\r\n   global $serverURL;\r\n   $cl = curl_init();\r\n   curl_setopt($cl, CURLOPT_RETURNTRANSFER, true);\r\n   curl_setopt($cl, CURLOPT_URL, \"$serverURL\/oauth2\/token\");\r\n   curl_setopt($cl, CURLOPT_POST, true);\r\n   \/* uncomment this line if you don't have the required SSL certificates *\/\r\n   \/\/ curl_setopt($cl, CURLOPT_SSL_VERIFYPEER, false);\r\n   curl_setopt($cl, CURLOPT_POSTFIELDS, array(\r\n     \"grant_type\" =&gt; \"client_credentials\",\r\n     \"client_id\" =&gt; \"my@email.address\",\r\n     \"client_secret\" =&gt; \"my password\"\r\n   ));\r\n   $auth_response = curl_exec($cl);\r\n   if ($auth_response === false)\r\n   {\r\n      echo \"Failed to authenticate\\n\";\r\n      var_dump(curl_getinfo($cl));\r\n      curl_close($cl);\r\n      return NULL;\r\n   }\r\n   curl_close($cl);\r\n   return json_decode($auth_response, true);\r\n}<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This sample uses the PHP builtin cURL library to perform REST calls and authenticates using OAuth 2.0. #!\/usr\/bin\/php &lt;?php $serverURL = &#8220;https:\/\/api.autorouter.aero\/v1.0&#8221;; $token = auth(); if ($token === null) exit; $cl = curl_init(); curl_setopt($cl, CURLOPT_RETURNTRANSFER, true); \/* uncomment the next line in case you don&#8217;t have the required SSL certificates *\/ \/\/ curl_setopt($cl, CURLOPT_SSL_VERIFYPEER, false); [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":33,"menu_order":0,"comment_status":"closed","ping_status":"open","template":"","meta":{"footnotes":""},"class_list":["post-306","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.autorouter.aero\/wiki\/wp-json\/wp\/v2\/pages\/306","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.autorouter.aero\/wiki\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.autorouter.aero\/wiki\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.autorouter.aero\/wiki\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.autorouter.aero\/wiki\/wp-json\/wp\/v2\/comments?post=306"}],"version-history":[{"count":8,"href":"https:\/\/www.autorouter.aero\/wiki\/wp-json\/wp\/v2\/pages\/306\/revisions"}],"predecessor-version":[{"id":723,"href":"https:\/\/www.autorouter.aero\/wiki\/wp-json\/wp\/v2\/pages\/306\/revisions\/723"}],"up":[{"embeddable":true,"href":"https:\/\/www.autorouter.aero\/wiki\/wp-json\/wp\/v2\/pages\/33"}],"wp:attachment":[{"href":"https:\/\/www.autorouter.aero\/wiki\/wp-json\/wp\/v2\/media?parent=306"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}