2014-10-27 58 views
0

我正在嘗試對SMA Datalogger執行HTTP POST請求,該請求使用JSON-RPC來響應HTTP請求。http共享主機POST請求

使用hurl.it我可以做一個成功的請求,例如:

目的地:POST,http://aaa.no-ip.org:101/rpc,執行重定向:對。
標題:Host:aaa.no-ip.org:101,Content-Type:text/plain。
身體:RPC = { 「PROC」: 「GetPlantOverview」, 「格式」: 「JSON」, 「版本」: 「1.0」, 「ID」 爲 「1」}

然後投。它進程的以下要求:

Success 
POST http://aaa.no-ip.org:101/rpc 
200 OK  401 bytes  3.76 secs 

    HEADERS 

    Accept: */* 
    Accept-Encoding: application/json 
    Content-Length: 122 
    Content-Type: text/plain 
    Host: aaa.no-ip.org 
    User-Agent: runscope/0.1 

    BODY 

    RPC=%7B%22proc%22%3A%22GetPlantOverview%22%2C%22format%22%3A%22JSON%22%2C%22version%22%3A%221.0%22%2C%22id%22%3A%221%22%7D 

和響應是:

HEADERS 

Cache-Control: no-store, no-cache, max-age=0 
Connection: keep-alive 
Content-Length: 401 
Content-Type: text/html 
Date: Wed, 22 Oct 2014 14:15:50 GMT 
Keep-Alive: 300 
Pragma: no-cache 
Server: Sunny WebBox 

BODY 

{"format":"JSON","result":{"overview":[{"unit":"W","meta":"GriPwr","name":"GriPwr","value":"99527"},{"unit":"kWh","meta":"GriEgyTdy","name":"GriEgyTdy","value":"842.849"},{"unit":"kWh","meta":"GriEgyTot","name":"GriEgyTot","value":"2851960.438"},{"unit":"","meta":"OpStt","name":"OpStt","value":""},{"unit":"","meta":"Msg","name":"Msg","value":""}]},"proc":"GetPlantOverview","version":"1.0","id":"1"} 

我的問題是,每次我試圖複製這些請求我總是得到:

string(0) "" 

這可能是因爲我在使用共享主機。我試過cURL,普通的PHP(socket和file_get_contents,甚至jQuery) 有人可以請提供一個例子來說明如何做這個請求嗎? jquery或php,我什至不在乎,我一直在嘗試2周,和這麼多的嘗試,無論是我得到的代碼錯誤或只是string(0)""

PS:以前嘗試的例子,參見:https://stackoverflow.com/questions/26408153/solar-energy-monitoring-sma-webbox-json-post-request

回答

0

這裏是JSONRPC客戶我已經成功地用於製造JSON RPC HTTP請求。希望這有助於: 您可能需要更改一些代碼才能工作。

找到在這裏:https://code.google.com/p/pmvc-framework/source/browse/trunk/src/main/php/pmvc/remoting/jsonrpc/JsonRpcClient.class.php?r=328

<?php 

use Exception; 
use ReflectionClass; 

/* 
* A client for accessing JSON-RPC over HTTP servers. 
* 
*/ 
class JsonRpcClient 
{ 

    private $_url    = false; 
    private $_reuseConnections = true; 

    private static $_curl = null; 
    private $_nextId = 99; 

    /* 
    * Creates the client for the given URL. 
    * @param string $_url 
    */ 
    public function __construct($url) 
{ 
    $this->_url = $url; 

} 
    /* 
    * Returns a curl resource. 
    * @return resource 
    */ 
    private function getCurl() 
    { 
     if (!isset(self::$_curl)) { 

     // initialize 
     self::$_curl = curl_init(); 

     // set options 
     curl_setopt(self::$_curl, CURLOPT_FAILONERROR,  true); 
     curl_setopt(self::$_curl, CURLOPT_FOLLOWLOCATION, true); 
     curl_setopt(self::$_curl, CURLOPT_FORBID_REUSE,  $this->_reuseConnections===false); 
     curl_setopt(self::$_curl, CURLOPT_FRESH_CONNECT, $this->_reuseConnections===false); 
     curl_setopt(self::$_curl, CURLOPT_CONNECTTIMEOUT, 5); 

     return self::$_curl; 
     } 
    } 

    /* 
    * Invokes the given method with the given arguments 
    * on the server and returns it's value. If {@code $returnType} 
    * is specified than an instance of the class that it names 
    * will be created passing the json object (stdClass) to it's 
    * constructor. 
    * 
    * @param string $method the method to invoke 
    * @param Array $params the parameters (if any) to the method 
    * @param string $id the request id 
    * @param Array $headers any additional headers to add to the request 
    */ 
    public function invoke($method, Array $params=Array(), $id=false, Array $headers=Array()) 
    { 
    // get curl 
    $curl = $this->getCurl(); 

    // set url 
    curl_setopt($curl, CURLOPT_URL, $this->_url); 

    // set post body 
    $request = json_encode(
     Array(
      'jsonrpc' => '2.0', 
      'method' => $method, 
      'params' => $params, 
      'id'  => ($id===false) ? ++$this->nextId : $id 
     ) 
    ); 

    curl_setopt($curl, CURLOPT_POST, true); 
    curl_setopt($curl, CURLOPT_POSTFIELDS, $request); 
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 

    // set headers 
    $curlHeaders = Array(); 
    $curlHeaders []= "Content-type: application/json-rpc"; 
    for (reset($headers); list($key, $value)=each($headers);) { 
    $curlHeaders []= $key.": ".$value."\n"; 

    } 
    curl_setopt($curl, CURLOPT_HTTPHEADER, $curlHeaders); 

    // post the data 
    $response = curl_exec($curl); 
    if (!$response) { 
     throw new Exception('cURL error '.curl_error($curl).' while making request to '.$this->_url); 

    } 

    // decode json response 
    $response = json_decode($response); 
    if ($response==NULL || curl_error($curl)!=0) { 
     throw new Exception("JSON parsing error occured: ".json_last_error()); 

     // throw errors 
     } else if (isset($response->error)) { 
      $msg = 'JSON-RPC error'; 
      if (isset($response->error->message) && !empty($response->error->message)) { 
       $msg .= ': "' . $response->error->message . '"'; 
      } 
      $msg .= "\n"; 
      $msg .= 'URL: ' . $this->_url; 
      $msg .= "\n"; 
      $msg .= 'Method: ' . $method; 
      $msg .= "\n"; 
      $msg .= 'Arguments: ' . self::printArguments($params, 2); 

      if (isset($response->error->code)) { 
       throw new Exception($msg, intval($response->error->code)); 
      } else { 
       throw new Exception($msg); 
      } 

     } 
     // get the headers returns (APPSVR, JSESSIONID) 
     $responsePlusHeaders = Array(); 
     $responsePlusHeaders['result'] = $response->result; 
     $responsePlusHeaders['headers'] = curl_getinfo($curl); 
     // return the data 
     return $responsePlusHeaders; 
    } 

    /* 
    * Printing arguments. 
    * @param $arg 
    * @param $depth 
    */ 
    private static function printArguments($args, $depth=1) 
    { 
     $argStrings = Array(); 
     foreach ($args as $arg) { 
     $argStrings[] = self::printArgument($arg, $depth); 
     } 
     return implode($argStrings, ', '); 
    } 

    /* 
    * Print an argument. 
    * @param $arg 
    * @param $depth 
    */ 
    private static function printArgument($arg, $depth=1) 
    { 
     if ($arg === NULL) { 
     return 'NULL'; 

     } else if (is_array($arg)) { 
      if ($depth > 1) { 
       return '[' . self::printArguments($arg, ($depth - 1)) . ']'; 
      } else { 
       return 'Array'; 
      } 
     } else if (is_object($arg)) { 
      return 'Object'; 
     } else if (is_bool($arg)) { 
      return ($arg === TRUE) ? 'true' : 'false'; 
     } else if (is_string($arg)) { 
      return "'$arg'"; 
     } 
     return strval($arg); 
    } 
} 

用途將被:

include JsonRpcClient.php 


$client = new JsonRpcClient('http://aaa.no-ip.org:101/rpc'); 
$response = $client->invoke('GetPlantOverview', 1, array('Host: aaa.no-ip.org:101'));