title: How to set Hikvision camera parameters using PHP curl
body: <p>I am attempting to automate Hikvision camera configuration using the Hikvision IPMD XML API. I am able to retrieve camera data using any of the documented services, but my attempts to change field values are failing. Here's the code I'm using the deviceInfo to change the System Device Name.</p>
  
  <pre><code>define('_EOL', "\n");
  
  // Set device name
  $url='http://192.168.6.12/System/deviceInfo';
  $creds='admin:password';
  $body='&lt;?xml version="1.0" encoding="UTF-8"?&gt;'.
      '&lt;DeviceInfo xmlns="http://www.hikvision.com/ver10/XMLSchema" version="1.0"&gt;'.
      '&lt;deviceName&gt;Cam 7 AAAA&lt;/deviceName&gt;'.
      '&lt;deviceID&gt;88&lt;/deviceID&gt;'.
      '&lt;/DeviceInfo&gt; ';
  $tmpFile='temp.txt';
  file_put_contents($tmpFile, $body);
  $bytes=filesize($tmpFile);
  $stream=fopen($tmpFile, 'r');
  echo('Bytes='.$bytes.', handle='.$stream._EOL);
  
  $session=curl_init(); // initialize a curl session
  curl_setopt($session,CURLOPT_URL, $url);
  curl_setopt($session,CURLOPT_USERPWD, $creds);
  curl_setopt($session,CURLOPT_HEADER,TRUE);
  curl_setopt($session,CURLOPT_RETURNTRANSFER, TRUE);
  //curl_setopt($session,CURLOPT_PUT, TRUE);
  curl_setopt($session,CURLOPT_CUSTOMREQUEST, 'PUT');
  curl_setopt($session,CURLOPT_CUSTOMREQUEST, 'Content-Type: application/xml; charset="UTF-8"');
  curl_setopt($session,CURLOPT_INFILE, $stream);  // XML block
  curl_setopt($session,CURLOPT_INFILESIZE, $bytes); // size in bytes
  
  $page=curl_exec($session);
  $info=curl_getinfo($session);
  $err='curl error: '.curl_errno($session).' '.curl_error($session);
  
  curl_close($session);
  
  print_r($info);
  echo($err._EOL);
  echo($page._EOL);
  </code></pre>
  
  <p>And here's the curl info on the request. It seems the camera is not responding to the request.</p>
  
  <pre><code>Bytes=185, handle=Resource id #6
  Array
  (
      [url] =&gt; http://192.168.6.12/System/deviceInfo
      [content_type] =&gt; 
      [http_code] =&gt; 0
      [header_size] =&gt; 0
      [request_size] =&gt; 197
      [filetime] =&gt; -1
      [ssl_verify_result] =&gt; 0
      [redirect_count] =&gt; 0
      [total_time] =&gt; 0.016
      [namelookup_time] =&gt; 0
      [connect_time] =&gt; 0.016
      [pretransfer_time] =&gt; 0.016
      [size_upload] =&gt; 0
      [size_download] =&gt; 0
      [speed_download] =&gt; 0
      [speed_upload] =&gt; 0
      [download_content_length] =&gt; -1
      [upload_content_length] =&gt; 185
      [starttransfer_time] =&gt; 0.016
      [redirect_time] =&gt; 0
      [certinfo] =&gt; Array
          (
          )
  
      [primary_ip] =&gt; 192.168.6.12
      [primary_port] =&gt; 80
      [local_ip] =&gt; 192.168.6.99
      [local_port] =&gt; 2903
      [redirect_url] =&gt; 
  )
  curl error: 52 Empty reply from server
  </code></pre>
  
  <p>By the way, this is the camera response to GET /System/deviceInfo:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;DeviceInfo version="1.0" xmlns="http://www.hikvision.com/ver10/XMLSchema"&gt;
  &lt;deviceName&gt;Cam 2 DEMO&lt;/deviceName&gt;
  &lt;deviceID&gt;88&lt;/deviceID&gt;
  &lt;deviceDescription&gt;IPCamera&lt;/deviceDescription&gt;
  &lt;deviceLocation&gt;hangzhou&lt;/deviceLocation&gt;
  &lt;systemContact&gt;Hikvision.China&lt;/systemContact&gt;
  &lt;model&gt;DS-2CD2042WD-I&lt;/model&gt;
  &lt;serialNumber&gt;DS-2CD2042WD-I20170519BBWR764768522&lt;/serialNumber&gt;
  &lt;macAddress&gt;18:68:cb:76:48:ce&lt;/macAddress&gt;
  &lt;firmwareVersion&gt;V5.4.5&lt;/firmwareVersion&gt;
  &lt;firmwareReleasedDate&gt;build 170124&lt;/firmwareReleasedDate&gt;
  &lt;bootVersion&gt;V1.3.4&lt;/bootVersion&gt;
  &lt;bootReleasedDate&gt;100316&lt;/bootReleasedDate&gt;
  &lt;hardwareVersion&gt;0x0&lt;/hardwareVersion&gt;
  &lt;/DeviceInfo&gt;
  </code></pre>
  
  <p>Previously I tried using all three EOL types (Mac, linux, DOS) before I removed them all, as in the code above. I know the authentication works because I can read anything from the camera. I can also successfully send PUT commands that have no data or XML block requirements, such as a reboot. So I suspect some issue with how I am sending the XML via curl. Also, I can change the device name via web browser while logged in. Then when I read the device information, the changes show up in the XML response. Any ideas where I've gone wrong? Thanks.</p>
  
  <p><strong>UPDATE 8/30/2018</strong></p>
  
  <p>I gave up on using the camera API for configuration. I only found a few commands that actually worked (like reboot and some read-only requests). Instead, I used Watir and Ruby to access the camera through its web interface. That has proven to be a very reliable way to automate camera configuration.</p>