title: Digest Authentication PHP
body: <p>I have to authenticate to a inverter web server, read a txt file, push information to another server, close and repeat the operation after 1 minute. This is the information that I sniff after successive authentications: </p>
  
  <pre><code>GET /data/sys.txt 
  HTTP/1.1..
  Host: 13.3.89.50..
  Connection: keep-alive
  Authorization: Digest username="customer", 
  realm="Sunways-Webserver", 
  nonce="23618", 
  uri="/data/sys.txt", 
  response="370c48d4879451443c156ec003739de0", 
  qop=auth, 
  nc=00000001, 
  cnonce="f359fa6da445f39f"..
  Accept: text/html,application/xhtml+xml,application/xml;
  q=0.9,image/webp,*/*;q=0.8..
  Upgrade-Insecure-Requests: 1
  User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHT
  ML, like Gecko) Chrome/50.0.2661.102 Safari/537.36
  Accept-Encoding:  
  </code></pre>
  
  <p>I need a PHP method to authenticate to the inverter and do things, so I wrote this script, but doesn't work:</p>
  
  <pre><code>&lt;?php
  $username = 'customer';
  $password = '00000000';
  
  $method = 'GET';
  $url = "http://13.3.89.50/data/sys.txt";
  
  $ch = curl_init();
  curl_setopt($ch,CURLOPT_URL, $url);
  curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false);
  curl_setopt($ch,CURLOPT_SSL_VERIFYHOST, false);
  curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch,CURLOPT_FOLLOWLOCATION, false);
  curl_setopt($ch,CURLOPT_TIMEOUT, 30);
  curl_setopt($ch,CURLOPT_CONNECTTIMEOUT, 30);
  if($method == 'POST')
  {
  $fieldsData = http_build_query($fields);
  curl_setopt($ch,CURLOPT_POSTFIELDS, $fieldsData);
  }
  
  curl_setopt($ch, CURLOPT_HEADER, 1);
  $first_response = curl_exec($ch);
  $info = curl_getinfo($ch);
  
  preg_match('/WWW-Authenticate: Digest (.*)/', $first_response, $matches);
  
  if(!empty($matches))
  {
  $auth_header = $matches[1];
  $auth_header_array = explode(',', $auth_header);
  $parsed = array();
  
   foreach ($auth_header_array as $pair)
  {
    $vals = explode('=', $pair);
   $parsed[trim($vals[0])] = trim($vals[1], '" ');
  }
  
  $response_realm     = (isset($parsed['realm'])) ? $parsed['realm'] : "";
  $response_nonce     = (isset($parsed['nonce'])) ? $parsed['nonce'] : "";
  $response_opaque    = (isset($parsed['opaque'])) ? $parsed['opaque'] : "";
  
  $authenticate1 = md5($username.":".$response_realm.":".$password);
  $authenticate2 = md5($method.":".$url);
  
  $authenticate_response = md5($authenticate1.":".$response_nonce.":".$authenticate2);
  
  $request = sprintf('Authorization: Digest username="%s", realm="%s",  nonce="%s", opaque="%s", uri="%s", response="%s"',
  $username, $response_realm, $response_nonce, $response_opaque, $url, $authenticate_response);
  
  $request_header = array($request);
  
  $ch = curl_init();
  curl_setopt($ch,CURLOPT_URL, $url);
  curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false);
  curl_setopt($ch,CURLOPT_SSL_VERIFYHOST, false);
  curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch,CURLOPT_FOLLOWLOCATION, false);
  curl_setopt($ch,CURLOPT_TIMEOUT, 30);
  curl_setopt($ch,CURLOPT_CONNECTTIMEOUT, 30);
  
  if($method == 'POST')
  {
  $fieldsData = http_build_query($fields);
  curl_setopt($ch,CURLOPT_POSTFIELDS, $fieldsData);
  }
  curl_setopt($ch, CURLOPT_HTTPHEADER, $request_header);
  
  $result['response']         = curl_exec($ch);
  $result['info']             = curl_getinfo ($ch);
  $result['info']['errno']    = curl_errno($ch);
  $result['info']['errmsg']   = curl_error($ch);
  }
  $fp = @fopen("http://13.3.89.50/data/sys.txt", 'r');
  
  // Delete # &amp; filter
  
  $fp = explode('#', $input);
  
  $actualPower = filter_var($fp[1],FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
  $dailyPower = filter_var($fp[2],FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
  $totalPower = filter_var($fp[5],FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
  
  ########## PUSH DATA TO DOMOTICZ ########## */
  
  fopen ("http://13.3.89.3:8080/json.htm?type=command&amp;param=udevice&amp;idx=20&amp;nvalue=0&amp;svalue={$actualPower};{$dailyPower}");
  fopen ("http://13.3.89.3:8080/json.htm?type=command&amp;param=udevice&amp;idx=21&amp;svalue={$totalPower}");
  
  fclose($fp);
  ?&gt;
  </code></pre>
  
  <p>Where is the mistake?</p>