Advertisement
Booster

xmlrpc digest client request

Jun 3rd, 2016
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.95 KB | None | 0 0
  1. $post_data = array(
  2.           'username'=> $USER,
  3.           'password'=> $PASS
  4.     );
  5.  
  6.   $options = array(
  7.           CURLOPT_URL            => $urlCdr,
  8.           CURLOPT_HEADER         => true,    
  9.           CURLOPT_VERBOSE        => true,
  10.           CURLOPT_RETURNTRANSFER => true,
  11.           CURLOPT_FOLLOWLOCATION => true,
  12.           CURLOPT_SSL_VERIFYPEER => false,    // for https
  13.           // CURLOPT_USERPWD        => $USER . ":" . $PASS,
  14.           CURLOPT_HTTPAUTH       => CURLAUTH_DIGEST,
  15.           CURLOPT_POST           => true,
  16.           CURLOPT_POSTFIELDS     => http_build_query($post_data)
  17.   );
  18.  
  19.   $ch = curl_init();
  20.  
  21.   curl_setopt_array( $ch, $options );
  22.  
  23.   try {
  24.     $raw_response  = curl_exec( $ch );
  25.  
  26.   } catch(Exception $ex) {
  27.       if ($ch != null) curl_close($ch);
  28.       throw new Exception($ex);
  29.   }
  30.  
  31.   if ($ch != null) curl_close($ch);
  32.  
  33.   $header = explode("\n", $raw_response)[3];
  34.   $re = "/([a-z]+)=\"(.*?)\"/";  
  35.   preg_match_all($re, $header, $matches);
  36.   $cnonce = time();
  37.   $credentials[] = 'username="' . $USER . '"';
  38.   $credentials[] = 'digest-uri="' . $urlCdr . '"';
  39.   $credentials[] = 'cnonce="' . $cnonce . '"';
  40.   $credentials[] = 'nc-value="1"';
  41.   $credentials[] = 'responce="'. md5($USER . ':1' . ':' . $cnonce) .'"';
  42.   foreach ($matches[1] as $k => $v) {
  43.     $credentials[] = $v . '="' . $matches[2][$k] . '"';
  44.   }
  45.   $header = "Authorization: Digest " . implode(",\n", $credentials);
  46.  
  47.     $request = xmlrpc_encode_request("getAccountCDRs", array(
  48.         'username'=> $USER,
  49.         'password'=> $PASS
  50.         ));
  51.     $context = stream_context_create(array('http' => array(
  52.         'header' => "Content-type: text/xml\n" . $header,
  53.         'content' => $request
  54.     )));
  55.     // $file = file_get_contents($urlCdr, false, $context);
  56.     $response = xmlrpc_decode($file);
  57.     if ($response && xmlrpc_is_fault($response)) {
  58.         trigger_error("xmlrpc: $response[faultString] ($response[faultCode])");
  59.     } else {
  60.         print_r($response);
  61.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement