Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Web and WebWorks Development

Reply
New Contributor
Rodogd
Posts: 4
Registered: ‎03-06-2012
My Device: Curve 8520

Consuming a webservice?

Hello,

 

I just created my webservice with 1 query on a php file, here's the code:

 

 

<?PHP

/* require the user as the parameter */
if(isset($_GET['user']) && intval($_GET['user'])) {

  /* soak in the passed variable or set our own */
  $number_of_posts = isset($_GET['num']) ? intval($_GET['num']) : 10; //10 is the default
  $format = strtolower($_GET['format']) == 'json' ? 'json' : 'xml'; //xml is the default
  $user_id = intval($_GET['user']); //no default

  /* connect to the db */
  $link = mysql_connect('localhost','root','X') or die('Cannot connect to the DB');
  mysql_select_db('tfs',$link) or die('Cannot select the DB');

  /* grab the posts from the db */
  $query = "SELECT name FROM accounts WHERE id = $user_id  ORDER BY id DESC LIMIT $number_of_posts";
  $result = mysql_query($query,$link) or die('Errant query:  '.$query);

  /* create one master array of the records */
  $posts = array();
  if(mysql_num_rows($result)) {
    while($post = mysql_fetch_assoc($result)) {
      $posts[] = array('post'=>$post);
    }
  }

  /* output in necessary format */
  if($format == 'json') {
    header('Content-type: application/json');
    echo json_encode(array('posts'=>$posts));
  }
  else {
    header('Content-type: text/xml');
    echo '<posts>';
    foreach($posts as $index => $post) {
      if(is_array($post)) {
        foreach($post as $key => $value) {
          echo '<',$key,'>';
          if(is_array($value)) {
            foreach($value as $tag => $val) {
              echo '<',$tag,'>',htmlentities($val),'</',$tag,'>';
            }
          }
          echo '</',$key,'>';
        }
      }
    }
    echo '</posts>';
  }

  /* disconnect from the db */
  @mysql_close($link);
}
?>

 

 

 

So, I don't have idea how to retrieve this to my application:

http://anvard-online.com/query.php?user=1

 

I want that result of that query, but I don't know how to retrieve it.

Retired
astanley
Posts: 1,382
Registered: ‎07-02-2009
My Device: BlackBerry Bold 9900
My Carrier: Bell

Re: Consuming a webservice?

Use the XmlHttpRequest JavaScript object to make an HTTP GET call to your Web service URL. 

 

Here's an example: 

http://blackberry.github.com/WebWorks-Samples/kitchenSink/html/browser/xhr.html

Follow me on Twitter: @n_adam_stanley
-------------------------------------------------------------------------------------------------------------------------
Your app doesn't work? Use BlackBerry remote web inspector to find out why.