curl_request()

Make an HTTP request with full control. The convenience helpers curl_get(), curl_post_json(), and curl_post_form() wrap this for common cases.

Syntax

curl_request(url, opts?)

Parameters

Name Type Required Description
url string yes The request URL.
opts array no Request options (see below).

opts keys: method (GET/POST/PUT/PATCH/DELETE), headers (array), query (array, appended to the URL), mode (json / form / raw — how data is encoded), data (request body).

Returns

A response array:

Key Description
ok true if the request succeeded
status HTTP status code
headers Response headers
body Response body (string — parse JSON with fromjson())
error Error message when ok is false

Example

$resp = curl_request("https://api.example.com/v1/items", [
    "method"  => "POST",
    "mode"    => "json",
    "headers" => ["Authorization" => "Bearer " . $token],
    "data"    => ["name" => $record["name"]]
]);

if ( $resp["ok"] ) {
    $created = fromjson($resp["body"]);
    record_update("items", $record["_meta"]["id"], ["external_id" => $created["id"]]);
} else {
    sys_log("API error: " . $resp["error"]);
}

Example output

[
    "ok" => true,
    "status" => 201,
    "headers" => ["Content-Type" => "application/json"],
    "body" => "{\"id\":\"abc123\"}",
    "error" => ""
]

Notes

  • Throws only on a low-level failure; HTTP errors (4xx/5xx) come back with ok => false and the status set — check ok.
  • For an authenticated call through a saved integration, use api_request().

See also: curl_get(), curl_post_json(), api_request()