webCOMAND

client::post()

Make an HTTP POST request to a URL.

Prototype

response post(string $url, array $options = [])

Parameters

Return

A response object is returned.  For details, see return for send().

webCOMAND Get Web Service Example

$url = 'https://demo.webcomand.com/ws/get';
$token = 'authentication-token-here';
$json = '{"query": "SELECT OID, Title FROM HelpDoc"}';
$response = \io_comand_web\client::post($url, [
    'headers' => [
        "Content-Type: application/json",
        "Authorization: Token $token"
    ],
    'body' => $json
]);
if(!$response || !isset($response->info)) {
    echo("Unexpected result.\n");
}
echo("Response Payload: " . $response->payload . "\n");
echo("Response HTTP Code: " . $response->info['http_code'] . "\n");

// if the response was JSON
if(isset($response->data)) {
    echo("JSON Data: " . print_r($response->data, TRUE));
}

webCOMAND Put Web Service Example

$url = 'https://demo.webcomand.com/ws/put';
$token = 'authentication-token-here';
$data = (object)[
    'type' => 'COMAND',
    'version' => '1.0',
    'time' => date('Y-m-d H:i:s'),
    'contents' => [
        (object)[
            'Type' => 'HelpDocText',
            'Title' => 'My Help'
        ]
    ]
];
$response = \io_comand_web\client::post($url, [
    'headers' => [
        "Authorization: Token $token"
    ],
    'fields' => [
        'data' => json_encode($data)
    ]
]);
if(!$response || !isset($response->info)) {
    echo("Unexpected result.\n");
}
echo("Response Payload: " . $response->payload . "\n");
echo("Response HTTP Code: " . $response->info['http_code'] . "\n");

// if the response was JSON
if(isset($response->data)) {
    echo("Response JSON: " . print_r($response->data, TRUE));
}

File Upload Example

$url = 'https://demo.webcomand.com/resume-form';
$filename = 'resume.pdf';
$contents = file_get_contents($filename);
$info = \io_comand_util\mime_type::get_info_from_data($contents);
$response = \io_comand_web\client::post($url, [
    'headers' => [
        "Authorization: $token"
    ],
    'fields' => [
        'resume' => (object)[
            'type' => 'string',
            'filename' => $filename,
            'filetype' => $info['mime'],
            'value' => $contents
        ]
    ]
]);
if(!$response || !isset($response->info)) {
    echo("Unexpected result.\n");
}
echo("Response HTTP Code: " . $response->info['http_code'] . "\n");

Related

get(), request(), send()