Log in

View Full Version : XML Response Help Needed :(



maddyui
11-16-2010, 09:53 AM
A Salute to all the dynamic drive members,
I am a PHP-MySQL coder. I dont know much of XML. But all of a sudden I am asked to make use of an API and create a working script.

I dont know how to get variables from XML. Kindly help me out. Kindly get me the below response in simple PHP variables also help me learn how you do it.

Here Is What I have to work on :


http://www.mobikwik.com/recharge.do?uid=<user_id>&pwd=<password>&cn=<cellNumber>&op
=<operator_code>&cir=<circle_code>&amt=<recharge_amount>&reqid=<reqid>
uid : Member Id of retailer registered with mobikwik
pwd : Password of the above retailer.
cn : Cell number to recharge
op : Operator of cell number ( See Appendix)
cir : Circle of cell number ( See Appendix)
amt : Amount to recharge
reqid : Request ID for this transaction. It should be a unique 20 digit number
Sample Request :
http://www.mobikwik.com/recharge.do?uid=hemanta.gupta@gmail.com&pwd=password&cn=98
12312345&op=1&cir=5&amt=50&reqid=12544172981250
Sample success response:
<recharge>
<status>SUCCESS</status>
<txId>12544172981250</txId>
<balance>1000</balance>
<discountprice>980</discountprice>
</recharge>
txId : Indicates transaction id returned by mobikwik.
balance : Indicates current Mobikwik balance of the user.

With Regards

djr33
11-16-2010, 02:37 PM
That's not XML except for this part:
<recharge>
<status>SUCCESS</status>
<txId>12544172981250</txId>
<balance>1000</balance>
<discountprice>980</discountprice>
</recharge>
If you can isolate that, you can use an XML function library (for example http://php.net/manual/en/book.simplexml.php) and work from there.
If you can't isolate it or need to analyze all of that data, you'll need to use another approach.
You could attempt regex (more direct, but more difficult) or use basic string functions if the data is predictable. For example, you could split it at each line break and work from there.

The main point is that you need to control (limit and standardize) the amount/type of data output, then it will be much easier to work with it as input. If you can't control the output, your job will be more difficult (not to say impossible), but hopefully in that case the format is predictable (even if inconvenient).

shiprecked
11-17-2010, 10:21 AM
Hi,

I want to understand how did you get information about MobiKwik's API, because there is a legal/copyright issue associated with their API. And most importantly, are you even authorized to use it/sample it for a discussion in forums.

maddyui
11-17-2010, 07:31 PM
@djr33
i understand that on calling the URL.. it returns the data in XML format...

but how to process it. Do you mind giving a sample of it pls? :(

a function that ud process this data n gimme in simple php variables?

traq
11-17-2010, 10:53 PM
I want to understand how did you get information about MobiKwik's API, because there is a legal/copyright issue associated with their API. And most importantly, are you even authorized to use it/sample it for a discussion in forums.
I would assume maddyui is taking care of any legal issues.

If you have some reason to suspect otherwise, I would suggest you address it more appropriately. Your comment reads like a wild, demanding accusation.

You mention a "legal/copyright" issue, but you don't mention what it is, or why it's any of your concern. Likewise, do you have some authority to demand that maddyui proves they are authorized to use the api? If so, it might be more appropriate to deal with the issue privately.


but how to process it. Do you mind giving a sample of it pls?
did you look at the link djr gave? http://php.net/manual/en/book.simplexml.php

maddyui
11-17-2010, 11:52 PM
@traq
I have all legal rights to use their API. There is no way that their API is illegal in any aspect.
Just for your confirmation you can check this :

http://www.google.co.in/search?hl=en&source=hp&q=mobikwik+api+illegal&aq=f&aqi=&aql=&oq=&gs_rfai=

Yes I had a look at the link http://php.net/manual/en/book.simplexml.php
but i got so confused roaming there. Can you be kind enough to give me a simple function for it?

Thanks

djr33
11-18-2010, 01:44 AM
Working with XML in PHP isn't simple. That's what you need to use, or another similar library. To use any of them, you need to be comfortable using classes. You can find tutorials for classes of course, and that's probably a good place to start, and also (if you look enough) probably tutorials for using XML in PHP-- with some examples. But there is no "simple" function that will do everything you need.

traq
11-18-2010, 05:17 AM
How you would start depends on how the data is returned to you. Once you have it assigned to a php $variable, you can do something like this:
$xmlString = '<recharge><status>SUCCESS</status><txId>12544172981250</txId><balance>1000</balance><discountprice>980</discountprice></recharge>';
$X = new SimpleXMLElement($xmlString);
// assuming there is only one set of elements,
// and there is only one of each element,
// you can access them directly fairly easily:
print $X->status; // prints "SUCCESS"
// or assign them to variables
$status = $X->status; // $status holds value 'SUCCESS'
If you have more complex XML structures, or if you need to modify or re-save the XML tree, it gets significantly more complex.

In addition, while you may be tempted to think of the SimpleXML Object ( $X in the example above) as a variable or an array, it is not. Objects are fundamentally different, and you'll run into problems fairly quickly if you don't read up and learn about how to handle them. SimpleXML is the easiest library I've used (far better than DomDocument), but it's isn't really "simple." As djr says, you need to have an understanding of how to use object-oriented PHP before this will make any sense to you.

shiprecked
11-18-2010, 08:31 AM
Dear MaddyUI,

I am VP-Operations at MobiKwik.com and only affiliate(s)/affiliate teams authorized by me are allowed to carry out the API integration.

At the same time, please confirm your client details. If you are not willing to share the information in a public forum, then mail your details to me at nikhil@mobikwik.com.

Thanks