From c17e446cf2ee92e64bfa3586883644d23390eff1 Mon Sep 17 00:00:00 2001 From: Grummfy Date: Sun, 18 Oct 2015 23:43:32 +0200 Subject: [PATCH] php example --- PHP/README.md | 5 ++++ PHP/composer.json | 5 ++++ PHP/config.php | 23 ++++++++++++++++ PHP/example-with-curl.php | 31 ++++++++++++++++++++++ PHP/example-with-guzzle.php | 53 +++++++++++++++++++++++++++++++++++++ 5 files changed, 117 insertions(+) create mode 100644 PHP/README.md create mode 100644 PHP/composer.json create mode 100644 PHP/config.php create mode 100644 PHP/example-with-curl.php create mode 100644 PHP/example-with-guzzle.php diff --git a/PHP/README.md b/PHP/README.md new file mode 100644 index 0000000..dcafbfb --- /dev/null +++ b/PHP/README.md @@ -0,0 +1,5 @@ +# Example for PHP of Runabove IOT + +See comment in the code. + +Configuration in config.php. \ No newline at end of file diff --git a/PHP/composer.json b/PHP/composer.json new file mode 100644 index 0000000..866131e --- /dev/null +++ b/PHP/composer.json @@ -0,0 +1,5 @@ +{ + "require": { + "guzzlehttp/guzzle": "^6.1" + } +} diff --git a/PHP/config.php b/PHP/config.php new file mode 100644 index 0000000..1805624 --- /dev/null +++ b/PHP/config.php @@ -0,0 +1,23 @@ + 'temp.home', + 'timestamp' => time(), + 'value' => 1234, + 'tags' => [ + // uncoment to get an error + // 'tag3' => 123, + 'tag1' => 'abc1', + 'tag2' => 'def2', + ], + ] +]; diff --git a/PHP/example-with-curl.php b/PHP/example-with-curl.php new file mode 100644 index 0000000..fc698c7 --- /dev/null +++ b/PHP/example-with-curl.php @@ -0,0 +1,31 @@ + 'composer install' + +include __DIR__ . '/vendor/autoload.php'; + +use GuzzleHttp\Client; + +include __DIR__ . '/config.php'; + +// create the client +$client = new Client([ + 'base_uri' => $url, + 'timeout' => 2.0, +]); + +try +{ + // do the query + $response = $client->request('POST', $path, [ + 'json' => $datas, + 'headers' => [ + 'Accept' => 'application/json', + 'Content-Type' => 'application/json', + ], + 'auth' => [$key, $token], + ]); + + // print the response + var_dump([ + $response->getStatusCode(), + $response->getReasonPhrase() + ]); +} +catch(\Exception $e) +{ + if (method_exists($e, 'getResponse')) + { + // get info about the error + $response = $e->getResponse(); + var_dump([ + $response->getStatusCode(), + $response->getReasonPhrase(), + $response->getBody()->getContents(), + ]); + } + else + { + var_dump($e); + } +}