{"id":13548910,"url":"https://github.com/GoogleCloudPlatform/functions-framework-php","last_synced_at":"2025-04-02T21:32:34.115Z","repository":{"id":37602208,"uuid":"225494695","full_name":"GoogleCloudPlatform/functions-framework-php","owner":"GoogleCloudPlatform","description":"FaaS (Function as a service) framework for writing portable PHP functions","archived":false,"fork":false,"pushed_at":"2025-03-17T22:54:12.000Z","size":292,"stargazers_count":213,"open_issues_count":9,"forks_count":37,"subscribers_count":30,"default_branch":"main","last_synced_at":"2025-03-30T15:42:44.796Z","etag":null,"topics":["cloud-functions","functions-as-a-service","google-cloud","php"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/GoogleCloudPlatform.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-12-03T00:23:21.000Z","updated_at":"2025-03-26T03:21:53.000Z","dependencies_parsed_at":"2023-10-11T03:55:08.518Z","dependency_job_id":"db8e0cc1-24cc-4fea-b5bd-65202ca80a7e","html_url":"https://github.com/GoogleCloudPlatform/functions-framework-php","commit_stats":{"total_commits":155,"total_committers":28,"mean_commits":5.535714285714286,"dds":0.6838709677419355,"last_synced_commit":"ec9cb5fa8065b01d13c698b3890539d018a80ac9"},"previous_names":[],"tags_count":17,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GoogleCloudPlatform%2Ffunctions-framework-php","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GoogleCloudPlatform%2Ffunctions-framework-php/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GoogleCloudPlatform%2Ffunctions-framework-php/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/GoogleCloudPlatform%2Ffunctions-framework-php/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/GoogleCloudPlatform","download_url":"https://codeload.github.com/GoogleCloudPlatform/functions-framework-php/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246895983,"owners_count":20851366,"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","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":["cloud-functions","functions-as-a-service","google-cloud","php"],"created_at":"2024-08-01T12:01:15.888Z","updated_at":"2025-04-02T21:32:34.108Z","avatar_url":"https://github.com/GoogleCloudPlatform.png","language":"PHP","readme":"# Functions Framework for PHP [![Packagist](https://poser.pugx.org/google/cloud-functions-framework/v/stable)](https://packagist.org/packages/google/cloud-functions-framework)\n\n[![PHP unit CI][ff_php_unit_img]][ff_php_unit_link] [![PHP lint CI][ff_php_lint_img]][ff_php_lint_link] [![PHP conformace CI][ff_php_conformance_img]][ff_php_conformance_link] ![Security Scorecard](https://api.securityscorecards.dev/projects/github.com/GoogleCloudPlatform/functions-framework-php/badge)\n\nAn open source FaaS (Function as a service) framework for writing portable\nPHP functions.\n\nThe Functions Framework lets you write lightweight functions that run in many\ndifferent environments, including:\n\n*   Your local development machine\n*   [Knative](https://github.com/knative/)-based environments\n\nThe framework allows you to go from:\n\n```php\nuse Psr\\Http\\Message\\ServerRequestInterface;\n\nfunction helloHttp(ServerRequestInterface $request)\n{\n    return \"Hello World from a PHP HTTP function!\" . PHP_EOL;\n}\n```\n\nTo:\n\n```sh\ncurl http://my-url\n# Output: \"Hello World from a PHP HTTP function!\"\n```\n\nAll without needing to worry about writing an HTTP server or complicated request\nhandling logic.\n\n\u003e Watch [this video](https://youtu.be/yMEcyAkTliU?t=912) to learn more about Functions Frameworks.\n\n# Features\n\n*   Spin up a local development server for quick testing\n*   Invoke a function in response to a request\n*   Automatically unmarshal events conforming to the [CloudEvents](https://cloudevents.io/) spec\n*   Portable between serverless platforms\n\n# Installation\n\nAdd the Functions Framework to your `composer.json` file using\n[Composer][composer].\n\n```sh\ncomposer require google/cloud-functions-framework\n```\n\n[composer]: https://getcomposer.org/\n\n# Define your Function\n\nCreate an `index.php` file with the following contents:\n\n```php\n\u003c?php\n\nuse Psr\\Http\\Message\\ServerRequestInterface;\n\nfunction helloHttp(ServerRequestInterface $request)\n{\n    return \"Hello World from a PHP HTTP function!\" . PHP_EOL;\n}\n```\n\n# Quickstarts\n\n## Run your function locally\n\nAfter completing the steps under **Installation** and **Define your Function**,\nrun the following commands:\n\n```sh\nexport FUNCTION_TARGET=helloHttp\nphp -S localhost:8080 vendor/google/cloud-functions-framework/router.php\n```\n\nOpen `http://localhost:8080/` in your browser and see *Hello World from a PHP HTTP function!*.\n\n\n## Run your function in a container\n\nAfter completing the steps under **Installation** and **Define your Function**, build the container using the example `Dockerfile`:\n\n```\ndocker build . \\\n    -f vendor/google/cloud-functions-framework/examples/hello/Dockerfile \\\n    -t my-cloud-function\n```\n\nRun the cloud functions framework container:\n\n```\ndocker run -p 8080:8080 \\\n    -e FUNCTION_TARGET=helloHttp \\\n    my-cloud-function\n```\n\nOpen `http://localhost:8080/` in your browser and see *Hello World from a PHP\nHTTP function*. You can also send requests to this function using `curl` from\nanother terminal window:\n\n```sh\ncurl localhost:8080\n# Output: Hello World from a PHP HTTP function!\n```\n\n## Run your function on Google Cloud Run Functions\n\n**NOTE**: For an extensive list of samples, see the [PHP functions samples][functions-samples]\nand the [official how-to guides][functions-how-to].\n\nFollow the [Cloud Run function quickstart](https://cloud.google.com/run/docs/quickstarts/functions/deploy-functions-gcloud#php) for PHP to learn how to deploy a function to Cloud Run.\n\n## Run your function as a container in Cloud Run\n\nYou can manually build your function as a container and deploy it into Cloud Run. Follow the [Cloud Run instructions for building a function](https://cloud.google.com/run/docs/building/functions) for complete instructions.\n\n## Use CloudEvents\n\nThe Functions Framework can unmarshall incoming [CloudEvents][cloud-events]\npayloads to a `cloudevent` object. This will be passed as arguments to your\nfunction when it receives a request. Note that your function must use the\ncloudevent function signature:\n\n```php\nuse Google\\CloudFunctions\\CloudEvent;\n\nfunction helloCloudEvent(CloudEvent $cloudevent)\n{\n    // Print the whole CloudEvent\n    $stdout = fopen('php://stdout', 'wb');\n    fwrite($stdout, $cloudevent);\n}\n```\n\nYou will also need to set the `FUNCTION_SIGNATURE_TYPE` environment\nvariable to `cloudevent`.\n\n```sh\nexport FUNCTION_TARGET=helloCloudEvent\nexport FUNCTION_SIGNATURE_TYPE=cloudevent\nphp -S localhost:8080 vendor/google/cloud-functions-framework/router.php\n```\n\nIn a separate tab, make a cURL request in Cloud Event format to your function:\n\n```\ncurl localhost:8080 \\\n    -H \"ce-id: 1234567890\" \\\n    -H \"ce-source: //pubsub.googleapis.com/projects/MY-PROJECT/topics/MY-TOPIC\" \\\n    -H \"ce-specversion: 1.0\" \\\n    -H \"ce-type: com.google.cloud.pubsub.topic.publish\" \\\n    -d '{\"foo\": \"bar\"}'\n```\n\nYour original process should output the following:\n\n```\nCLOUDEVENT metadata:\n- id: 1234567890\n- source: //pubsub.googleapis.com/projects/MY-PROJECT/topics/MY-TOPIC\n- specversion: 1.0\n- type: com.google.cloud.pubsub.topic.publish\n- datacontenttype:\n- dataschema:\n- subject:\n- time:\n```\n\n**IMPORTANT**: The above tutorials to deploy to a docker container and to\nCloud Run work for CloudEvents as well, as long as `FUNCTION_TARGET` and\n`FUNCTION_SIGNATURE_TYPE` are set appropriately.\n\n[cloud-events]: http://cloudevents.io\n\n## Working with PSR-7 HTTP Objects\n\nThe first parameter of your function is a `Request` object which implements the\nPSR-7 `ServerRequestInterface`:\n\n```php\nuse Psr\\Http\\Message\\ServerRequestInterface;\n\nfunction helloHttp(ServerRequestInterface $request): string\n{\n    return sprintf(\"Hello %s from PHP HTTP function!\" . PHP_EOL,\n        $request-\u003egetQueryParams()['name'] ?? 'World');\n}\n```\n\nYou can return a PSR-7 compatible `ResponseInterface` instead of a string. This\nallows you to set additional request properties such as the HTTP Status Code\nand headers.\n\n```php\nuse Psr\\Http\\Message\\ServerRequestInterface;\nuse Psr\\Http\\Message\\ResponseInterface;\nuse GuzzleHttp\\Psr7\\Response;\nuse GuzzleHttp\\Psr7\\Utils;\n\nfunction helloHttp(ServerRequestInterface $request): ResponseInterface\n{\n    $body = sprintf(\"Hello %s from PHP HTTP function!\" . PHP_EOL,\n        $request-\u003egetQueryParams()['name'] ?? 'World');\n\n    return (new Response())\n        -\u003ewithBody(Utils::streamFor($body))\n        -\u003ewithStatus(418) // I'm a teapot\n        -\u003ewithHeader('Foo', 'Bar');\n}\n```\n\nA request to this function will produce a response similar to the following:\n\n```\nHTTP/1.1 418 I'm a teapot\nHost: localhost:8080\nDate: Wed, 03 Jun 2020 00:48:38 GMT\nFoo: Bar\n\nHello World from PHP HTTP function!\n```\n\nSee the [PSR-7 documentation][psr7] documentation for more on working\nwith the request and response objects.\n\n[psr7]: https://www.php-fig.org/psr/psr-7/\n\n## Use Google Cloud Storage\n\nWhen you require the `google/cloud-storage` package with composer, the functions\nframework will register the `gs://` stream wrapper. This enables your function\nto read and write to Google Cloud Storage as you would any filesystem:\n\n```php\n// Get the contents of an object in GCS\n$object = file_get_contents('gs://{YOUR_BUCKET_NAME}/object.txt');\n// Make modifications\n$object .= \"\\nadd a line\";\n// Write the new contents back to GCS\nfile_put_contents('gs://{YOUR_BUCKET_NAME}/object.txt', $object);\n```\n\nYou can unregister this at any time by using\n[`stream_wrapper_unregister`][stream_wrapper_unregister]:\n\n```php\n// unregister the automatically registered one\nstream_wrapper_unregister('gs');\n```\n\n[stream_wrapper_unregister]: https://www.php.net/manual/en/function.stream-wrapper-unregister.php\n\n## Run your function on Knative\n\nCloud Run and Cloud Run on GKE both implement the\n[Knative Serving API](https://www.knative.dev/docs/). The Functions Framework is\ndesigned to be compatible with Knative environments. Just build and deploy your\ncontainer to a Knative environment.\n\nIf you want even more control over the environment, you can\n[deploy your container image to Cloud Run on GKE](https://cloud.google.com/run/docs/quickstarts/prebuilt-deploy-gke).\nWith Cloud Run on GKE, you can run your function on a GKE cluster, which gives\nyou additional control over the environment (including use of GPU-based\ninstances, longer timeouts and more).\n\n# Configure the Functions Framework\n\nYou can configure the Functions Framework using the environment variables shown below:\n\n| Environment variable      | Description\n| ------------------------- | -----------\n| `FUNCTION_TARGET`         | The name of the exported function to be invoked in response to requests.\n| `FUNCTION_SOURCE` | The name of the file containing the source code for your function to load. Default: **`index.php`** (if it exists)\n| `FUNCTION_SIGNATURE_TYPE` | The signature used when writing your function. Controls unmarshalling rules and determines which arguments are used to invoke your function. Can be either `http`, `event`, or `cloudevent`. Default: **`http`**\n\n# Contributing\n\nContributions to this library are welcome and encouraged. See\n[CONTRIBUTING](CONTRIBUTING.md) for more information on how to get started.\n\n[ff_php_unit_img]: https://github.com/GoogleCloudPlatform/functions-framework-php/workflows/PHP%20Unit%20CI/badge.svg\n[ff_php_unit_link]:  https://github.com/GoogleCloudPlatform/functions-framework-php/actions?query=workflow%3A\"PHP+Unit+CI\"\n[ff_php_lint_img]: https://github.com/GoogleCloudPlatform/functions-framework-php/workflows/PHP%20Lint%20CI/badge.svg\n[ff_php_lint_link]:  https://github.com/GoogleCloudPlatform/functions-framework-php/actions?query=workflow%3A\"PHP+Lint+CI\"\n[ff_php_conformance_img]: https://github.com/GoogleCloudPlatform/functions-framework-php/workflows/PHP%20Conformance%20CI/badge.svg\n[ff_php_conformance_link]:  https://github.com/GoogleCloudPlatform/functions-framework-php/actions?query=workflow%3A\"PHP+Conformance+CI\"\n","funding_links":[],"categories":["PHP"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FGoogleCloudPlatform%2Ffunctions-framework-php","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FGoogleCloudPlatform%2Ffunctions-framework-php","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FGoogleCloudPlatform%2Ffunctions-framework-php/lists"}