使用XML标记属性作为PHP变量,并在HTTP请求中使用它

时间:2020-12-10 00:42:18

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?

是否有办法将XML属性保存为PHP变量,并自动将其放入另一个http请求中?或者有更好的方法吗?

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

基本上,我向服务器发送一个http请求,我得到的代码是这样的:

<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:

我需要将字符串保存在属性2中,并将其插入到一个http请求中,该请求类似如下:

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

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

'123456' ID必须是属性'two'中的字符串。

Any help would be appreciated!

如有任何帮助,我们将不胜感激!

Thanks, Jane

谢谢,简

3 个解决方案

#1


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.

如果你100%完全完全地完全确信内容总是有那种精确的格式,你可以用regex作为其他答案。

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

否则,DOM不是很难管理……

$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.

如果您有一个您将收到的XML的真实示例,请发布它,我将对此进行修改。

#2


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.

如果你能…发送JSON。如果不能,返回的唯一内容是那个小片段,那么我将使用regex提取值。

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

类似/ . * 2 = "([^]+)”。*/应该匹配所有内容,用“$1”替换匹配

Otherwise use simplexml.

否则使用simplexml。

#3


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;
}

?>

#1


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.

如果你100%完全完全地完全确信内容总是有那种精确的格式,你可以用regex作为其他答案。

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

否则,DOM不是很难管理……

$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.

如果您有一个您将收到的XML的真实示例,请发布它,我将对此进行修改。

#2


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.

如果你能…发送JSON。如果不能,返回的唯一内容是那个小片段,那么我将使用regex提取值。

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

类似/ . * 2 = "([^]+)”。*/应该匹配所有内容,用“$1”替换匹配

Otherwise use simplexml.

否则使用simplexml。

#3


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;
}

?>