Sample code for 30+ languages & platforms
PHP

Send a REST Request using StringBuilder Bodies

See more REST Examples

Demonstrates Rest.FullRequestSb, which sends a complete request whose text body is read from a StringBuilder and stores the text response body in a second StringBuilder.

Background. Using StringBuilder objects for the request and response bodies avoids extra string conversions, which is helpful when working with large payloads.

Chilkat PHP Downloads

Chilkat2 PHP
<?php

use Chilkat2\Rest;
use Chilkat2\StringBuilder;

$rest = new Rest();
$bTls = true;
$bAutoReconnect = true;
try {
    $rest->Connect('example.com', 443, $bTls, $bAutoReconnect);

    // FullRequestSb sends a request whose text body is read from a StringBuilder and stores the text
    // response body in a second StringBuilder.  This avoids extra string conversions for large bodies.
    $sbRequest = new StringBuilder();
    $sbRequest->Append('{ "query": "chilkat" }');

    $rest->AddHeader('Content-Type', 'application/json');

    $sbResponse = new StringBuilder();
    $rest->FullRequestSb('POST', '/api/search', $sbRequest, $sbResponse);

    $responseText = $sbResponse->GetAsString();
} catch (\Chilkat2\Exception $e) {
    echo $e->getMessage(), "\n";
    exit;
}

echo $responseText, "\n";