{"id":13499841,"url":"https://github.com/maliknaik16/webthing-php","last_synced_at":"2025-09-19T20:35:34.161Z","repository":{"id":57079286,"uuid":"227076716","full_name":"maliknaik16/webthing-php","owner":"maliknaik16","description":"PHP implementation of an HTTP Web Thing API, compatible with PHP 7.4+, for exposing and controlling devices like lights and sensors via the Web of Things (WoT) framework. Includes example setups for a dimmable light and a humidity sensor","archived":false,"fork":false,"pushed_at":"2022-09-02T16:26:18.000Z","size":86,"stargazers_count":24,"open_issues_count":1,"forks_count":3,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-08-11T16:34:17.085Z","etag":null,"topics":["framework","http","iot","mozilla","php","sensors","webthings"],"latest_commit_sha":null,"homepage":"https://webthings.io/framework/","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mpl-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/maliknaik16.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-12-10T09:20:51.000Z","updated_at":"2025-04-01T20:18:23.000Z","dependencies_parsed_at":"2022-08-24T14:56:25.574Z","dependency_job_id":null,"html_url":"https://github.com/maliknaik16/webthing-php","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/maliknaik16/webthing-php","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maliknaik16%2Fwebthing-php","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maliknaik16%2Fwebthing-php/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maliknaik16%2Fwebthing-php/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maliknaik16%2Fwebthing-php/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/maliknaik16","download_url":"https://codeload.github.com/maliknaik16/webthing-php/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maliknaik16%2Fwebthing-php/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":275998196,"owners_count":25567385,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-09-19T02:00:09.700Z","response_time":108,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["framework","http","iot","mozilla","php","sensors","webthings"],"created_at":"2024-07-31T22:00:43.843Z","updated_at":"2025-09-19T20:35:34.142Z","avatar_url":"https://github.com/maliknaik16.png","language":"PHP","funding_links":[],"categories":["Section"],"sub_categories":["Libraries"],"readme":"# Web of Things\n\n[![travis](https://api.travis-ci.org/maliknaik16/webthing-php.svg?branch=master)](https://travis-ci.com/maliknaik16/webthing-php)\n[![GitHub forks](https://img.shields.io/github/forks/maliknaik16/webthing-php)](https://github.com/maliknaik16/webthing-php/network/)\n[![GitHub version](https://badge.fury.io/gh/maliknaik16%2Fwebthing-php.svg)](https://badge.fury.io/gh/maliknaik16%2Fwebthing-php)\n[![Source Code](https://img.shields.io/badge/source-maliknaik16%2Fwebthing--php-blue?style=flat-square)](https://github.com/maliknaik16/webthing-php)\n[![PHP Version](https://img.shields.io/badge/PHP-7.4%2B-orange)](https://php.net)\n[![Software License](https://img.shields.io/badge/license-MPL--2.0-green?style=flat-square)](https://github.com/maliknaik16/webthing-php/blob/master/LICENSE.txt)\n\nImplementation of an HTTP [Web Thing](https://iot.mozilla.org/wot/). This library is compatible with PHP 7.4+.\n\n# Installation\n\nThe ``webthing`` can be installed using ``composer`` via the following command:\n\n```bash\ncomposer require webthing/webthing:^0.0.1\n```\n\n# Running the Example\nThe following list of commands clones this repository and installs all dependencies using the composer and runs the `single-thing.php` example.\n```bash\ngit clone https://github.com/maliknaik16/webthing-php.git\ncd webthing-php\ncomposer install\nphp examples/single-thing.php\n```\n\n# Example Implementation\n\nIn this code-walkthrough we will set up a dimmable light and a humidity sensor (both using fake data, of course). Both working examples can be found in [here](https://github.com/maliknaik16/webthing-php/tree/master/examples).\n\n## Dimmable Light\n\nImagine you have a dimmable light that you want to expose via the web of things API. The light can be turned on/off and the brightness can be set from 0% to 100%. Besides the name, description, and type, a [Light](https://iot.mozilla.org/schemas/#Light) is required to expose two properties:\n\n  - ``on``: the state of the light, whether it is turned on or off\n\n    - Setting this property via a ``PUT {\"on\": true/false}`` call to the REST API toggles the light.\n\n  - ``brightness``: the brightness level of the light from 0-100%\n\n    - Setting this property via a PUT call to the REST API sets the brightness level of this light.\n\nFirst we create a new Thing:\n\n```php\n$light = new Thing(\n  'urn:dev:ops:my-lamp-1234',\n  'My Lamp',\n  ['OnOffSwitch', 'Light'],\n  'A web connected lamp'\n);\n```\n\nNow we can add the required properties.\n\nThe ``on`` property reports and sets the on/off state of the light. For this, we need to have a ``Value`` object which holds the actual state and also a method to turn the light on/off. For our purposes, we just want to log the new state if the light is switched on/off.\n\n```php\n$light-\u003eaddProperty(new Property(\n  $light,\n  'on',\n  new Value(TRUE, function($v) {\n    echo \"On-State is now \" . $v . \"\\n\";\n  }),\n  [\n    '@type' =\u003e 'OnOffProperty',\n    'title' =\u003e 'On/Off',\n    'type' =\u003e 'boolean',\n    'description' =\u003e 'Whether the lamp is turned on',\n  ])\n);\n```\n\nThe ``brightness`` property reports the brightness level of the light and sets the level. Like before, instead of actually setting the level of a light, we just log the level.\n\n```php\n$light-\u003eaddProperty(new Property(\n  $light,\n  'brightness',\n  new Value(50, function($v) {\n    echo \"Brightness is now \" . $v . \"\\n\";\n  }),\n  [\n    '@type' =\u003e 'BrightnessProperty',\n    'title' =\u003e 'Brightness',\n    'type' =\u003e 'integer',\n    'description' =\u003e 'The level of light from 0-100',\n    'minimum' =\u003e 0,\n    'maximum' =\u003e 100,\n    'unit' =\u003e 'percent',\n  ])\n);\n```\n\nNow we can add our newly created thing to the server and start it:\n\n```php\n// If adding more than one thing, use MultipleThings() with a name.\n// In the single thing case, the thing's name will be broadcast.\n$server = new WebThingServer(new SingleThing($thing), '127.0.0.1', 8888, 8081);\n\n$server-\u003estart();\n$server-\u003estartWebSocket();\n```\nThis will start the server, making the light available via the WoT REST API and announcing it as a discoverable resource on your local network via mDNS.\n\n## Sensor\n\nLet's now also connect a humidity sensor to the server we set up for our light.\n\nA [MultiLevelSensor](https://iot.mozilla.org/schemas/#MultiLevelSensor) (a sensor that returns a level instead of just on/off) has one required property (besides the name, type, and optional description): ``level``. We want to monitor this property and get notified if the value changes.\n\nFirst we create a new Thing:\n\n```php\n$sensor = new Thing(\n 'urn:dev:ops:my-humidity-sensor-1234',\n  'My Humidity Sensor',\n  ['MultiLevelSensor'],\n  'A web connected humidity sensor'\n);\n```\n\nThen we create and add the appropriate property:\n\n  - ``level``: tells us what the sensor is actually reading\n\n    - Contrary to the light, the value cannot be set via an API call, as it wouldn't make much sense, to SET what a sensor is reading. Therefore, we are creating a **readOnly** property.\n\n      ```php\n      $level = new Value(0.0);\n      $sensor-\u003eaddProperty(new Property(\n        $sensor,\n        'level',\n        $level,\n        [\n          '@type' =\u003e 'LevelProperty',\n          'title' =\u003e 'Humidity',\n          'type' =\u003e 'number',\n          'description' =\u003e 'The current humidity in %',\n          'minimum' =\u003e 0,\n          'maximum' =\u003e 100,\n          'unit' =\u003e 'percent',\n          'readOnly' =\u003e TRUE,\n        ])\n      );\n      ```\n\nNow we have a sensor that constantly reports 0%. To make it usable, we need a thread or some kind of input when the sensor has a new reading available. For this purpose we start a thread that queries the physical sensor every few seconds. For our purposes, it just calls a fake method.\n\n```php\n// $level is a `Value` object.\n// $loop is a `React\\EventLoop\\Factory` object.\n$loop-\u003eaddPeriodicTimer(7, function() use ($level) {\n  $new_level = readFromGpio();\n  printf(\"Setting new humidity level: %s\\n\", $new_level);\n  $level-\u003enotifyOfExternalUpdate($new_level);\n});\n\n\nfunction readFromGpio() {\n  return abs(70.0 * rand() * (-0.5 + rand()));\n}\n```\nThis will update our ``Value`` object with the sensor readings via the ``$level-\u003enotifyOfExternalUpdate(readFromGpio());`` call. The ``Value`` object now notifies the property and the thing that the value has changed, which in turn notifies all websocket listeners.\n\n# Resources\n  - [https://iot.mozilla.org/wot](https://iot.mozilla.org/wot)\n  - [https://iot.mozilla.org/framework/](https://iot.mozilla.org/framework/)\n  - [https://iot.mozilla.org/gateway/](https://iot.mozilla.org/gateway/)\n  - [https://www.w3.org/WoT/IG/](https://www.w3.org/WoT/IG/)\n\n# License\n\nMozilla Public License Version 2.0\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmaliknaik16%2Fwebthing-php","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmaliknaik16%2Fwebthing-php","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmaliknaik16%2Fwebthing-php/lists"}