-1

私はできる限りこれを説明しようとしていますが、何か不明な点がある場合は質問してください。センサーに関する多くの情報を取得する API を使用しています。

これはクラス内のメソッドです

public function getSensors()
 {
     $params = array();
     return json_decode($this->consumer->sendRequest(constant('REQUEST_URI').'/sensors/list', $params, 'GET')->getBody());

そして私のindex.phpで

$params = array('id'=> XXXXXX); 
$response = $consumer->sendRequest(constant('REQUEST_URI').'/sensor/info', $params,'GET');

echo json_decode($response->getBody());

これにより、次のような情報の塊が得られます。

{"id":"xxxxx","client":"xxxx","name":"xxxxx","lastUpdated": xxxx}

この情報の一部のみを使用したい。

これは getBody() メソッドです。

public function getBody() {
    if (self::METHOD_POST == $this->method && (!empty($this->postParams) || !empty($this->uploads))) {

        if (0 === strpos($this->headers['content-type'], 'application/x-www-form-urlencoded')) {

            $body = http_build_query($this->postParams, '', '&');

            if (!$this->getConfig('use_brackets')) {

                $body = preg_replace('/%5B\d+%5D=/', '=', $body);

            }

            // support RFC 3986 by not encoding '~' symbol (request #15368)

            return str_replace('%7E', '~', $body);



        } elseif (0 === strpos($this->headers['content-type'], 'multipart/form-data')) {

            require_once 'HTTP/Request2/MultipartBody.php';

            return new HTTP_Request2_MultipartBody(

                $this->postParams, $this->uploads, $this->getConfig('use_brackets')

            );

        }

    }

    return $this->body;

}
4

1 に答える 1

0

API を使用している場合、API でメソッドが定義されていない限り、取得する情報を制御することはできません。センサーを読み取っている場合、それが選択肢になる可能性はほとんどありません。

それでも、必要のないものは無視できます。

例えば:

$myArray = json_decode('{"id":"xxxxx","client":"xxxx","name":"xxxxx","lastUpdated": xxxx}');
$mySubset = array($myArray['id'], $myArray['lastUpdated']);

json_decodeポイントを説明するために使用します。あなたのコメントから、配列を返しているように見えますgetBody()が、報告したエラーメッセージはオブジェクトを指しています。

使用できる配列の場合

$myResp = $response->getBody();
$myId = $myResp['id'];

使用できるオブジェクトについて

$myResp = $response->getBody();
$myId = $myResp->id;

的を射ていない場合は申し訳ありません。ここでは少しブラインドで発砲しています。

于 2013-08-05T21:38:57.390 に答える