Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat-4176-send-http-request #38

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions php/.gitignore
Expand Up @@ -456,6 +456,7 @@ gradle-app.setting
##### Composer
composer.phar
/vendor/
vendor

# Commit your application's lock file https://getcomposer.org/doc/01-basic-usage.md#commit-your-composer-lock-file-to-version-control
# You may choose to ignore a library lock file http://getcomposer.org/doc/02-libraries.md#lock-file
Expand Down
54 changes: 54 additions & 0 deletions php/send-http-request/README.md
@@ -0,0 +1,54 @@
# Function to send request and get response

A PHP Cloud Function for getting the response from request.

_Example input:_

```json
{
"url": "https://demo.appwrite.io/v1/locale/countries/eu",
"method": "GET",
"headers": {
"x-client-version": "1.0.0"
},
"body": ""
}
```

_Example output:_


```json
{
"success": true,
"response": {
"headers": "{headers_content}",
"code": 200,
"body": "{body_content}"
}
}
```

## 🚀 Deployment

1. Clone this repository, and enter this function folder:

```
$ git clone https://github.com/open-runtimes/examples.git && cd examples
$ cd php/send-http-request
```

2. Enter this function folder and build the code:
```
docker run --rm --interactive --tty --volume $PWD:/usr/code openruntimes/php:8.1 sh /usr/local/src/build.sh
```

3. Start the Open Runtime:
```
docker run -p 3000:3000 -e INTERNAL_RUNTIME_KEY=secret-key -e INTERNAL_RUNTIME_ENTRYPOINT=index.php --rm --interactive --tty --volume $PWD/code.tar.gz:/tmp/code.tar.gz:ro openruntimes/php:8.1 sh /usr/local/src/start.sh
```

Your function is now listening on port `3000`, and you can execute it by sending `GET` request with appropriate authorization headers. To learn more about runtime, you can visit PHP runtime [README](https://github.com/open-runtimes/open-runtimes/tree/main/runtimes/php-8.1).

## 📝 Notes
- This example is compatible with PHP 8.1. Other versions may work but are not guaranteed to work as they haven't been tested.
11 changes: 11 additions & 0 deletions php/send-http-request/composer.json
@@ -0,0 +1,11 @@
{
"name": "opr/send-http-request",
"description": "",
"type": "library",
"license": "ISC",
"authors": [],
"require": {
"php": ">=8.0.0",
"guzzlehttp/guzzle": "^7.0"
}
}
51 changes: 51 additions & 0 deletions php/send-http-request/index.php
@@ -0,0 +1,51 @@
<?php

require 'vendor/autoload.php';

use GuzzleHttp\Client;

return function($req, $res) {
try {
$payload = \json_decode($req['payload'], true);
if (!isset($payload['url']) || empty($url = \trim($payload['url']))) {
throw new \Exception('Please provide valid url.');
}
if (!isset($payload['method']) || empty($method = \trim($payload['method']))) {
throw new \Exception('Please provide valid method.');
}
$body = isset($payload['body']) ? \trim($payload['body']) : null;
$headers = isset($payload['headers']) ? $payload['headers'] : [];
} catch(\Exception $err) {
\var_dump($err);

$res->json([
'success' => false,
'message' => 'Payload is invalid: ' . $err->getMessage()
]);
return;
}

$client = new Client();
$response = $client->request($method, $url, [
'body' => $body,
'headers' => $headers
]);

if ($response->getStatusCode() >= 200 && $response->getStatusCode() < 300) {
$res->json([
'success' => true,
'response' => [
'headers' => $response->getHeaders(),
'code' => $response->getStatusCode(),
'body' => $response->getBody()->getContents()
]
]);
stnguyen90 marked this conversation as resolved.
Show resolved Hide resolved
return;
}

$res->json([
'success' => false,
'message' => $response->getBody()->getContents()
]);
}
?>