Skip to content

Instantly share code, notes, and snippets.

@OneStep21
Last active July 29, 2021 08:25
Show Gist options
  • Save OneStep21/e95bf0714dcc6a4220d0c0a1d399ef58 to your computer and use it in GitHub Desktop.
Save OneStep21/e95bf0714dcc6a4220d0c0a1d399ef58 to your computer and use it in GitHub Desktop.
Next, we’ve used the CURLOPT_HTTPHEADER option to set the Content-Type header to application/json to inform the API server that we’re sending JSON data.
<?php
$url = '{POST_REST_ENDPOINT}';
$curl = curl_init();
$fields = array(
'field_name_1' => 'Value 1',
'field_name_2' => 'Value 2',
'field_name_3' => 'Value 3'
);
$json_string = json_encode($fields);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, TRUE);
curl_setopt($curl, CURLOPT_POSTFIELDS, $json_string);
//If you don't set the Content-Type header, it will use application/x-www-form-urlencoded as the default value.
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true );
$data = curl_exec($curl);
curl_close($curl);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment