2016-05-19 47 views
0

我必須向逆變器Web服務器進行身份驗證,讀取txt文件,將信息推送到另一臺服務器,關閉並在1分鐘後重復操作。這是我連續認證後嗅出信息:我需要一個PHP方法來驗證逆變器和做事Digest驗證PHP

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: 

,所以我寫了這個劇本,但不工作:

<?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 # & 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&param=udevice&idx=20&nvalue=0&svalue={$actualPower};{$dailyPower}"); 
fopen ("http://13.3.89.3:8080/json.htm?type=command&param=udevice&idx=21&svalue={$totalPower}"); 

fclose($fp); 
?> 

錯誤在哪裏?

回答

0

這就像一個魅力:)

<?php 
$login = 'customer'; 
$password = '00000000'; 
$url = 'http://13.3.89.50/data/sys.txt'; 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL,$url); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); 
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY); 
curl_setopt($ch, CURLOPT_USERPWD, "$login:$password"); 
$result = curl_exec($ch); 
curl_close($ch); 

$results = explode('#', $result); 

$actualPower = filter_var($results[1],FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION); 
$dailyPower = filter_var($results[2],FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION); 
$totalPower = filter_var($results[5],FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION); 
$actualKPower= $actualPower * 1000; 

    ########## PUSH DATA TO DOMOTICZ ########## */ 

file_get_contents("http://13.3.89.3:8080/json.htm?type=command&param=udevice&idx=20&nvalue=0&svalue={$actualKPower};{$totalPower}"); 
file_get_contents("http://13.3.89.3:8080/json.htm?type=command&param=udevice&idx=21&svalue={$totalPower}"); 

?>