1

Is there any way to save an XML attribute as a PHP variable and automatically place it in another http request? Or is there a better way to do that?

Basically, I send a server an http request, the code I get back looks something like this:

<tag one="info" two="string">

I need to save the string in attribute two and insert it in an http request that looks something like this:

http://theserver.com/request?method=...&id=123456

The '123456' ID needs to be the string in attribute 'two'.

Any help would be appreciated!

Thanks, Jane

5
  • How are you building your request URL now? Are you already parsing the XML using DOM or SimpleXML? In other words,show what you got so far as there are 10+ different ways to do this. Commented May 17, 2011 at 22:38
  • Wow, I hadn't even thought that far, yet... I was thinking of using simpleXML or SAX, but I was actually told by the people handling the remote server that DOM was the better choice to parse their data. Not sure why that is, but then again my knowledge of PHP is pretty basic. Commented May 17, 2011 at 22:45
  • simpleXML is a lovely way to parse XML... until it isn't. DOM may be a little harder to get your head around at first, but it's far more flexible and powerful in the long term. Commented May 17, 2011 at 22:48
  • Shouldn't listen to people that do not give a reason :). If your knowledge of PHP is basic, simpleXML feels more intuitive. If you already know the DOM through other languages, then DOM may feel more at home for you. The principle remains that you need to extract the info from the tag, urlencode it and concatenate it to the URL you are building. Commented May 17, 2011 at 22:57
  • OK thanks, wow lots of useful information here. Hope I can wrap my head around these new concepts and make good use of all of the help I've gotten here. Thanks! Commented May 17, 2011 at 23:05

3 Answers 3

3

If you are 100% entirely absolutely completely TOTALLY sure that the content will always have that exact format, you can probably use regex as the other answers have suggested.

Otherwise, DOM isn't very hard to manage...

$dom = new DOMDocument;
$dom->loadXML($yourcontent);
$el = $dom->getElementsByTagName('A')->item(0); // presuming your tag is the only element in the document
if ($el) {
    $id = $el->getAttribute('id');
}
$url = 'http://theserver.com/request?method=...&id=' . $id;

If you have a real example of the XML that you'll receive, please do post it and I'll adapt this answer for it.

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you! This is basically it (after the dtd): <ATH user="56834" date="20110517"> <A cat="4" id="3240985"/> </ATH> I need the id attribute to be automatically inserted in my follow-up requests...
@Jane See the updated answer. I don't know what you mean by the last sentence though; I think it's probably something different to what this question's dealing with...
Great!!! Thank you, Lonesomeday, so instead of $yourcontent, can I load the actual URL?
@Jane You'll want $dom->load($yourUrl);
1

If you can... send it in JSON. If you can't, and the only thing that is returned is that wee snippet, then I'd use regex to pull out the value.

Something like /.*two="([^"]+)".*/ should match everything, replace matches with '$1'

Otherwise use simplexml.

Comments

1

You could use:

<?php

$string = '<tag one="info" two="123456">';
if (preg_match('/<tag one="[^"]*" two=\"([^"]*)\">/',$string,$match)) {
    $url = 'http://theserver.com/request?method=...&id=' . $match[1];
    echo $url;
}

?>

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.