I have a php file that contains:
<?php
$no = $_POST['no'];
$url = "http://domain.tld/answer.xml";
?>
The $url stores a URL that links to an xml file and that xml files is like:
<?xml version="1.0" encoding="UTF-8"?>
<vxml version = "2.1" >
<Call> </Call>
</vxml>
I want to pass the $_POST['no'] to my xml file. Suppose if $_POST['no'] is 9999988888, then the tag in xml should be like:
<Call>9999988888</Call>
Can anyone tell how to do this? :)
that xml file is on my own server.
you could use simplexml, as:
$no = $_POST['no'];
$xml = simplexml_load_file('your_xml_file.xml');
$xml->Call = $no;
//save/update your xml
$xml->asXML('your_xml_file.xml');
You need to generate the xml using PHP.
First of all, rename your .xml to .php.
Then, start it using
<?php
header('Content-type: text/xml');
?>
When you reach your call tag, change it to :
<Call><?php echo intval($_GET['no']); ?></Call>
Note: I've assumed your no is a number. If it's not, remove intval. It's nice to have it as it will protect you from injections.
At last, call your XML file using :
$url = "http://domain.tld/answer.php?no=8877454";
Your XML should have some keywords, i.e.
<?xml version="1.0" encoding="UTF-8"?>
<vxml version = "2.1" >
<Call>{{CALL}}</Call>
</vxml>
In your PHP File, just fetch this XML & use str_replace to replace those keywords,
<?php
$no = $_POST['no'];
$url = "http://domain.tld/answer.xml";
$xml = file_get_contents($url);
$newXML = str_replace("{{CALL}}", $no, $xml);
header('Content-type: text/xml');
echo $newXML;
?>
Related
I'm trying to use php to delete an xml element but it doesn't work. I tried some different code but no one works. I would also like to use cookies to get element in the future. Can you suggest me what I have to do ? I'm not expert and for this I'm in difficulty.
Here the code:
<?php
$dom = new DOMDocument();
$dom->load("Dati.xml");
$matchingElements = $dom->getElementsByTagName("Matematica");
$totalMatches = $matchingElements->length;
$elementsToDelete = array();
$elementsToDelete[] = $matchingElements->item(0);
foreach ( $elementsToDelete as $elementToDelete ) {
$elementToDelete->parentNode->removeChild($elementToDelete);
}
$dom->save($xmlFileToLoad);
echo "<script type='text/javascript'>";
echo "window.close();";
echo "</script>";
echo "Puoi chiudere questa pagina";
?>
Here the xml:
<?xml version="1.0" encoding="UTF-8"?>
<document>
<Informatica>
<nome>aaaa</nome>
<classe>3C</classe>
<titolo>Informatica</titolo>
<materia>Informatica</materia>
<ISBN>123456789101112</ISBN>
<prezzo>12</prezzo>
<autori>tizio</autori>
<contatto>nanni-lombardo1#hotmail.it</contatto>
<codice>123456</codice>
</Informatica>
<Matematica>
<nome>bbb</nome>
<classe>3C</classe>
<titolo>math</titolo>
<materia>Matematica</materia>
<ISBN>123456789101112</ISBN>
<prezzo>12</prezzo>
<autori>tizio</autori>
<contatto>nanni-lombardo1#hotmail.it</contatto>
<codice>123456</codice>
</Matematica>
</document>
Please be sure to :
Remove the space before your XML start tag
Define your $xmlFileToLoad variable
Make your destination XML writable (see Chmod & permissions)
My test file works fine : https://eu.andredasilva.fr/testandre/test.php (source code : https://eu.andredasilva.fr/testandre/test.php.source)
Base XML : https://eu.andredasilva.fr/testandre/Dati.xml
Result XML : https://eu.andredasilva.fr/testandre/test.xml
Hi guys very new to the php world.
I am listening for a PHP post that contains xml, when the xml is retrieved i need to access individual nodes. I am able to echo the full xml file but not individual attributes.
Currently I am just sending the data using a chrome extension Postman. There is no front end code. Here is my XML:
<?xml version="1.0" encoding="UTF-8"?>
<job>
<job_ref>abc123</job_ref>
<job_title>Test Engineer</job_title>
</job>
And here is my PHP:
if($_SERVER['REQUEST_METHOD'] === 'POST') {
$xml = file_get_contents('php://input');
echo $xml;
$xml=simplexml_load_file($xml);
echo $xml->job_ref . "<br>";
echo $xml->job_title . "<br>";
}else{
die();
}
Any hep wopuld be amazing am I am very stuck.
Many thanks
simplexml_load_file expect PATH to the XML file, not its content. You have to use simplexml_load_string instead:
$xml = simplexml_load_string($xml);
I am trying to take a two-tiered foreach which outputs an xml - and copy only the resulting output of that into another file.
I am so confused that I don't even know if to ask "how to copy output to other file" or "how to convert foreach into string" - because writing a string over is no problem.
So far I have
// clear previous contents of tutorials.xml
$myFile = "tutorials.xml";
$fh = fopen($myFile, 'a') or die("can't open file");
$stringData = "";
fwrite($fh, $stringData);
// here begins the string I want to write to the other file
echo <<<EOF
<?xml version="1.0" encoding="UTF-8"?>
<albums>
EOF;
foreach($albumInfo as $album) {
echo <<<EOF
<album>
<id>{$album->id}</id>
<title>{$album->title}</title>
<videos>
EOF;
}
foreach($videos as $video) // loop through our videos
{
$minutes = floor($video->duration/60);
$secondsleft = $video->duration%60;
if($secondsleft<10)
$secondsleft = "0" . $secondsleft;
echo <<<EOF
<video>
<id>{$video->id}</id>
<title>
<description>{$video->description}</description>
<duration>{$video->duration}</duration>
</video>
EOF;
}
echo <<<EOF
</album>
EOF;
?>
<?php echo '</albums>' ?>
// here ends the string I want to write to the other file
// the script below just takes the raw php and copies it over - I need the output.
<?php
copy('tutorials-job.php', 'tutorials.xml');
?>
Thank you for your help!
You already show that you know how to output content to a file with the first few lines of this code. You can either load your output into an output buffer and then write the contents of the buffer to the file, or instead of echoing everything, put that data into a string and just write that string to a file.
That being said, if you are not stuck on XML format, you might consider using something like JSON to where you can easily convert between your array/object representation to your serialized representation with a single line of code.
replace echo with an assignment to a variable then write that to the file
echo <<<EOF
<?xml version="1.0" encoding="UTF-8"?>
<albums>
EOF;
becomes
$str='
<?xml version="1.0" encoding="UTF-8"?>
<albums>
';
latter use
$str .='
to add to the string
I have a slight problem. I need to parse a file and populate a web banner with the results. Problem is, the file is called : "_banner_products.php" and it's contents are as follows:
<?php header('Content-Type:text/xml'); ?><?php echo '<?xml version="1.0" encoding="UTF-8"?>'; ?>
<carouselle>
<firstLayer>
<layerName>Leica Disto X310</layerName>
<layerProduct>Disto X310</layerProduct>
<layerPic>http://www.leicaestonia.ee/extensions/boxes_design/flashpics/1334482548.jpg</layerPic>
<layerPrice>0,-</layerPrice>
<layerPriceOld></layerPriceOld>
<layerLink>http://www.leicaestonia.ee/index.php?id=11627</layerLink>
<layerTimer>01.05.2012 00:00</layerTimer>
</firstLayer>
<firstLayer>
.....
.....
</firstLayer>
</carouselle>
How can I loop through this file to group all the "firstLayer" children into one and so on..
If I just use:
$file = fopen("_banner_products.php", "r");
while (!feof($file)){
$line = fgets($file);
}
simplexml_load_file throws this-
"_banner_products.php:1: parser error : Start tag expected, '<' "
Then I only get the contents of the <...> tags meaning there is no way for me to differentiate if I am out of the scope already.
Thanks for anyone responding. If anything is unclear I´ll try to explain more.
EDIT.
Thank you for the solution, indeed using the full URL worked:
simplexml_load_file("http://localhost/MySite/_banner_products.php");
You are having issue because simplexml_load_file is treating your file like a local xml file .. what you need to do is add the full URL
Example
simplexml_load_file("http://localhost/web/_banner_products.php");
Use Case getting layerName for example
_banner_products.php
<?php
header ( 'Content-Type:text/xml' );
echo '<?xml version="1.0" encoding="UTF-8"?>';
?>
<carouselle>
<firstLayer>
<layerName>Leica Disto X310</layerName>
<layerProduct>Disto X310</layerProduct>
<layerPic>http://www.leicaestonia.ee/extensions/boxes_design/flashpics/1334482548.jpg</layerPic>
<layerPrice>0,-</layerPrice>
<layerPriceOld></layerPriceOld>
<layerLink>http://www.leicaestonia.ee/index.php?id=11627</layerLink>
<layerTimer>01.05.2012 00:00</layerTimer>
</firstLayer>
<firstLayer>
<layerName>Leica Disto X310</layerName>
<layerProduct>Disto X310</layerProduct>
<layerPic>http://www.leicaestonia.ee/extensions/boxes_design/flashpics/1334482548.jpg</layerPic>
<layerPrice>0,-</layerPrice>
<layerPriceOld></layerPriceOld>
<layerLink>http://www.leicaestonia.ee/index.php?id=11627</layerLink>
<layerTimer>01.05.2012 00:00</layerTimer>
</firstLayer>
</carouselle>
view details
$xml = simplexml_load_file("http://localhost/lab/stockoverflow/_banner_products.php");
echo "<pre>" ;
foreach($xml as $key => $element)
{
echo $element->layerName , PHP_EOL ;
}
The most obvious way to do this is to strip out the first line, and add the XML declaration back in with your code.
You could also parse the file with PHP, using eval(), but be very sure about what you are parsing, as this could be a very large security hole.
Here is a simple form I am using to send XML data in admin_xml.php
<form name="review" action="admin_xml.php" method="post">
<textarea name="xml" cols="40" rows="10"></textarea>
<input class="submit" type="submit" value="Submit Request">
</form>
Here is the XML which I enter in order to retrieve the data from MySQL Database
<?xml version="1.0" encoding="UTF-8"?>
<GetOrdersIds>
<Credentials>
<Username>my_username</Username>
<Password>my_password</Password>
</Credentials>
<Criterions>
<OrderNumber></OrderNumber>
<StartDate>2009-01-01</StartDate>
<EndDate>2009-07-01</EndDate>
</Criterions>
</GetOrdersIds>
Here is the php code which extracts the tags from the xml:
<?php
$text_all = "<?xml version=\"1.0\"?>\r\n<GetOrdersIds version=\"1.0\">\r\n<Credentials>\r\n<Username>my_username</Username>\r\n<Password>my_password</Password>\r\n</Credentials>\r\n<Criterions>\r\n<OrderNumber></OrderNumber>\r\n<StartDate>2009-01-01</StartDate>\r\n<EndDate>2009-07-01</EndDate>\r\n</Criterions>\r\n</GetOrdersIds>";
$field = "Criterions";
$result = substr($text_all, strpos($text_all, "<".$field.">")+strlen("<".$field.">"), strpos($text_all, "</".$field.">")-strlen("<".$field.">")-strpos($text_all, "<".$field.">"));
?>
The result from the above php code is:
<OrderNumber></OrderNumber>
<StartDate>2009-01-01</StartDate>
<EndDate>2009-07-01</EndDate>
When the script is run, the php code goes through my mysql data, and extracts all orders between the Start Date and the End Date given above.
Is there a better way of improving the following php code, so that it performs the same function:
$result = substr($text_all, strpos($text_all, "<".$field.">")+strlen("<".$field.">"), strpos($text_all, "</".$field.">")-strlen("<".$field.">")-strpos($text_all, "<".$field.">"));
This is the code which searches through the xml data and retrieves all the tags.
UPDATE :
Can anyone list the $result code in SimpleXML?
Some temp vars will help and also make it easier to read.
$result = substr($text_all, strpos($text_all, "<".$field.">")+strlen("<".$field.">"), strpos($text_all, "</".$field.">")-strlen("<".$field.">")-strpos($text_all, "<".$field.">"));
Can be rewritten to.
$tagLen = strlen('<'.$field.'>');
$openTag = strpos($text_all, '<'.$field.'>');
$closeTag = strpos($text_all, '</'.$field.'>', $openTag);
$result = substr($text_all, $openTag + $tagLen, $closeTag - $tagLen - $openTag);
or use SimpleXML
$doc = simplexml_load_string($text_all);
echo $doc->Criterions->asXML();
If you want the individual values use this.
$doc = simplexml_load_string($text_all);
echo $doc->Criterions->OrderNumber;
echo $doc->Criterions->StartDate;
echo $doc->Criterions->EndDate;
if you want the element in XML then use asXML():
$doc = simplexml_load_string($text_all);
echo $doc->Criterions->OrderNumber->asXML();
echo $doc->Criterions->StartDate->asXML();
echo $doc->Criterions->EndDate->asXML();
With SimpleXML:
<?php
$xmlSTR = '<?xml version="1.0" encoding="UTF-8"?>
<GetOrdersIds>
<Credentials>
<Username>my_username</Username>
<Password>my_password</Password>
</Credentials>
<Criterions>
<OrderNumber></OrderNumber>
<StartDate>2009-01-01</StartDate>
<EndDate>2009-07-01</EndDate>
</Criterions>
</GetOrdersIds>';
$xml = new SimpleXMLElement($xmlSTR);
echo $xml->Credentials->Username;
//To see the entire structure print_r($xml); as php can access objects as arrays
Have a look at the simplexml module.
Use an XML parser like SimpleXML to extract that data:
$doc = simplexml_load_string($_POST['xml']);
$OrderNumber = $doc->Criterions->OrderNumber;
$StartDate = $doc->Criterions->StartDate;
$EndDate = $doc->Criterions->EndDate;
Assuming your XML string is stored in the variable called $text_all (as you show in your example), this will get you what you want man... :-)
$mysql_stuff = '';
$xml = new SimpleXMLElement($text_all);
foreach($xml->Criterions->children() as $criterion) {
$mysql_stuff .= $criterion->asXML()."\n";
}
echo $mysql_stuff;
$mysql_stuff will now contain:
<OrderNumber/>
<StartDate>2009-01-01</StartDate>
<EndDate>2009-07-01</EndDate>
I tested this and it works. I hope this answers your question!