{"id":33978730,"url":"https://github.com/pnut-api/phpnut","last_synced_at":"2025-12-13T02:57:19.251Z","repository":{"id":57043126,"uuid":"80633303","full_name":"pnut-api/phpnut","owner":"pnut-api","description":"Pnut.io PHP library","archived":false,"fork":false,"pushed_at":"2021-03-18T13:05:39.000Z","size":444,"stargazers_count":5,"open_issues_count":1,"forks_count":2,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-08-04T13:47:25.208Z","etag":null,"topics":["library","pnut","social-network"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/pnut-api.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-02-01T15:27:12.000Z","updated_at":"2021-03-18T13:05:20.000Z","dependencies_parsed_at":"2022-08-24T01:21:05.050Z","dependency_job_id":null,"html_url":"https://github.com/pnut-api/phpnut","commit_stats":null,"previous_names":[],"tags_count":16,"template":false,"template_full_name":null,"purl":"pkg:github/pnut-api/phpnut","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pnut-api%2Fphpnut","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pnut-api%2Fphpnut/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pnut-api%2Fphpnut/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pnut-api%2Fphpnut/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/pnut-api","download_url":"https://codeload.github.com/pnut-api/phpnut/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/pnut-api%2Fphpnut/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":27699135,"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-12-13T02:00:09.769Z","response_time":147,"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":["library","pnut","social-network"],"created_at":"2025-12-13T02:57:14.495Z","updated_at":"2025-12-13T02:57:19.245Z","avatar_url":"https://github.com/pnut-api.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# phpnut\n\nPHP library for the pnut.io API.\n\n\n## Installation\n\n\nYou can install **phpnut** via composer or by downloading the source.\n\n#### Via Composer:\n\n**phpnut** is available on Packagist as the [`pnut-api/phpnut`](http://packagist.org/packages/pnut-api/phpnut) package:\n\n```\ncomposer require pnut-api/phpnut\n```\n\nTo include the library in your project, you may use normal autoloading similar to `require_once __DIR__.'/vendor/autoload.php';` if your project uses [Composer](https://getcomposer.org/). Otherwise, you can also `require_once 'phpnut.php';` or `require_once 'ezphpnut.php'`.\n\n\n## Usage\n\n### Quick examples\n\n\n#### Create a post\n\n```php\n$app = new phpnut\\phpnut($accessToken);\n\n$app-\u003ecreatePost('Hello world', ['reply_to' =\u003e 123]);\n```\n\n#### Search for a tag\n\n```php\n$app = new phpnut\\phpnut($clientId, $clientSecret);\n\n$posts = $app-\u003esearchHashtags('mndp');\n\nprint_r($posts);\n```\n\n\n## EZphpnut\n\nIf you are planning to design an app for viewing within a browser that requires a login screen etc, this is a great place to start. This aims to hide all the nasty authentication stuff from the average developer. It is also recommended that you start here if you have never worked with OAuth and/or APIs before.\n\n```php\n\u003c?php\n\n$app = new phpnut\\ezphpnut();\n\n// check that the user is signed in\nif ($app-\u003egetSession()) {\n\n    // post on behalf of the user\n    $app-\u003ecreatePost('Hello world');\n\n    // get the current user as JSON\n    $data = $app-\u003egetUser();\n\n    // accessing the user's username\n    echo 'Welcome '.$data['username'];\n\n// if not, redirect to sign in\n} else {\n\n    $url = $app-\u003egetAuthUrl();\n    header('Location: '.$url);\n\n}\n\n?\u003e\n```\n\nA basic working example of ezphpnut is contained in the **ez-example** directory - see the \u003ca href=\"./ez-example/README.md\"\u003eREADME.md\u003c/a\u003e in that directory for more info.\n\n\n## phpnut\n\nUse this class if you need more control of your application (such as running a command line process) or are integrating your code with an existing application that handles sessions/cookies in a different way.\n\n\n### Credentials\n\nIf you already have an access token (in a cron job for example):\n\n```php\n\u003c?php\n\n$accessToken = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';\n\n// construct the phpnut object\n$app = new phpnut\\phpnut($accessToken);\n\n?\u003e\n```\n\nIf you have client credentials:\n\n```php\n\u003c?php\n\n$clientId     = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'; // from https://pnut.io/dev\n$clientSecret = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';\n\n// construct the phpnut object\n$app = new phpnut\\phpnut($clientId, $clientSecret);\n\n?\u003e\n```\n\nYou can alternatively use constants:\n\n```php\n\u003c?php\n\n// define('PNUT_ACCESS_TOKEN', 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX');\ndefine('PNUT_CLIENT_ID', 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX');\ndefine('PNUT_CLIENT_SECRET', 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX');\n\n// construct the phpnut object\n$app = new phpnut\\phpnut();\n\n?\u003e\n```\n\n\n\n### Applications using client ID and client secret\n\nFirst construct your authentication url.\n```php\n\u003c?php\n\n// change these to your app's values\n$clientId     = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';\n$clientSecret = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';\n\n// construct the phpnut object\n$app = new phpnut\\phpnut($clientId,$clientSecret);\n\n$redirectUri  = 'http://localhost/callback.php';\n$scope        = ['stream','email','write_post','follow','messages','update_profile','presence'];\n\n// create an authentication Url\n$url = $app-\u003egetAuthUrl($redirectUri,$scope);\n\n?\u003e\n```\n\nOnce the user has authenticated the app, grab the token in the callback script, and get information about the user.\n\n```php\n\u003c?php\n\n$app = new phpnut\\phpnut($clientId,$clientSecret);\n\n// get the token returned by Pnut\n// (this also sets the token)\n$token = $app-\u003egetAccessToken($redirectUri);\n\n// get info about the user\n$user = $app-\u003egetUser();\n\n// get the unique user id\n$userId = $user['id'];\n\n```\n\nSave the token and user id in a database or elsewhere, then make API calls in future scripts after setting the token.\n\n```php\n\u003c?php\n\n$app-\u003esetAccessToken($token);\n\n// post on behalf of the user with that token\n$app-\u003ecreatePost('Hello world');\n\n```\n\n\n### App streams (websocket)\n\nTo consume the stream, try something like:\n\n```php\n\u003c?php\n\n$app = new phpnut\\phpnut($clientId,$clientSecret);\n\n// You need an app token to consume the stream, get the token returned by Pnut.io\n// (this also sets the token)\n$token = $app-\u003egetAppAccessToken();\n\n// create a stream\n// if you already have a stream you can skip this step\n// this stream is going to consume posts and bookmarks (but not follows)\n$stream = $app-\u003ecreateStream(['post','bookmark','follow']);\n// you might want to save $stream['endpoint'] or $stream['id'] for later so\n// you don't have to re-create the stream\n\n// we need to create a callback function that will do something with posts/bookmarks\n// when they're received from the stream. This function should accept one single\n// parameter that will be the php object containing the meta / data for the event.\nfunction handleEvent($event) {\n    switch ($event['meta']['type']) {\n        case 'post':\n            print \"Handle a post type\\n\";\n            break;\n        case 'bookmark':\n            print \"Handle a bookmark type\\n\";\n            break;\n    }\n}\n\n// register that function as the stream handler\n$app-\u003eregisterStreamFunction('handleEvent');\n\n// open the stream for reading\n$app-\u003eopenStream($stream['endpoint']);\n\n// now we want to process the stream. We have two options. If all we're doing\n// in this script is processing the stream, we can just call:\n// $app-\u003eprocessStreamForever();\n// otherwise you can create a loop, and call $app-\u003eprocessStream($milliseconds)\n// intermittently, like:\nwhile (true) {\n    print \"hello, I'm going to do some other non-streaming things here...\\n\";\n    // now we're going to process the stream for awhile (3 seconds)\n    $app-\u003eprocessStream(3000000);\n    // then do something else...\n}\n```\n\n## Documentation\n\nMore info on the pnut.io API [here](https://docs.pnut.io).\n\nA git repository of it [is also available](https://github.com/pnut-api/api-spec).\n\nFor individual help, ask questions in the [Developer chat](https://patter.chat/18).\n\n\n## Prerequisites\n\n* PHP \u003e= 8.0\n\n\n## Contributors\n\n* \u003ca href=\"https://alpha.app.net/jdolitsky\" target=\"_blank\"\u003e@jdolitsky\u003c/a\u003e\n* \u003ca href=\"https://pnut.io/@ravisorg\" target=\"_blank\"\u003e@ravisorg\u003c/a\u003e\n* \u003ca href=\"https://github.com/wpstudio\" target=\"_blank\"\u003e@wpstudio\u003c/a\u003e\n* \u003ca href=\"https://alpha.app.net/harold\" target=\"_blank\"\u003e@harold\u003c/a\u003e\n* \u003ca href=\"https://alpha.app.net/hxf148\" target=\"_blank\"\u003e@hxf148\u003c/a\u003e\n* \u003ca href=\"https://alpha.app.net/edent\" target=\"_blank\"\u003e@edent\u003c/a\u003e\n* \u003ca href=\"https://pnut.io/@c\" target=\"_blank\"\u003e@cdn\u003c/a\u003e\n* \u003ca href=\"https://pnut.io/@ryantharp\" target=\"_blank\"\u003e@ryantharp\u003c/a\u003e\n* \u003ca href=\"https://pnut.io/@33mhz\" target=\"_blank\"\u003e@33MHz\u003c/a\u003e\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpnut-api%2Fphpnut","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpnut-api%2Fphpnut","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpnut-api%2Fphpnut/lists"}