It’s possible to use PHP’s file_get_contents to PUT JSON data to a RESTful webservice if you don’t want to use cURL.
In this example I’m going to try to add or update a person using the NationBuilder API.
Firstly, we need the URL of the webservice, and the data you want to update as a JSON string.
$access_token = 'set access token here'; $nation = 'set nation name here'; $url = "https://$nation.nationbuilder.com/api/v1/people/push?access_token=$access_token"; // details we want to update $data = [ 'person' => [ 'email_opt_in' => true, 'do_not_contact' => false, 'do_not_call' => false, 'first_name' => 'Robert', 'last_name' => 'Price', 'email' => 'robert.price@email.adress', 'home_address' => [ 'zip' => 'AB1 2CD' ], 'bio' => 'Software developer - robertprice.co.uk' ] ]; $json_data = json_encode($data);
Now we need to setup a stream_context so file_get_contents knows how to handle the request.
Even though we’re PUTing data over HTTPS, we need to use the http context. We set method
to PUT
Content-type to application/json
, Accept
to application/json
, Connection
to close
, and Content-length
to the length of our JSON string. We’re PUTing this data over HTTP1.1 so we set the to
1.1
. We’re sending the JSON in the body of the request, so we set this in the content
.
As we are using HTTPS, we need to configure the ssl
settings. In this case I’m not going to verify the peer or the peer name.
$context = stream_context_create([ 'http' => [ 'method' => 'PUT', 'header' => "Content-type: application/json\r\n" . "Accept: application/json\r\n" . "Connection: close\r\n" . "Content-length: " . strlen($json_data) . "\r\n", 'protocol_version' => 1.1, 'content' => $json_data ], 'ssl' => [ 'verify_peer' => false, 'verify_peer_name' => false ] ]);
Finally we PUT the data by calling file_get_contents. NationBuilder returns a JSON response to this call, so we can decode that and echo a confirmation message to the screen.
$rawdata = file_get_contents($url, false, $context); if ($rawdata === false) { exit("Unable to update data at $url"); } $data = json_decode($rawdata, true); if (JSON_ERROR_NONE !== json_last_error()) { exit("Failed to parse json: " . json_last_error_msg()); } echo "Updated details for {$data['person']['first_name']} {$data['person']['last_name']}\n";
The full code for this, and some other examples of querying NationBuilder using PHP, can be found in my NationBuilder People In Nation – GitHub repository.
You may also be interested in my previous article on POSTing JSON to Web Service.