Sending XML file through PHP remote url - php

The situation is as follows. I have to send a defined XML to an url http:\\www.example.com:1234 with some variables that I have to previously define.
XML is like this:
<Title1>
<Title2>Some Text</Title2>
<Title3>Variable 1</Title3>
<Title4>Some Text</Title4>
<Title5>
<Title51>Variable 2</Title51>
</Title5>
</Title1>
But, I want to define those variables (1, 2) within an html/php form and get method, so the user can introduce both variables and then click on the submit button from the form to send the XML to the previously URL.
Also, XML should have the "Content-Type","application/x-www-form-urlencoded" header.
Is this possible? I've tried to pass these variables directly to the XML and the best that I've come to is to showing the XML and not parsing the php strings.
Also, I've tried some scripts like simplexml from PHP classes, but with no luck so far.

1) To modify existing XML with new values . Try this
sample.xml :
<Title1>
<Title2>Some Text</Title2>
<Title3>Variable 1</Title3>
<Title4>Some Text</Title4>
<Title5>
<Title51>Variable 2</Title51>
</Title5>
</Title1>
PHP :
$xml = simplexml_load_file("sample.xml");
$xml->Title3 = $_GET['t3']; // Updating <Title3></Title3> from GET method
$xml->Title5[0]->Title51 = $_GET['t5']; // Updating <Title51></Title51> from GET method
$xml->asXML('sample.xml'); // saving the xml file
2)To create new XML file (sample.xml) :
PHP:
$xml = new SimpleXMLElement("<Title1></Title1>");
$xml->Title2='Some Text';
$xml->Title3 = $_GET['t3'];
$xml->Title4='Some Text';
$xml->Title5[0]->Title51 = $_GET['t5'];
$xml->asXML('sample.xml'); // saving the xml file
I have showed you both possibilities mentioned in the comment . Use anyone which comforts you :)

Related

Redirect to XML file and back to PHP page possible?

I'm currently coding an ID filter, whenever a user submits an ID it will pick out all the orders and filters out all the ID's you DON'T want to see from the XML file.
a little preview - XML
<filter><!-- Copy filter-item and put the order-id in as the value to skip it-->
<filter_item>1138850639</filter_item><filter_item>1138371172</filter_item><filter_item>1137835945</filter_item></filter>
</root>
PHP
if (isset($_POST['load']))
{
$orderFilter = $_POST['orderItemFilter'];
$xml = simplexml_load_file("Config.xml");
$sxe = new SimpleXMLElement($xml->asXML());
$itemsNode = $sxe->filter;
$itemsNode->addChild("filter_item", $orderFilter);
$sxe->asXML("Config.xml");
}
Now the problem is that the file itself does contain the ID, but the Config.xml HAS TO BE RUNNED to be able to display the final order page. Is there a way to redirect from an XML file to return to the previous page?
There are two mentioned to do this.
First you can do this in Javascript.
javascript:history.go(-1)
Second, you can do this in PHP, remember it is not ggood idea to because it will not work on HTTPS and headers can be hijacked and reirect to unwanted pages.
header('Location: ' . $_SERVER['HTTP_REFERER']);

Creating XML in PHP saves a blank file

im new to XML Handling in PHP
i wrote this script to get variables from post and insert them into a tag with the name of the variable it self from the posted variables and the data the actual data inside this text fields
i have set $id_picture to foo instead of the posted data but with same result
$xml = new DOMDocument('1.0', 'UTF-8');
$id_picture = $_POST['id_picture'];
$xml_id_picture = $xml->createElement("id_picture");
$xml_id_picture_node = $xml->createTextNode($id_picture);
$xml_id_picture->appendChild($xml_id_picture_node);
//upload xml
$xml->save('xml.xml');
what im trying to achieve is save the data from the post to the first variable then i get lost on making it a xml tag and inserting the data in between
<id_picture>foo</id_picture>
You never inserted your new node into the main object. You need something like
$xml->appendChild($xml_id_picture);
so that your newly created id_picture node will actually show up in your document.
You have to add the node to the document.
$xml = new DOMDocument('1.0', 'UTF-8');
$id_picture = $_POST['id_picture'];
$xml_id_picture = $xml->createElement("id_picture");
$xml_id_picture_node = $xml->createTextNode($id_picture);
$xml_id_picture->appendChild($xml_id_picture_node);
$xml->appendChild($xml_id_picture);//<-- here
//upload xml
$xml->save('xml.xml');
http://codepad.org/6Ml2cUYe
It looks like you are creating an element, appending a node to it, but then you are not appending the element to the document.

How to Parse XML data to a PHP variable

I am mediocre with php and ignorant with XML...if you can be detailed it will help me learn.
I am attempting to use PHP programming to execute a scripts to this link... http://ws.geonames.org/postalCodeSearch?postalcode=VARIABLE_ZIP&country=US. The VARIABLE_ZIP is the actual zip code entered into the form that will submit the information in the link above. The output of that link creates an XML page that i do not want displayed anywhere on my website.
What I want to do is capture the XML data Latitude and Longitude values as variables within php and store them into a database.
1) I have a form
2) The user inputs their zip code into the form
3) upon submitting the form: I want code to capture the XML data generated by the link with the zip code variable added: http://ws.geonames.org/postalCodeSearch?postalcode=90210&country=US (I don't want this page to display anywhere, I just want to capture the data)
4) I want to take those variables back to PHP so I can store them in my database
*Step 3 is an unknown process for me.
This is what the XML code generates on a separate page...(i don't want a page to display):
= = = = =
<geonames>
<totalResultsCount>1</totalResultsCount>
<code><postalcode>90210</postalcode>
<name>Beverly Hills</name>
<countryCode>US</countryCode>
<lat>34.09011</lat>
<lng>-118.40648</lng>
<adminCode1>CA</adminCode1>
<adminName1>California</adminName1>
<adminCode2>037</adminCode2>
<adminName2>Los Angeles</adminName2>
<adminCode3/><adminName3/>
</code>
</geonames>
= = = = =
Someone provided me with the following but I am not sure how to execute it and parse the variables I need:
<?php
$pc = $_POST['postalcode'];
$c = $_POST['country'];
$xml = file_get_contents("http://ws.geonames.org/postalCodeSearch?postalcode={$pc}&country={$c}");
file_put_contents("geopost_{$pc}_{$c}.xml",$xml);
?>
Thank you for your assistance!
The best thing to do is to see how the XML is structured by navigating directly to the link (I used my own zipcode and country), and then navigate through the tree grabbing the data you need using SimpleXMLElement. This example grabs the Latitude and Longitude:
<?php
$pc = $_POST['postalcode'];
$c = $_POST['country'];
$xml = new SimpleXMLElement(file_get_contents("http://ws.geonames.org/postalCodeSearch?postalcode=".$pc."&country=".$c));
$lat = $xml->code->lat;
$lng = $xml->code->lng;
echo "Lat: $lat<br/>Lng: $lng";
?>
This should help get you started with storing those values.
Step 3 is to process the xml with PHP which is outlined quite nicely with these Simple XML examples
Take a look at PHPs SimpleXML for how to parse and read the XML data received.

How do I serialize DOM into XML using PHP?

I am attempting to send html data in a question form from my php web application to mechanical turk so a user can see the entire html document from an email to work with. I have had difficulty thus far. In the thread linked below, I attempted to parse the html data using html5-lib.php, but I think I'm still missing a step in order to complete this.
Here's the current error I'm receiving:
Catchable fatal error: Object of class DOMNodeList could not be converted to string in urlgoeshere.php on line 35
Here's the current code I'm working with...
$thequestion = 'click here';
$thequestion = HTML5_Parser::parseFragment($thequestion);
var_dump($thequestion);
echo $thequestion;
//htmlspecialchars($thequestion);
$QuestionXML = '<QuestionForm xmlns="http://mechanicalturk.amazonaws.com/AWSMechanicalTurkDataSchemas/2005-10-01/QuestionForm.xsd">
<Question>
<QuestionContent>
<Text>'.$thequestion.'</Text> //<--- Line35
</QuestionContent>
<AnswerSpecification>
<FreeTextAnswer/>
</AnswerSpecification>
</Question>
</QuestionForm> ';
I'm not 100% sure if the parser is what I need to do in order to have this sent correctly - All I want to do is send html through this xml type document - I'm surprised it's been this difficult thus far.
This is somewhat of a continuation of another thread -
What PHP code will help me parse html data in an xml form?
Take a look at DOMDocument for working with DOM/xml in PHP. If you want to embed HTML in your XML, use CDATA sections like this:
$QuestionXML = '<QuestionForm xmlns="http://mechanicalturk.amazonaws.com/AWSMechanicalTurkDataSchemas/2005-10-01/QuestionForm.xsd">
<Question>
<QuestionContent>
<Text><![CDATA['.$thequestion.']]></Text>
</QuestionContent>
<AnswerSpecification>
<FreeTextAnswer/>
</AnswerSpecification>
</Question>
</QuestionForm> ';
Not sure exactly what you're after. This is how i would create the XML needed to be transferred.
Please let me know if i misunderstood the question
It also seems that you are missing the QuestionIdentifier node which is mandatory according to the .xsd file.
<?
$dom = new DOMDocument('1.0','UTF-8');
$dom->formatOutput = true;
$QuestionForm = $dom->createElement('QuestionForm');
$QuestionForm->setAttribute('xmlns','http://mechanicalturk.amazonaws.com/AWSMechanicalTurkDataSchemas/2005-10-01/QuestionForm.xsd');
// could loop this part for all the questions of the XML
$thequestion = 'click here';
//Not sure what this is supposed to be, but its required. Check the specs of the app for it.
$questionID = "";
$Question = $dom->createElement('Question');
$QuestionIdentifier = $dom->createElement('QuestionIdentifier',$questionID);
$QuestionContent = $dom->createElement('QuestionContent');
$QuestionContent->appendChild($dom->createElement('Text',$thequestion));
$AnswerSpecification = $dom->createElement('AnswerSpecification');
$AnswerSpecification->appendChild($dom->createElement('FreeTextAnswer'));
$Question->appendChild($QuestionIdentifier);
$Question->appendChild($QuestionContent);
$Question->appendChild($AnswerSpecification);
$QuestionForm->appendChild($Question);
// End loop
$dom->appendChild($QuestionForm);
$xmlString = $dom->saveXML();
print($xmlString);
?>

Updating the XML file using PHP script

I'm making an interface-website to update a concert-list on a band-website.
The list is stored as an XML file an has this structure :
I already wrote a script that enables me to add a new gig to the list, this was relatively easy...
Now I want to write a script that enables me to edit a certain gig in the list.
Every Gig is Unique because of the first attribute : "id" .
I want to use this reference to edit the other attributes in that Node.
My PHP is very poor, so I hope someone could put me on the good foot here...
My PHP script :
Well i dunno what your XML structure looks like but:
<gig id="someid">
<venue></venue>
<day></day>
<month></month>
<year></year>
</gig>
$xml = new SimpleXmlElement('gig.xml',null, true);
$gig = $xml->xpath('//gig[#id="'.$_POST['id'].'"]');
$gig->venue = $_POST['venue'];
$gig->month = $_POST['month'];
// etc..
$xml->asXml('gig.xml)'; // save back to file
now if instead all these data points are attributes you can use $gig->attributes()->venue to access it.
There is no need for the loop really unless you are doing multiple updates with one post - you can get at any specific record via an XPAth query. SimpleXML is also a lot lighter and a lot easier to use for this type of thing than DOMDOcument - especially as you arent using the feature of DOMDocument.
You'll want to load the xml file in a domdocument with
<?
$xml = new DOMDocument();
$xml->load("xmlfile.xml");
//find the tags that you want to update
$tags = $xml->getElementsByTagName("GIG");
//find the tag with the id you want to update
foreach ($tags as $tag) {
if($tag->getAttribute("id") == $id) { //found the tag, now update the attribute
$tag->setAttribute("[attributeName]", "[attributeValue]");
}
}
//save the xml
$xml->save();
?>
code is untested, but it's a general idea

Categories