1

我有一个 Datasnap 服务器和一个名为:

function TServerMethodsMain.AddCity( ACity : TJSONObject ) : Boolean ;

我在下面做了小的 php 代码来调用这个方法。

<?php

class city
{
   public $id;
   public $description;
   public $uf;          
}

$objcity = new city ;

$objcity -> id          = 1         ;
$objcity -> description = 'MY CITY' ;
$objcity -> uf          = 'XX'      ;

$url  = 'http://192.168.1.101:8088/datasnap/rest/TServerMethodsMain/AddCity/' ;
$url .= json_encode( $objcity ) ;

$page = file($url) ;  

$show = json_decode($page[0]);

echo '<pre>';

print_r ($show);

echo '</pre>';

?>

我从浏览器(Firefox 或 IE)收到此错误消息:

警告:文件(http://192.168.1.101:8088/datasnap/rest/TServerMethodsMain/AddCity/{"id":1,"description":"MY CITY","uf":"XX"}) [function.文件]:打开流失败:HTTP 请求失败!第 19 行 C:\xampp\htdocs\json-php\index.php 中的 HTTP/1.1 500 内部服务器错误

好吧,我有其他方法可以很好地处理这个 php 代码,但前提是我作为参数传递原始类型:字符串、整数......

我进行了调试,发现问题发生在我需要将参数JSONObject转换为Object ( unMarshallll ) 的时候。当我通过客户端 Delphi Win32 调用此方法时,它工作正常!

有人知道这个问题吗?

谢谢 !

4

3 回答 3

1

您的 PHP 代码向 datasnap 服务器发出 GET 请求。对于 JSONObject 之类的复杂参数,您需要使用 POST 或 PUT HTTP 动词以及适当的 JSONObject 作为消息体。 http://docwiki.embarcadero.com/RADStudio/en/DataSnap_REST_Messaging_Protocol#Parameters_in_the_URL

因此,您需要向 datasnap 服务器发送 POST 请求。检查文档以获取更多详细信息。 http://docwiki.embarcadero.com/RADStudio/en/DataSnap_REST_Messaging_Protocol

于 2011-01-29T22:37:19.323 回答
1

PHP 代码适用于DataSnap - Delphi XE2

$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Accept: application/json", "Content-Type: text/xml; charset=utf-8"));
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, 'user:pass');
curl_setopt($ch, CURLOPT_URL, $param_url);
$response = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);
于 2012-12-31T17:59:29.560 回答
0

按照下面的链接和我的答案

http://docwiki.embarcadero.com/RADStudio/en/Talk:DataSnap_REST_Messaging_Protocol

于 2011-02-02T16:35:32.713 回答