如何使用php和mysql将xml响应解析为变量并存储到表中?
如何将XML响应解析为变量并存储到表中 使用以下脚本获取XML响应:如何使用php和mysql将xml响应解析为变量并存储到表中?,php,xml,dom,Php,Xml,Dom,如何将XML响应解析为变量并存储到表中 使用以下脚本获取XML响应: <?php require_once 'db.php'; define( 'HOST', 'https://gateway.autodns.com'); define( 'XML_FILE', 'test.xml' ); $xml = implode( "", file(XML_FILE) ); header( 'Content-Type: text/xml' ); echo requestCurl( $xml ); f
<?php
require_once 'db.php';
define( 'HOST', 'https://gateway.autodns.com');
define( 'XML_FILE', 'test.xml' );
$xml = implode( "", file(XML_FILE) );
header( 'Content-Type: text/xml' );
echo requestCurl( $xml );
function requestCurl( $data )
{
$ch = curl_init( HOST );
curl_setopt ( $ch, CURLOPT_POSTFIELDS, $data );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt ( $ch, CURLOPT_SSL_VERIFYPEER, FALSE );
if( !$data = curl_exec( $ch )) {
echo 'Curl execution error.', curl_error( $ch ) ."\n"; return FALSE;
}
curl_close( $ch );
return $data;
}
$api_insert = $mysqli->query("INSERT INTO api name, created, payable VALUES ('', '', '')");
?>
PHP有一个名为
simplexml\u load\u string
的漂亮方法,它的作用是将XML结构化字符串加载到一个易于使用的对象中。假设您有一个名为$xmlResponse
的字符串变量,其中包含以下格式正确的XML:
$xmlResponse
<?xml version='1.0'?>
<business>
<company>Code Blog</company>
<owner>Nic Raboy</owner>
<employees>
<employee>
<firstname>Nic</firstname>
<lastname>Raboy</lastname>
</employee>
<employee>
<firstname>Maria</firstname>
<lastname>Campos</lastname>
</employee>
</employees>
</business>
使用XML解析将XML生成数组 下面的例子
$simple = "<para><note>simple note</note></para>";
$p = xml_parser_create();
xml_parse_into_struct($p, $simple, $vals, $index);
xml_parser_free($p);
echo "Index array\n";
print_r($index);
echo "\nVals array\n";
print_r($vals);
$simple=“simple note”;
$p=xml_parser_create();
xml_将_解析为_结构($p、$simple、$vals、$index);
无xml解析器($p);
echo“索引数组\n”;
打印(索引);
回显“\nVals数组\n”;
打印(VAL);
使用:
XPath表达式选择/response/result/
下的所有(/
)域
节点DOMXPath::query
返回一个可遍历的节点列表,该列表包含$domains
变量
下一个循环从$domains
获取DOMElement
项,内部循环将子节点收集到$props
数组中。如果收集了所有元素(名称
、应付款
和创建的
),则printf
将它们打印到标准输出
输出
您可以使用对XPath的额外调用仅获取特定元素,而不是获取所有子节点:
$children = $xpath->query('*[self::name or self::payable or self::created]', $d);
if ($children->length != 3) {
continue;
}
注意,域元素作为相对XPath表达式的上下文节点传递
为了使用MySQLi将值存储到数据库中,请将printf
调用替换为以下内容:
$sql = sprintf(
'INSERT INTO `api` (name, created, payable) VALUES ("%s", "%s", "%s")',
$mysqli->real_escape_string($props['name']),
$mysqli->real_escape_string($props['payable']),
$mysqli->real_escape_string($props['created'])
);
$result = $mysqli->query($sql);
我试过这个方法,但有一些错误<代码>$simple=requestCurl($xml)$p=xml_parser_create();xml_将_解析为_结构($p、$simple、$vals、$index);无xml解析器($p);echo“索引数组\n”;打印(索引);回显“\nVals数组\n”;打印(VAL)代码>错误是XML解析错误:语法错误位置:https://localhost/auto_dns_api/api.php 第1行第1列:索引数组
谢谢。当我打印printf(“名称:%s\n标题:%s\n创建:%s\n\n”、$props['Name']、$props['payment']、$props['created']);“
我收到错误XML解析错误:找不到根元素位置:https://localhost/auto_dns_api/api.php 第1行,第1列:
您知道它为什么会这样显示。$xml=requestCurl($xml)
正确吗?如果我是这样定义的,则会得到类似XML解析错误:语法错误位置:https://localhost/auto_dns_api/api.php 第1行第1列:名称:facebook。拜仁
@samsam是一个有效代码。您需要将$xml
变量设置为包含从远程主机下载的xml的字符串
// $xml = 'XML content';
$doc = new DOMDocument;
$doc->loadXML($xml);
$xpath = new DOMXPath($doc);
$expr = '/response/result//domain';
$domains = $xpath->query($expr);
foreach ($domains as $d) {
$props = [];
foreach ($d->childNodes as $child) {
$props[$child->nodeName] = $child->nodeValue;
}
if (
isset($props['name']) &&
isset($props['payable']) &&
isset($props['created'])
)
{
printf(
"Name: %s\nPayable: %s\nCreated: %s\n\n",
$props['name'],
$props['payable'],
$props['created']
);
}
}
Name: facebook.bayern
Payable: 2017-05-06 13:34:46
Created: 2015-05-06 13:34:46
Name: google.com
Payable: 2017-04-08 22:04:04
Created: 2016-04-08 22:04:05
$children = $xpath->query('*[self::name or self::payable or self::created]', $d);
if ($children->length != 3) {
continue;
}
$sql = sprintf(
'INSERT INTO `api` (name, created, payable) VALUES ("%s", "%s", "%s")',
$mysqli->real_escape_string($props['name']),
$mysqli->real_escape_string($props['payable']),
$mysqli->real_escape_string($props['created'])
);
$result = $mysqli->query($sql);