curl_post_json()

POST a JSON body to a URL. Sets the method to POST and JSON-encodes data. A wrapper around curl_request().

Syntax

curl_post_json(url, data?, opts?)

Parameters

Name Type Required Description
url string yes The request URL.
data array no The body — encoded as JSON.
opts array no Extra options (e.g. headers) — see curl_request().

Returns

A response array (ok, status, headers, body, error).

Example

$resp = curl_post_json("https://hooks.example.com/notify", [
    "event"  => "deal_won",
    "record" => $record["_meta"]["id"],
    "amount" => $record["amount"]
], ["headers" => ["X-Api-Key" => $key]]);

sys_log("Webhook status: " . $resp["status"]);

Example output

["ok" => true, "status" => 200, "body" => "{\"received\":true}", ...]

See also: curl_post_form(), curl_request()