开发者

How do I send a DateTime or some sort of timestamp from PHP to Java using Hessian binary remoting protocol?

There开发者_JS百科 is a Java service that I need to invoke from PHP that looks something like this:

getProductsForMode(java.util.Calendar date, int modeId)

I'm having trouble mapping my PHP date class to the java.util.Calendar class. I've tried a couple different approaches, all to no avail:

Approach #1 (wrapping DateTime object in a CalendarHandle that gets mapped to Java's CalendarHandle):

class CalendarHandle
{
  public $type;
  public $date;

  function __construct($date=null)
  {
    $this->date = $date;
  }
}

$this->hessianClient = new HessianClient(_HESSIAN_ENDPOINT, $hessianOptions);
$config = &HessianConfig::globalConfig();
$config->typeMap->mapRemoteType('com.caucho.hessian.io.CalendarHandle', 'CalendarHandle');

$time = new CalendarHandle(new DateTime($dateEvaluated));
$products = $this->hessianClient->getProductsForMode($time, 1234);

Error:

[06-Jul-2011 11:16:52] PHP Fatal error: Uncaught exception 'HttpError' with message 'HttpError: Malformed HTTP header' in /Users/jordanb/Sites/proposal-framework/trunk/plugins/srHessianClientPlugin/hessianphp/Http.php:265

Approach #2: Mapping DateTime to java.util.Calendar

$this->hessianClient = new HessianClient(_HESSIAN_ENDPOINT, $hessianOptions);
$config = &HessianConfig::globalConfig();
$config->typeMap->mapRemoteType('java.util.Calendar', 'DateTime');

$time = new DateTime($dateEvaluated);
$products = $this->hessianClient->getProductsForMode($time, 1234);

Error:

[06-Jul-2011 11:28:50] PHP Fatal error: Uncaught exception 'HessianError' with message 'Hessian Parser, Malformed reply: expected r' in /Users/jordanb/Sites/proposal-framework/trunk/plugins/srHessianClientPlugin/hessianphp/Protocol.php:350

Has anyone had success sending a DateTime or some sort of timestamp from PHP to Java using Hessian? Thanks in advance!


Would it be practical to just use the php time() function (which returns an int and send this as a long to Java? In Java you could just pass the long to new Date(long Date).


Use unix timestamp. That's the best bet.

PHP:

$ts = time();

Pass $ts to java in someway.

Java:

long ts = getTimeFromPhp();
Date dt = new Date(ts); 

Or you can use Calendar:

Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(ts);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜