Quantcast
Channel: PHP Website Development » WSDL
Viewing all articles
Browse latest Browse all 11

Returning a Dictionary object with PHP & SOAP

$
0
0

I’m writing a SOAP API in PHP and I’m stuck. I’m trying to return a Dictionary object with this format:
Key: #
Value:
Id: #
Title: some title
Text: blahI’ve been looking through the WSDL and XSD files of another site’s SOAP API service and I’ve figured out how to write a Dictionary object into them, now I’m stuck at the PHP part. My Dictionary is set up in the WSDL / XSD to have an integer as the key and a Comment object as the value, so it should be an array of int key / Comment value pairs…but I just can’t figure out how to do it in PHP. Here is the structure I’ve got in my XSD file:



true











The Comment object just contains a few pieces of information, like an id, the poster id and name, text, date, etc. I want the Dictionary key to be the Comment id, and the value to be the Comment object itself.
Can someone help me out with this, just help me wrap my head around how this should work in PHP. Thanks!
…………………………………..

Well, since I had nothing better to do, I kept tinkering with it and figured it out. I had most of the right elements, just in the wrong order.
My WSDL stayed the same, I didn’t have to change anything there. In my first XSD, I changed my output element to this:






Basically I’m returning a DictionaryResponse object named GetDictionaryResult from the second XSD, the DictionaryResponse is wrapped in a GetDictionaryResponse object. My second XSD was where I was going wrong, SOAP is confusing, but I ended up with these elements:









true













The DictionaryResponse object contains a string called “status”, which I only put there as a test. It also contains an unbounded ArrayOfKeyValueOfintComment objects, which is where the magic happens. Finally, the PHP itself:
class GetDictionaryResponse {
var $GetDictionaryResult;

function GetDictionaryResponse() {
$this->GetDictionaryResult = new GetDictionaryResult();
}
}

class GetDictionaryResult {
var $status;
var $Data;

function GetDictionaryResult() {
$this->status = (string)”Ok”;
}

function AddItem($k, $v) {
$d = new ArrayOfKeyValueOfintComment($k, $v);
$this->Data[] = $d;
}
}

class ArrayOfKeyValueOfintComment {
var $Key, $Value;

function ArrayOfKeyValueOfintComment($k, $v) {
$this->Key = $k;
$this->Value = $v;
}
}

function GetDictionary() {
$ret = new GetDictionaryResponse();
for($i = 0; $i < 3; $i++) {
$c = new Comment(array([comment elements]));
$ret->GetDictionaryResult->AddItem($i, $c);
}

return $ret;
}Hopefully it doesn’t look too confusing. Essentially the return looks like this:
GetDictionaryResponse->
GetDictionaryResult->
string status;
Dictionary Data;
I can consume it in .Net and get a valid Dictionary from it like this:
DictionaryResponse r = client.GetDictionary(); Comment c = r.Data[0];
Hopefully I’ve helped some future readers out!


Viewing all articles
Browse latest Browse all 11

Trending Articles