Skip to content
This repository was archived by the owner on Jun 12, 2019. It is now read-only.

php example #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions PHP/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Example for PHP of Runabove IOT

See comment in the code.

Configuration in config.php.
5 changes: 5 additions & 0 deletions PHP/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"require": {
"guzzlehttp/guzzle": "^6.1"
}
}
23 changes: 23 additions & 0 deletions PHP/config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

// config
$token = ''; // set the token here
$secret = ''; // set the secret here

$host = 'opentsdb.iot.runabove.io';
$url = 'https://opentsdb.iot.runabove.io';
$path = '/api/put';

$datas = [
[
'metric' => 'temp.home',
'timestamp' => time(),
'value' => 1234,
'tags' => [
// uncoment to get an error
// 'tag3' => 123,
'tag1' => 'abc1',
'tag2' => 'def2',
],
]
];
31 changes: 31 additions & 0 deletions PHP/example-with-curl.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

// example with curl

include __DIR__ . '/config.php';
$jsonDatas = json_encode($datas);

// init curl
$process = curl_init($host);

// config the curl command
curl_setopt($process, CURLOPT_URL, $url . $path);
// get header in the return content
curl_setopt($process, CURLOPT_HEADER, 1);
curl_setopt($process, CURLOPT_USERPWD, $key . ':' . $token);
curl_setopt($process, CURLOPT_POST, 1);
// post data
curl_setopt($process, CURLOPT_POSTFIELDS, $jsonDatas);
// get the content
curl_setopt($process, CURLOPT_RETURNTRANSFER, true);

// do the query
$return = curl_exec($process);
$infos = curl_getinfo($process);
curl_close($process);

// print result
var_dump([
$infos['http_code'],
$return,
]);
53 changes: 53 additions & 0 deletions PHP/example-with-guzzle.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

// example with guzzle

// first init composer => '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);
}
}