0

I have a client who needs to be able to hit a script on my server, provide the script with an ID number, and then receive back an XML string with information about that ID. For example, he would send a request to "www.mywebsite.com?uid=424" and receive back an xml string that may look something like this:

<client>
    <clientID>424</clientID>
    <clientName>ABC123</clientName>
    <contact>Joe Schmoe</contact>
</client>

I've never done this before, so i was wondering if it's as simple as getting the ID number, running a query to get all of the information and then printing the xml data, or if there is some sort of data-returning function for things like this. I've tried to google it, but wasn't able to find anything... probably because I'm not searching for the right stuff. Any help would be greatly appreciated.

3 Answers 3

3

Get the url parameters with $_GET:

<?php
$id = $_GET['uid'];
?>
<client>
    <clientID><?php echo $id; ?></clientID>
    <clientName>ABC123</clientName>
    <contact>Joe Schmoe</contact>
</client>
0

Simply put - yes.

More complicated - depends on what kind of request the other side is looking for. If they can accept html, then yes you are done. If they need another type of output - you might need to change your content-type headers.

0

You can make it a simple as you want. I would do it something like this:

<?php
 //Your sql connection
 $db = new PDO("mysql:dbname=testdb;host=127.0.0.1", "user", "pass");

 //Get the id from the url and cast it to an integer
 $id = (int) $_GET['id'];

 //select the stuff from your database
 $q = $db->prepare("SELECT `name`,`id`,`contact` FROM `blabla` WHERE id = ?");
 $q->execute( array( $id ) );
 $client = $q->fetch(2);

 //Output as xml
 header("Content-type: text/xml; charset=utf-8");
 echo 
"<?xml version="1.0"?>
  <client>
    <clientID>{$client['id']}</clientID>
    <clientName>{$client['name']}</clientName>
    <contact>{$client['contact']}</contact>
</client>"; 
 exit;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.