Question

I have a Datasnap Server and a have a method named :

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

I did small php code below to invoke this method.

<?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>';

?>

I got this error message from browser (Firefox or IE) :

Warning: file(http://192.168.1.101:8088/datasnap/rest/TServerMethodsMain/AddCity/{"id":1,"description":"MY CITY","uf":"XX"}) [function.file]: failed to open stream: HTTP request failed! HTTP/1.1 500 Internal Server Error in C:\xampp\htdocs\json-php\index.php on line 19

Well, I have others methods that works fine with this php code, but only if I pass as parametrer Primitive Types : String, Integer...

I did a debug and see that problem happens at moment I need to convert parameter JSONObject into Object ( unMarshalll ). When I invoke this method by Client Delphi Win32 it works fine !

Does anybody knows anything about the problem ?

Thanks !

Was it helpful?

OTHER TIPS

Your PHP code issue a GET request to the datasnap server. For complex parameters like JSONObject you need to use POST or PUT HTTP verb with proper JSONObject as message body. http://docwiki.embarcadero.com/RADStudio/en/DataSnap_REST_Messaging_Protocol#Parameters_in_the_URL

So, you need to send a POST request to the datasnap server. Check the documentations for further details. http://docwiki.embarcadero.com/RADStudio/en/DataSnap_REST_Messaging_Protocol

This PHP Code works with 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);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top