开发者

Read XML posted via cURL into an array for potential DB insert

开发者 https://www.devze.com 2023-04-06 16:47 出处:网络
I am receiving XML POSTED via curl, and can successfully write the posted XML to a file, so I know that its being posted successfully.

I am receiving XML POSTED via curl, and can successfully write the posted XML to a file, so I know that its being posted successfully.

I really need to access the posted XML and create an array as soon as it is received, so I can set up variables and prepare the posted XML for a database Insert.

Can anyone show an example of how I would access the XML, received as:

$postXML = trim(file_get_contents('php://input'));

Which is:

<?xml version="1.0" encoding="UTF-8" ?>
<data>
    <first_name>Larry</first_name>
    <last_name>Jones</last_name>开发者_如何学编程
    <url>www.somewhere.com</url>
</data>

I need to access <first_name>, <last_name> and <url> and create as variables for processing and DB inserts.

Any help is very much appreciated!


Use simplexml
Load the XML string with:

$xml = simplexml_load_string($postXML); 

Then access the data you want:

$first_name = (string)$xml->first_name;
$last_name = (string)$xml->last_name;
$url = (string)$xml->url;
0

精彩评论

暂无评论...
验证码 换一张
取 消