This website recently got a major rebuild; if you're missing something, let Lorna know.

PUTting data fields with PHP cURL



This is a little post about how to PUT multiple data fields using the PHP cURL extension. Why I wanted to do this in the first place is beyond the scope of this post, since its quite a long story. The curl command line allows data fields to be sent with a PUT request, and I wanted to do the same from PHP. Here is a snippet of code to show how I did it.

$data = array("a" => $a);
$ch = curl_init($this->_serviceUrl . $id);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($data));

$response = curl_exec($ch);
if(!$response) {
    return false;
}

I'm putting this here so I remember for next time - if it helps you as well then even better :)


In: php
Tags: #curl #php #webservice