You are here:  » noob question about parsing external data


noob question about parsing external data

Submitted by vanarie on Fri, 2006-09-15 03:17 in

I'm new to php/xml data exchange model and just snagged magic parser to get started. I have a local mysql db and want to put a form on another server/website to write data to my local db.

Currently, I can write data to my db from external sites via the standard form post submit, but I have people who are asking me about an API to write the data.

So, how would this work using MagicP? A basic example would be great if it's not covered in the manual somewhere.

Submitted by support on Fri, 2006-09-15 07:17

Hi,

Within an API (web service) scenario; Magic Parser could be used to easily process a submitted XML document that contained any number of records that were to be inserted into your local database.

The XML document might be received by your API in one of two ways. Firstly (the more complicated but indusry standard way), you could develop a SOAP/XML web service. PHP has SOAP functionality which can be compiled in (it is not a default module) which is easy to use; more details here:

http://uk.php.net/manual/en/ref.soap.php

Now, the less complicated solution is to offer an API using the standard HTTP POST method, but where a valid XML document containing the records to be added to your database is POST'ed to your script instead of form data. The script can then access the XML via the $GLOBALS["HTTP_RAW_POST_DATA"]. The data must be submitted using a Content-Type: header of "text/xml".

For example, let's say you've published an API whereby a user can submit multiple records to your database by POST'ing the following XML document to your server (in this case, a database of people and their dates of birth):

<request>
  <record>
    <name>John Smith</name>
    <dob>1/1/1962</dob>
  <record>
  <record>
    <name>Adam Long</name>
    <dob>10/4/1988</dob>
  <record>
<request>

A simple PHP API script to accept that XML, process it using Magic Parser, and return an XML result might look like this:

<?php
  
require("MagicParser.php");
  function 
myRecordHandler($record)
  {
    
$name $record["NAME"];
    
$dob $record["DOB"];
    
// now insert $name and $dob into your database
  
}
  
$request $GLOBALS["HTTP_RAW_POST_DATA"];
  if (
$request)
  {
    
// parse the request using Magic Parser's built in string:// wrapper as the filename
    
MagicParser_parse("string://".$request,"myRecordHandler","xml|REQUEST/RECORD/");
    
$result "SUCCESS";
  }
  else
  {
    
$result "ERROR";
  }
  
header("Content-Type: text/xml");
  print 
"<response><result>".$result."</result></response>";
  exit();
?>

I hope this gives you something to get started from!

Cheers,
David.

Submitted by vanarie on Fri, 2006-09-15 09:27

So the submitted form could look like the references in this post:
www.phpbuilder.com/board/showthread.php?t=10304087

Once submitted, the form would write the records and return an XML result to be displayed on the same remote page. The remote server would HAVE TO have the means to accept and display the XML result as needed using an XML parser of some sort like MP.

This this correct?

For that matter, might it be the case that if I wanted to XML'ify my entire web service, I would in effect change all input/output formats in my php/mysql calls to XML and parse it out on my local server or an external server/page as needed?

Submitted by support on Fri, 2006-09-15 10:56

When data is being submitted to your site by way of an API (web service); there is no concept of a "form" as such, because the calling program is simply generating the appropriate XML automatically by software rather than by human input. The calling program can generate the XML however they wish - you just have to tell them what it looks like.

Once they've submitted it; if the objective of your web service is to provide a response which they then display within an HTML page; then yes - you can provide the response in XML and they can use any method of parsing they choose, as the language doesn't even need to be XML.

Hope this helps,
Cheers,
David.

Submitted by vanarie on Fri, 2006-09-15 14:36

>> The calling program can generate the XML however they wish - you just have to tell them what it looks like.

So, I would specify how the data should be formatted. Is this also called DTD - data type definition?

Submitted by support on Fri, 2006-09-15 14:55

Correct - you can specify the interface to your API using a DTD (Document Type Definition)...

http://www.w3schools.com/xml/xml_dtd.asp

Cheers,
David.

Submitted by vanarie on Sat, 2006-09-16 14:45

Hello,

What would a form post page look like that would submit this to my parsing script? Thanks!

<request>
  <record>
    <name>John Smith</name>
    <dob>1/1/1962</dob>
  <record>
  <record>
    <name>Adam Long</name>
    <dob>10/4/1988</dob>
  <record>
<request>

Submitted by support on Sun, 2006-09-17 03:12

Hi,

When you're writing an API, there is no concept of a form post page because the information is submitted to your web service URL by another computer program or script, not from a web browser.

The other script doesn't even have to be written in PHP or have anything to do with HTML, it just has to submit the XML to your specification (as per the DTD) to your parsing script using a HTTP "POST" request.

If you want to update data from a HTML form post page, you have to do it the "normal" way; where you just submit fields and then your script reads the fields and inserts the new record into a database.

Cheers,
David.

Submitted by vanarie on Sun, 2006-09-17 14:26

I appreciate your help!