Rob Keplin - Software Engineer

# PHP – Making an HTTP PUT Request Using cURL

Category: PHP

A quick self-reminder of how to make an HTTP PUT request with PHP. The example below creates an Elasticsearch index with a specific mapping.

$body = array(
  'mappings' => array(
      'properties' => array(
            'make'  => array('type' => 'keyword'),
            'model' => array('type' => 'keyword'),
            'year'  => array('type' => 'integer'),
            'color' => array('type' => 'string')
      )
  )
);

$body = $json_encode($body)

$ch = curl_init('http://localhost:9200/car');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json',
    'Content-Length: ' . strlen($body))
);

$result = curl_exec($ch);

More examples of making HTTP Requests using PHP can be found here.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

*

*