PHP Variable into a Xml Request String

Echoing PHP Variable inside XML

Just concatenate the string

$post_string = '

<?xml version="1.0" encoding="UTF-8" ?>

<Ping vid="" sid="">
<ProjectInfo>
<ProjectId>' . $projectid . '</ProjectId>
<SubProject></SubProject>
</ProjectInfo>
</Ping>

';

how to provides xml request as variable in php

PHP is parsing $SysName:XML as a variable. If you want to send the literal string '$$SysName:XML' you need to escape every dollar sign in the XML string like this:

<SVEXPORTFORMAT> \$\$SysName:XML </SVEXPORTFORMAT>

Load PHP variables into XML file

You don't create the initial XML file, the library you are using creates it for you.

XML DOM is a fine choice for this job.

$xml = new DOMDocument();                                  # Create a document
$xml_firstname = $xml->createElement("firstname", "Over"); # Create an element
$xml_lastname = $xml->createElement("lastname", "Coder"); # Create an element
$xml->appendChild($xml_firstname); # Add the element to the document
$xml->appendChild($xml_lastname); # Add the element to the document
$xml->save("myfancy.xml"); # Save the document to a file

The output would be

<?xml version="1.0" encoding="utf-8"?>
<firstname>Over</firstname>
<lastname>Coder</lastname>

Inserting Query String Variables in XML using PHP/curl

The string definition is bad
use this

$xml = "<xmlrequest>
<details>
<emailaddress>{$email}</emailaddress>
<mailinglist>8</mailinglist>
<format>html</format>
<confirmed>no</confirmed>
</details>
</xmlrequest>";

or this

$xml = '<xmlrequest>
<details>
<emailaddress>' . $email . '</emailaddress>
<mailinglist>8</mailinglist>
<format>html</format>
<confirmed>no</confirmed>
</details>
</xmlrequest>';

Because this variable probably can be various string I think it's better if you use <![CDATA[]]> section around the email.

Insert PHP variables into CURL XML post

You are using simple quotes ' in the XML string construction. Inside the simple quotes, the variables aren't interpreted. You must encapsulate your XML string inside double quotes " or concat the variable in your string.

<?php
$s = 'a string';
echo '$s'; // Show $s
echo "$s"; // Show a string

Example on your string with concatenation :

$unauth = 'attribute value';
$xml_data = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<TipsApiRequest xmlns="http://www.avendasys.com/tipsapiDefs/1.0">
<TipsHeader version="3.0"/>
<Endpoints>
<Endpoint status="Known" macAddress="'.$unauth.'">
<EndpointTags tagName="unauthorized" tagValue="true"/>
</Endpoint>
</Endpoints>
</TipsApiRequest>';

You can also use HEREDOC string construction which interprete variables inside :

$unauth = 'attribute value';
$xml_data = <<<XML
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<TipsApiRequest xmlns="http://www.avendasys.com/tipsapiDefs/1.0">
<TipsHeader version="3.0"/>
<Endpoints>
<Endpoint status="Known" macAddress="$unauth">
<EndpointTags tagName="unauthorized" tagValue="true"/>
</Endpoint>
</Endpoints>
</TipsApiRequest>
XML;

Also you must validate POST data using the PHP filters function to avoid code injection in your XML request.

Passing PHP variables to XML in a .php file

You don't use DOM this way. You use the DOM API to create the entire document:

$doc   = new DOMDocument();
$books = $doc->createElement( "books" );
$doc->appendChild( $books );
// ...

See:

  • http://www.ibm.com/developerworks/library/os-xmldomphp/
  • http://www.tonymarston.net/php-mysql/dom.html
  • https://web.archive.org/web/1/http://articles.techrepublic%2ecom%2ecom/5100-10878_11-6141415.html

A more verbose example (generating XHTML with DOM)

// Create head element
$head = $document->createElement('head');
$metahttp = $document->createElement('meta');
$metahttp->setAttribute('http-equiv', 'Content-Type');
$metahttp->setAttribute('content', 'text/html; charset=utf-8');
$head->appendChild($metahttp);

See this tutorial on how to use DOM for XHTML. For reuse of code, you can write your own classes extending DOM classes to get configurable components.


If you don't want to use DOM or want to use plain text for generating the XML, just approach it like any other template, e.g.

<root>
<albums>
<album id="<?php echo $albumId; ?>">
<title><?php echo $title; ?></title>
... other elements ...
</album>
</albums>
</root>


Related Topics



Leave a reply



Submit