PHP从API响应中读取特定值

时间:2014-05-03 09:23:00

标签: php api sockets

我有这段代码:

<?php
#
# Sample Socket I/O to CGMiner API
#
function getsock($addr, $port)
{
 $socket = null;
 $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
 if ($socket === false || $socket === null)
 {
    $error = socket_strerror(socket_last_error());
    $msg = "socket create(TCP) failed";
    echo "ERR: $msg '$error'\n";
    return null;
 }

 $res = socket_connect($socket, $addr, $port);
 if ($res === false)
 {
    $error = socket_strerror(socket_last_error());
    $msg = "socket connect($addr,$port) failed";
    echo "ERR: $msg '$error'\n";
    socket_close($socket);
    return null;
 }
 return $socket;
}
#
# Slow ...
function readsockline($socket)
{
 $line = '';
 while (true)
 {
    $byte = socket_read($socket, 1);
    if ($byte === false || $byte === '')
        break;
    if ($byte === "\0")
        break;
    $line .= $byte;
 }
 return $line;
}
#
function request($cmd)
{
 $socket = getsock('127.0.0.1', 4028);
 if ($socket != null)
 {
    socket_write($socket, $cmd, strlen($cmd));
    $line = readsockline($socket);
    socket_close($socket);

    if (strlen($line) == 0)
    {
        echo "WARN: '$cmd' returned nothing\n";
        return $line;
    }

    print "$cmd returned '$line'\n";

    if (substr($line,0,1) == '{')
        return json_decode($line, true);

    $data = array();

    $objs = explode('|', $line);
    foreach ($objs as $obj)
    {
        if (strlen($obj) > 0)
        {
            $items = explode(',', $obj);
            $item = $items[0];
            $id = explode('=', $items[0], 2);
            if (count($id) == 1 or !ctype_digit($id[1]))
                $name = $id[0];
            else
                $name = $id[0].$id[1];

            if (strlen($name) == 0)
                $name = 'null';

            if (isset($data[$name]))
            {
                $num = 1;
                while (isset($data[$name.$num]))
                    $num++;
                $name .= $num;
            }

            $counter = 0;
            foreach ($items as $item)
            {
                $id = explode('=', $item, 2);
                if (count($id) == 2)
                    $data[$name][$id[0]] = $id[1];
                else
                    $data[$name][$counter] = $id[0];

                $counter++;
            }
        }
    }

    return $data;
 }

 return null;
}
#
if (isset($argv) and count($argv) > 1)
 $r = request($argv[1]);
else
 $r = request('summary');
#
echo print_r($r, true)."\n";
#
?>

输出此信息:

summary returned 'STATUS=S,When=1399108671,Code=11,Msg=Summary,Description=cgminer 4.3.0hf|SUMMARY,Elapsed=531,MHS av=453052.33,MHS 5s=537024.44,MHS 1m=458922.01,MHS 5m=375184.88,MHS 15m=201623.38,Found Blocks=0,Getworks=16,Accepted=518,Rejected=12,Hardware Errors=271,Utility=58.54,Discarded=276,Stale=0,Get Failures=0,Local Work=65806,Remote Failures=0,Network Blocks=1,Total MH=240524241.0000,Work Utility=5589.33,Difficulty Accepted=49008.00000000,Difficulty Rejected=448.00000000,Difficulty Stale=0.00000000,Best Share=93465,Device Hardware%=0.5450,Device Rejected%=0.9059,Pool Rejected%=0.9059,Pool Stale%=0.0000,Last getwork=1399108671|'
Array
(
    [STATUS] => Array
        (
            [STATUS] => S
            [When] => 1399108671
            [Code] => 11
            [Msg] => Summary
            [Description] => cgminer 4.3.0
        )

    [SUMMARY] => Array
        (
            [0] => SUMMARY
            [Elapsed] => 531
            [MHS av] => 453052.33
            [MHS 5s] => 537024.44
            [MHS 1m] => 458922.01
            [MHS 5m] => 375184.88
            [MHS 15m] => 201623.38
            [Found Blocks] => 0
            [Getworks] => 16
            [Accepted] => 518
            [Rejected] => 12
            [Hardware Errors] => 271
            [Utility] => 58.54
            [Discarded] => 276
            [Stale] => 0
            [Get Failures] => 0
            [Local Work] => 65806
            [Remote Failures] => 0
            [Network Blocks] => 1
            [Total MH] => 240524241.0000
            [Work Utility] => 5589.33
            [Difficulty Accepted] => 49008.00000000
            [Difficulty Rejected] => 448.00000000
            [Difficulty Stale] => 0.00000000
            [Best Share] => 93465
            [Device Hardware%] => 0.5450
            [Device Rejected%] => 0.9059
            [Pool Rejected%] => 0.9059
            [Pool Stale%] => 0.0000
            [Last getwork] => 1399108671
        )

)

如何获得特定值?例如,如何仅输出&#39; [MHS 15m]&#39;

1 个答案:

答案 0 :(得分:2)

如果$res是包含数组的变量,则可以将值设为

echo $res['SUMMARY']['MHS 15m'];