Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP-将传入的HTTP POST转换为变量_Php_Xml_Post - Fatal编程技术网

PHP-将传入的HTTP POST转换为变量

PHP-将传入的HTTP POST转换为变量,php,xml,post,Php,Xml,Post,我正在使用一个web服务,该服务向PHP页面提交HTTP帖子,如下所示: 表格/职位参数: 没有 标题: 内容类型:text/xml 正文: 留言 命令和短信 12345 123456 15552345678 2. 我需要能够将每个XML元素转换为PHP变量,以便更新数据库。我以前从未使用XML数据处理过传入的POST,也不知道从哪里开始-我熟悉处理传入的GET/POST请求,但不熟悉原始XML。请看一下SimpleXMLElement: 如果执行以下操作,则可以看到密钥: print_r

我正在使用一个web服务,该服务向PHP页面提交HTTP帖子,如下所示:

表格/职位参数: 没有

标题: 内容类型:text/xml

正文:


留言
命令和短信
12345
123456
15552345678
2.

我需要能够将每个XML元素转换为PHP变量,以便更新数据库。我以前从未使用XML数据处理过传入的POST,也不知道从哪里开始-我熟悉处理传入的GET/POST请求,但不熟悉原始XML。

请看一下
SimpleXMLElement

如果执行以下操作,则可以看到密钥:

print_r($_POST);
大多数时候,我们使用这种api,我们想记录它,因为我们看不到它:

$debugFile = 'debug.log'
file_put_contents($debugFile, print_r($_POST, true), FILE_APPEND);

还可以查看来自的答案,以获取原始输入。

我认为您需要使用。然后,您可以按照@ChristianGolihardt的建议使用
simplexmlement

请注意,
HTTP\u RAW\u POST\u DATA
仅在php.ini中启用了
始终填充\u RAW\u POST\u DATA
设置时可用。否则,可能最容易做到这一点:

$postData = file_get_contents("php://input");
...
$xml = new SimpleXMLElement($postData);
...

这将消除所有SimpleXMLElement对象并返回数组:

从xml字符串:

<?php

$xml='<?xml version="1.0"?>
<mogreet>
<event>message-in</event>
<type>command_sms</type>
<campaign_id>12345</campaign_id>
<shortcode>123456</shortcode>
<msisdn>15552345678</msisdn>
<carrier><![CDATA[T-Mobile]]></carrier>
<carrier_id>2</carrier_id>
<message><![CDATA[xxxx testing]]></message>
</mogreet>';

$xml = simplexml_load_string($xml);
$xml_array = json_decode(json_encode((array) $xml), 1);

print_r($xml_array);

?>
输出:

Array
(
    [event] => message-in
    [type] => command_sms
    [campaign_id] => 12345
    [shortcode] => 123456
    [msisdn] => 15552345678
    [carrier] => Array
        (
        )

    [carrier_id] => 2
    [message] => Array
        (
        )

)

你能在php中以字符串的形式获取xml代码吗?OP说xml来自POST数据,而不是文件。
<?php

$xml='<?xml version="1.0"?>
<mogreet>
<event>message-in</event>
<type>command_sms</type>
<campaign_id>12345</campaign_id>
<shortcode>123456</shortcode>
<msisdn>15552345678</msisdn>
<carrier><![CDATA[T-Mobile]]></carrier>
<carrier_id>2</carrier_id>
<message><![CDATA[xxxx testing]]></message>
</mogreet>';

$xml = simplexml_load_string($xml);
$xml_array = json_decode(json_encode((array) $xml), 1);

print_r($xml_array);

?>
$xml = simplexml_load_file("mogreet.xml");

$xml_array = json_decode(json_encode((array) $xml), 1);

print_r($xml_array);
Array
(
    [event] => message-in
    [type] => command_sms
    [campaign_id] => 12345
    [shortcode] => 123456
    [msisdn] => 15552345678
    [carrier] => Array
        (
        )

    [carrier_id] => 2
    [message] => Array
        (
        )

)