web services

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
roice
Forum Commoner
Posts: 35
Joined: Tue Mar 02, 2010 9:14 am

web services

Post by roice »

Hello,
lets says that I have web service in some url address that get date, destination and package ID and retrive the package details.
How the php code should be in order to send the information to the web service and get the XML with the package data back?

Thank you in advanced
User avatar
mecha_godzilla
Forum Contributor
Posts: 375
Joined: Wed Apr 14, 2010 4:45 pm
Location: UK

Re: web services

Post by mecha_godzilla »

Hi,

If you want to query the status of a consignment (which I assume is what you're trying to do) then your web service can just capture a package ID from the URL:

https://internationalcouriers.xxx/track ... ageID=1234

You'll need some way to validate the person that's requesting this information though, otherwise it would be possible for anyone to query the database and find out tracking information for packages that don't belong to them if they tried to guess the package ID.

When your web service is called, it can capture the package ID like this

Code: Select all

$packageID = $_GET['packageID'];
and then include this value in a database query. Before going any further, I'll just add that you need to make sure the value is escaped and sanitised before it is included in the query or your script and database could be compromised.

You can then use the in-built XML functionality in PHP to generate a new XML file (technically, it's not a real file but the XML data held in a variable).

To automate the sending/receiving process you can use cURL - the way this works is that the user submits a form with the package ID that gets sent to (say) "track-my-parcel.php" and this script then uses cURL to call the web service and capture the output, which will just be an XML file. You can then use the in-built XML functionality in PHP to parse the XML data before displaying it to the user. If you decide to use cURL you can also call the web service using the POST method instead, which means that you could also send an user ID that the web service can authenticate against without this information appearing in the URL - although you would be advised to use a HTTPS connection if security is important.

So, in summary, if you want to design this kind of web service yourself you will need to know:

1. How to capture data from forms.
2. How to sanitise form data so that it's safe to use in database queries.
3. How to use the cURL library (or similar in-built functionality).
4. How to use the in-built XML functionality of PHP (if you don't want to manually generate and parse XML data in your scripts).
5. How to generate the correct headers to make your PHP script output XML data.
6. How to handle errors at each stage of the process in case something goes wrong (invalid tracking ID specified, web service temporarily unavailable, etc.)

HTH,

Mecha Godzilla
Post Reply