Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 12 of 12
  1. #1
    Regular Coder
    Join Date
    Mar 2009
    Posts
    175
    Thanks
    3
    Thanked 1 Time in 1 Post

    Need some advice on soap object arrays

    I've never needed to work much with soap until now but have a project I need to build a webservice client on.

    The thing I'm struggling with is in outputting an array of objects, I'm able to var_dump the functions and see what's there and can run a check on the apikey I need to use and return true, I just can't get this array to pop.

    Here's an example of what I'm playing with here:
    Code:
    $apikey = "mykey";
    $soap = new SoapClient("pathto.wsdl", array("trace" => 1, "exception" => 0));
    $header = new SoapHeader('url', 'apiKey', $apikey);
    $soap->__setSoapHeaders($header);
    $checkkey = $soap->checkKey($apikey);
    if($checkkey == 0) {
     echo "Invalid API Key!";
    } else {
     var_dump($soap->__getFunctions());
     echo "<br /><br />";
    
     // GET GAMES
     $array['id'] = 'id';
     $array['title'] = 'title';
     $array['status'] = 'status';
     $array['votes'] = 'votes';
    
     $games = $soap->getGames($apikey, $array);
    
     $id = $games->id;
     $title = $games->title;
     $status = $games->status;
     $votes = $games->votes;
    
     echo $games;
    
     foreach($games as $key => $value) {
      echo "".$key." is ".$value."<br />";
     }
     foreach($array as $key => $value) {
      echo "".$key." is ".$value."<br />";
     }
     foreach($games as $game) {
      echo $game;
     }
    I can get it to output 'array' but when I try to actually get values I can't get anything. Any advice on this?

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Are you referring to $games? Var_dump($games) to see what exactly it is and what its structure is.

  • #3
    Regular Coder
    Join Date
    Mar 2009
    Posts
    175
    Thanks
    3
    Thanked 1 Time in 1 Post
    If I var_dump($soap->__getFunctions()) I get:
    Code:
    array(6) { [0]=> string(32) "boolean checkKey(string $apiKey)" [1]=> string(30) "array getGames(string $apiKey)" [2]=> string(34) "boolean clearGames(string $apiKey)" [3]=> string(40) "boolean addVote(string $apiKey, int $id)" [4]=> string(46) "boolean addGame(string $apiKey, string $title)" [5]=> string(41) "boolean setGotit(string $apiKey, int $id)" }
    If I var_dump($games) I get array(0) { } Array.
    If I leave the $apikey out of the input for getGames() it will return false.
    I'm sure I'm just not setting the input up properly in getGames, I've tried several different ways but nothing I've tried seems to work. In my mind I should be able to pass the apikey and the array of keys into the function and pop out a result array but it's clearly not the case.

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    getGames only accepts a single parameter from the looks of it. Give it just the API key, and check what's in the var_dump($games) again.
    I think you may be merging together the concepts of the __call and __soapCall together (although functionally the same). Since you are invoking the __call on the $soap->getGames(), you don't need to provide it with an argument to populate results into.
    I typically use __soapCall since the __call is actually deprecated on the SoapClient class.

  • #5
    Regular Coder
    Join Date
    Mar 2009
    Posts
    175
    Thanks
    3
    Thanked 1 Time in 1 Post
    Thanks for the help Fou-Lou, it's much appreciated.
    This is the thing that's throwing me, even setting this up like
    Code:
     $games = $soap->getGames($apikey);
    Var_dump($games);
    which is how it seems like it should be to me as well, returns array(0) { } Array.

    There has to be something I'm missing here.
    getGames is supposed to return an array of objects, each object contains an id, title, status, and votes.
    I just can't figure out why I get an empty array here, could it be that they just don't have any objects set in the soap server for this?
    Last edited by sfraise; 04-18-2012 at 11:01 PM.

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Looks to me that is correct. The results of what it gave you are an array, and if it failed to lookup properly then that would be a scalar value of either false or null. So yeah it looks to me that they just don't have any results for you.

    I'd write this in a different language first to verify the results of getGames with that $apikey. C# is a good choice for writing a soap client. PHP is temperamental at best with regards to soap I find.

  • #7
    Regular Coder
    Join Date
    Mar 2009
    Posts
    175
    Thanks
    3
    Thanked 1 Time in 1 Post
    Thanks again for your help Fou-Lou, turns out that was the issue, there just wasn't any info there to populate the array yet.

    I must have misunderstood the premise on this, I thought there was already a set of data there for me to pull but I had to set up the function to add data and then add some in myself. Should be pretty smooth from here on out.

    The only other thing I have to do on this is figure out how to limit the ability for someone to vote to once per day, there's no login or anything so I'll have to do it through a cookie or something which I hate. This method always has the inherent flaw that someone can wipe their cookies and remove the limit. I thought about doing it by IP by storing to a database and checking against that but it wouldn't work for multiple people on a lan. Any better ideas on how to do this without relying on a cookie? I'm sure it's fine for what I'm doing and it's already stated as acceptable but it just seems like such a low-rent method.

  • #8
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    You could add an IP check and store that on your site first. Still no guarantee at all, but it's not as simple as just deleting the cookies. The downside of course is that there is a possibility that someone who wants to vote cannot since that IP already voted that day.
    You could create a login system of your own if you wanted. I personally wouldn't put the effort in if its nothing that's really critical and more for fun. A cookie IMO would suffice, I wouldn't expect someone to endlessly vote on something, but you never know. Login system is really your only available control.

  • #9
    Regular Coder
    Join Date
    Mar 2009
    Posts
    175
    Thanks
    3
    Thanked 1 Time in 1 Post
    Thanks again Fou Lu, I just went ahead and used a cookie.
    I've got this thing all finished except for one last issue I'm trying to solve.
    I need to sort my results by number of votes which should be an easy task using sort(), but it seems it's not that simple here. I need to make this code short and sweet so I really don't want to go about setting up a complicated function to try and sort these results when it seems that there should be a simple way of going about it. Let me show you my code and see if you have any ideas on how to sort this out simply:
    Code:
     // CHECK IF API KEY IS VALID
     $checkkey = $soap->checkKey($apikey);
     // IF API NOT VALID RETURN ERROR
     if($checkkey == 0) {
      echo "Invalid API Key!";
     // IF API IS VALID SHOW CONTENT
     } else {
      echo "<div class=\"listtop\"><div class=\"gameidtop\">ID</div><div class=\"gametitletop\">Title</div><div class=\"gamevotestop\">Votes</div><div class=\"gamestatustop\">Status</div><div style=\"clear:both;\"></div></div>";
      echo "<div id=\"ajaxdiv\">";
      // GET GAMES
      $games = $soap->getGames($apikey);
      // COUNT NUMBER OF GAMES
      $count = count($games);
    
      echo "<div class=\"ownedgames\">";
      // LOOP THROUGH GAMES
      for($i = 0; $i < $count; $i++){ 
       // LOOP THROUGH GAME VALUES
       foreach($games["$i"] as $key => $value) {
        // GET OWNED GAMES
        if(($key == 'id') && ($games["$i"]->status == 'gotit')) {
         echo "<div class=\"gamewrapper\"><div class=\"gameid\">".$value."</div>";
        }
        if(($key == 'title') && ($games["$i"]->status == 'gotit')) {
         echo "<div class=\"gametitle\">".$value."</div>";
        }
        if(($key == 'votes') && ($games["$i"]->status == 'gotit')) {
         echo "<div id=\"".$games["$i"]->id."_vote\" class=\"gamevotes\">-</div>";
        }
        if(($key == 'status') && ($games["$i"]->status == 'gotit')) {
         echo "<div id=\"".$games["$i"]->id."_status\" class=\"gamestatus\">".$value."</div><div style=\"clear:both;\"></div></div>";
        }
       }
      }
      echo "</div>";
    
      echo "<div class=\"wantedgames\">";
      // LOOP THROUGH GAMES
      for($i = 0; $i < $count; $i++){ 
       // LOOP THROUGH GAME VALUES
       foreach($games["$i"] as $key => $value) {
        // GET WANTED GAMES
        if(($key == 'id') && ($games["$i"]->status == 'wantit')) {
         echo "<div class=\"gamewrapper\"><div class=\"gameid\">".$value."</div>";
        }
        if(($key == 'title') && ($games["$i"]->status == 'wantit')) {
         echo "<div class=\"gametitle\">".$value."</div>";
        }
        if(($key == 'votes') && ($games["$i"]->status == 'wantit')) {
         echo "<div id=\"".$games["$i"]->id."_vote\" class=\"gamevotes\">".$value."</div>";
        }
        if(($key == 'status') && ($games["$i"]->status == 'wantit')) {
         echo "<div id=\"".$games["$i"]->id."_status\" class=\"gamestatus\">".$value."</div><div class=\"buttons\"><img class=\"votebutton\" src=\"/xbox/images/votebutton.png\" onclick=\"$('#selectedvoteid').val('".$games["$i"]->id."');addVote();setCookie();return false;\" alt=\"Vote\" title=\"Vote\" /><img class=\"addbutton\" src=\"/xbox/images/addbutton.png\" onclick=\"$('#selectedstatusid').val('".$games["$i"]->id."');haveIt();return false;\" alt=\"Have It\" title=\"Have It\" /></div><div style=\"clear:both;\"></div></div>";
        }
       }
      }
      echo "</div></div>";
     }
    The first loop of owned games is fine, but the second loop I need to sort by number of votes, as it is by default it's sorted by id. If it were me that set up this soap server I would have added an input to add to the function to sort by specific key but alas I did not set up the soap server and that option does not exist lol.

    Can you think of a simple way to do this or am I forced to set up a function to do this? Actually now that I think of it, I can't get sort() to work on this at all so I'm not ever exactly sure how I would even set up a function to do it. Maybe I'm just over thinking it and overlooking something simple.

  • #10
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    I'd use a usort, only because I hate multisort and like the control I get from a comparator.
    PHP Code:
    function sortByVotesASC($a$b)
    {
        
    $iResult 0;
        if (isset(
    $a['votes'], $b['votes']))
        {
            
    $iResult $a['votes'] - $b['votes'];
        }
        return 
    $iResult;
    }

    usort($array'sortByVotestASC'); 
    It will sort the $array as a reference, so if you don't want the original sorted then make a copy of it and use that to sort and iterate.

  • #11
    Regular Coder
    Join Date
    Mar 2009
    Posts
    175
    Thanks
    3
    Thanked 1 Time in 1 Post
    This is where I'm getting stuck, I can't just do the sort on $games.
    I had thought that I could, but I'm doing the for loop around the foreach loop which is throwing it off I assume. What i really need to do is get $i to jive with the order of the votes. In my mind I should have been able to just do usort($games, 'sortByVotestASC'); before the for loop and it would be sorted, but it's not.
    Code:
      // GET GAMES
      $games = $soap->getGames($apikey);
      // FUNCTION TO SORT ARRAY
      function sortByVotesASC($a, $b) {
        $iResult = 0;
        if (isset($a['votes'], $b['votes'])) {
            $iResult = $a['votes'] - $b['votes'];
        }
        return $iResult;
      }
      usort($games, 'sortByVotestASC');
    // LOOP THROUGH GAMES
    for($i = 0; $i < $count; $i++){ 
       // LOOP THROUGH GAME VALUES
       foreach($games["$i"] as $key => $value) {
    ***** EDIT *****
    This seems to do the trick...
    Code:
    function cmp( $a, $b )
    { 
      if(  $a->votes ==  $b->votes ){ return 0 ; } 
      return ($a->votes < $b->votes) ? -1 : 1;
    } 
    
    usort($games,'cmp');
    Last edited by sfraise; 04-20-2012 at 06:22 PM.

  • #12
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Votes is a property. Change the sorter to use $a->votes and $b->votes instead of $a['votes'] and $b['votes'].


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •