PHP

HOW TO SEND JSON DATA WITH PHP CURL

CURL INIT $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "http://site.com"); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

CURL POST JSON (A)

01

02

APPEND DATA TO SEND curl_setopt($ch, CURLOPT_POSTFIELDS, [   "name" => "Jon Doe",   "colors" => json_encode(["Red",    "Green", "Blue"]) ]);

CURL POST JSON (B)

03

CURL SEND $result = curl_exec($ch);  curl_close($ch);

CURL POST JSON (C)

DATA TO QUERY STRING $data = [   "name" => "Jon Doe",   "colors" => json_encode(["Red",   "Green", "Blue"]) ]; $data = "?" . http_build_query($data);

04

CURL GET JSON (A)

05

CURL INIT $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, "http://site.com"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

CURL GET JSON (B)

06

CURL SEND $result = curl_exec($ch); curl_close($ch);

CURL GET JSON (C)