Jump to content

Best method of accomplishing this?


eyev

Recommended Posts

I will briefly explain the scenario I'm in.  I'm trying to work with the Ustream.tv API, which is decent at best, but it's much better than nothing.. Right now, I am attempting to write a script that will grab an XML file, and spit out results based on what it returns.  The script is generally going to display who's online and who's not out of select individuals(the ones who are on my site).

 

You can access multiple peoples XML files parallel, so say I have 5 users who i am tracking the streams of i would just call say: user1;user2;user3;user4;user5.xml - But this is where I am running into problems, since the script will be dynamic (users could be added on a daily basis to track), I am not sure how to store the usernames. 

  $dom->load("http://api.ustream.tv/ust_api.xml/channel/"_INSERT USERS HERE_"/getInfo?key=my api key");

My original idea was to store them in an array, however when spitting them out - they have brackets and such.  If I store them in a normal variable ie:

$users = "user1\;user2\;user3\;"

 

I won't know how to distinguish them when I get the XML file back...

 

 

the XML trees are setup like this:

 

<array>
<user1>
<result>
<username>
<status>
</result>
</user1>
<user2>
<result>
<username></username>
<status></status>
</result>
</user2>
etc.
</array>

 

 

Since the script is built to be dynamic I cant actually just hardcode user1, user2, etc. I need to find a way to make it dynamic and pick up the names from the variable then check array.user1.result.status - however, I'm at a loss on how to do this exactly..

 

 

I am sure this sounds very confusing because I am confusing myself just thinking/writing about it but I do appreciate any help offered!  I'm somewhat new to PHP - i use to be pretty active but it's been a few years (the military got me away from coding but now I am back as it passes time)...

 

 

Link to comment
Share on other sites

Yes that is rather confusing.  I'll attempt to offer a simpler explanation (Which you can correct if necessary)

 

You have a set of users.  You want to make a request using those usernames, and making the request is fine.  Then you will get an XML response back including data for each username, and you want to parse that data and use it.

 

Does that sound right?

 

Do you have an idea already of how you will parse the XML response or are you looking for a way to do that?

Link to comment
Share on other sites

here is how I am currently going at it...

 

  $users = "user1\;user2\;user3\;";

  $dom = new DOMDocument();
  $dom->load("http://api.ustream.tv/ust_api.xml/channel/".$users."/getInfo?key=myApiKey");
  foreach ($dom->getElementsByTagname('result') as $element) {
    $status = $element->getAttribute('status');
    foreach (($element->childNodes) as $e) {
      if (is_a($e, 'DOMElement')) {
        if ($e->tagName == 'title') {
          $titlez = htmlspecialchars($e->textContent);
        } elseif ($e->tagName == 'status') {
          $onornot = htmlspecialchars($e->textContent);
        }
      }
    }

  }

echo($onornot);

 

 

in the above example, it will grab the XML - and return with a table for user1,2,3 - the problem I am having is printing each ones results out because the usernames aren't static (they're dynamic)...

 

so i cant just hard code user1,2,3,4,etc. because when a user would sign up, they'd not be in the code.. I want a way for it to seperate each user inside the variable(much like an array), and have the option to parse the XML for each variable in the array.. However, I've had no luck with arrays because if i try to print them all out it will spit out:

Array ( [0] => Array ( [0] => user1 [1] => user2 [2] => user3 ) )

 

I'm just at a lost on how I can store these variables, easily call them and attach them to an XML request, and then use the variables to organize the XML document....

 

 

btw, the array code I used is:

 


$theVariable = array("users" =>
array (
    0=> "neggy\;",
    1=> "blindz0r\;",
    2=> "vixenita\;"),
);



print_r(array_values($theVariable));

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.