Created
October 2, 2017 19:10
-
-
Save andrzj/2510f376ef232937b7b7e0797b0ff6df to your computer and use it in GitHub Desktop.
POSTing JSON Data With PHP cURL
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$data = array("name" => "Hagrid", "age" => "36"); | |
$data_string = json_encode($data); | |
$ch = curl_init('http://api.local/rest/users'); | |
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); | |
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($ch, CURLOPT_HTTPHEADER, array( | |
'Content-Type: application/json', | |
'Content-Length: ' . strlen($data_string)) | |
); | |
$result = curl_exec($ch); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment