Skip to content

opwire/sample-cmdline-php

Repository files navigation

opwire-agent: sample command line in PHP

Installation

Checkout source code

Clone example source code from github repository:

git clone https://github.com/opwire/sample-cmdline-php.git

Change the project home to current working directory:

cd sample-cmdline-php

Download opwire-agent

To download the latest opwire-agent on Linux/macOS/BSD systems, run:

curl https://opwire.org/opwire-agent/install.sh | bash

For other systems:

  • Download the relevant opwire-agent release,
  • Extract the opwire-agent or opwire-agent.exe binary from the archive to example folder (current directory).

project-home-dir

Call the service from browsers

Execute the following command (with configuration file ./opwire-agent.cfg):

./opwire-agent serve

Open the URL http://localhost:8888/$?type=microservice&type=php:

example-output

Test the service with curl

Default input/output format (json)

Execute the following command:

./opwire-agent serve -p=8888 --default-command="php example.php"

Notice: above command is similar to

./opwire-agent serve -p=8888 --default-command="php example.php --format=json"

or

./opwire-agent serve -p=8888 --default-command="php example.php --input-format=json --output-format=json"

Valid input (a JSON object)

Make a HTTP request with curl:

curl -v \
  --request POST \
  --url 'http://localhost:8888/$?type=microservice&type=php' \
  --data '{
  "name": "Opwire",
  "url": "https://opwire.org/"
}'

Result:

> POST /$?type=microservice&type=php HTTP/1.1
> User-Agent: curl/7.35.0
> Host: localhost:8888
> Accept: */*
> Content-Length: 52
> Content-Type: application/x-www-form-urlencoded
>
* upload completely sent off: 52 out of 52 bytes
< HTTP/1.1 200 OK
< Content-Type: text/plain
< X-Exec-Duration: 0.038846
< Date: Fri, 15 Mar 2019 11:16:53 GMT
< Content-Length: 686
<
{
    "OPWIRE_EDITION": {
        "revision": "b4cec6a",
        "version": "v1.0.1"
    },
    "OPWIRE_REQUEST": {
        "header": {
            "Accept": [
                "*\/*"
            ],
            "Content-Length": [
                "52"
            ],
            "Content-Type": [
                "application\/x-www-form-urlencoded"
            ],
            "User-Agent": [
                "curl\/7.35.0"
            ]
        },
        "query": {
            "type": [
                "microservice",
                "php"
            ]
        },
        "params": null
    },
    "input": {
        "name": "Opwire",
        "url": "https:\/\/opwire.org\/"
    }
}

Invalid input (not a JSON object)

Make a HTTP request with curl:

curl -v \
  --request POST \
  --url 'http://localhost:8888/$?type=microservice&type=php' \
  --data 'Not a JSON object'

Result:

> POST /$?type=microservice&type=php HTTP/1.1
> User-Agent: curl/7.35.0
> Host: localhost:8888
> Accept: */*
> Content-Length: 17
> Content-Type: application/x-www-form-urlencoded
>
* upload completely sent off: 17 out of 17 bytes
< HTTP/1.1 500 Internal Server Error
< Content-Type: text/plain
< X-Error-Message: exit status 1
< Date: Fri, 15 Mar 2019 11:17:44 GMT
< Content-Length: 76
<
{
    "name": "SyntaxError",
    "message": "Syntax error, malformed JSON"
}

JSON input, plaintext output

Execute the following command:

./opwire-agent serve -p=8888 \
  --default-command="php example.php --input-format=json --output-format=text"

Valid input (a JSON object)

Make a HTTP request with curl:

curl -v \
  --request POST \
  --url 'http://localhost:8888/$?type=microservice&type=php' \
  --data '{
  "name": "Opwire",
  "url": "https://opwire.org/"
}'

Result:

> POST /$?type=microservice&type=php HTTP/1.1
> User-Agent: curl/7.35.0
> Host: localhost:8888
> Accept: */*
> Content-Length: 54
> Content-Type: application/x-www-form-urlencoded
>
* upload completely sent off: 54 out of 54 bytes
< HTTP/1.1 200 OK
< Content-Type: text/plain
< X-Exec-Duration: 0.018843
< Date: Fri, 15 Mar 2019 11:19:36 GMT
< Content-Length: 554
<
OPWIRE_EDITION:
{
    "revision": "b4cec6a",
    "version": "v1.0.1"
}

OPWIRE_REQUEST:
{
    "header": {
        "Accept": [
            "*\/*"
        ],
        "Content-Length": [
            "54"
        ],
        "Content-Type": [
            "application\/x-www-form-urlencoded"
        ],
        "User-Agent": [
            "curl\/7.35.0"
        ]
    },
    "query": {
        "type": [
            "microservice",
            "php"
        ]
    },
    "params": null
}

input:
{
    "name": "Opwire",
    "url": "https:\/\/opwire.org\/"
}

Invalid input (not a JSON object)

Make a HTTP request with curl:

curl -v \
  --request POST \
  --url 'http://localhost:8888/$?type=microservice&type=php' \
  --data 'Not a JSON object'

Result:

> POST /$?type=microservice&type=php HTTP/1.1
> User-Agent: curl/7.35.0
> Host: localhost:8888
> Accept: */*
> Content-Length: 17
> Content-Type: application/x-www-form-urlencoded
>
* upload completely sent off: 17 out of 17 bytes
< HTTP/1.1 500 Internal Server Error
< Content-Type: text/plain
< X-Error-Message: exit status 1
< Date: Fri, 15 Mar 2019 11:20:22 GMT
< Content-Length: 28
<
Syntax error, malformed JSON

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b your-new-feature)
  3. Commit your changes (git commit -am "Add some feature")
  4. Push to the branch (git push origin your-new-feature)
  5. Create new Pull Request

License

MIT

See LICENSE to see the full text.

Releases

No releases published

Packages

No packages published

Languages