I'm trying to do some feed submission for MWS.
I have a db that I need to make a call from.
dbCall functiion and $sql is the sql that calls the variable from customer database.
$var = dbCall($sql);
for ($x = 0; $x < 1; $x++ )
{
feed[] = "<Message>
<MessageID>".$messageID."</MessageID>
<OperationType>Update</OperationType>
<Product>
<SKU>book_".$var[$x]."</SKU>
<StandardProductID>
<Type>ASIN</Type>
<Value>".$var[$x]."</Value>
</StandardProductID>
<Condition>
<ConditionType>New</ConditionType>
<ConditionNote>Brand New! Never used!</ConditionNote>
</Condition>
</Product>
</Message>";
$messageID = $messageID + 1;
}
$total_feed = implode(" ", $feed);
$final_feed = '<?xml version="1.0" encoding="utf-8"?>
<AmazonEnvelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="amznenvelope.xsd">
<Header>
<DocumentVersion>1.01</DocumentVersion>
<MerchantIdentifier>'.$merch_id.'</MerchantIdentifier>
</Header>
<MessageType>Product</MessageType>
<PurgeAndReplace>false</PurgeAndReplace>' .$total_feed.'</AmazonEnvelope>');
`
When I run this; I get the string back but some of the tags don't align properly (see SKU and value).
How do I get an XML output from database variables?
Edit:
When I print the output for implode(" ",$feed) - all my tags seem lower case, but if I use htmlspecialchars or htmlentities, I get them in correct format.
<message> instead of <Message>
using trim adjusted the xml correctly.
Related
I'm retrieving an XML code from the database and storing it as a variable in php. However when I try to use this variable inside an XML code, it gives me an error. Here is my code:
<?php
$conn = mysqli_connect('localhost', 'root', '', 'thisdatabase');
$result = mysqli_query($conn, 'SELECT * FROM createdproduct');
while ($row = mysqli_fetch_assoc($result))
{
//$selected = (isset($_POST['list']) && $_POST['list'] == $row['id']) ? 'selected' : '';
//echo htmlentities($row["productURL"]);
$test = htmlspecialchars($row["productURL"]);
}
//echo htmlentities($test);
?>
and this is my XML wrapped in php:
<?php
//some code
$xml = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<order xmlns:xlink="http://www.w3.org/1999/xlink" xmlns="http://api.mydomain.net">
<orderItems>
'.$test.'
</orderItems>
<payment>
<type>typ</type>
</payment>
<shipping>
<shippingType id="00"/>
<address type="private">
<person> <
<salution id="1"/>
<firstName>person</firstName>
<lastName>person</lastName>
</person>
<street>street</street>
<houseNumber>00</houseNumber>
<city>city</city>
<country code="US">USA</country>
<state code="mm">California</state>
<zipCode>111</zipCode>
<email>aaa#mydomain.net</email>
<phone>+49 341 789 123</phone>
<fax>+49 341 789 123</fax>
</address>
</shipping>
</order>';
//some code
?>
Your error code is rather generic but I suspect the issue is in the data you're pulling from the database and using in the $test variable.
As discussed above, you might try confirming this by using a simple word in place of the actual data, like: $test = 'something';. The reason I believe this to be the problem is because htmlentities() is intended for use in HTML, not XML.
You might consider trying htmlspecialchars() instead.
EDIT: I knew I had read this on SO somewhere, check this post out for further detailed information.
I was able to get the result I wanted using html_entity_decode. Thank you
I've been trying to get the id field to pull and have no idea where I'm going wrong. The rest of the data pulls correctly but I'm trying to add something new to some existing code and everything I've tried hasn't worked. Below is my XML and the PHP code I've been working off of.
I haven't worked with a combo of xml and php before so I could really use a push in the right direction.
<?xml version="1.0" encoding="UTF-8"?>
<enterprise>
<person>
<sourcedid>
<source>Spirit Awards</source>
<id>SP8675309</id>
</sourcedid>
<userid>...</userid>
<name>
<fn>...</fn>
</name>
<email>...</email>
</person>
PHP code:
function get_userid(){
return $this->uid;
}
function __construct($xmlData){
$this->uid = (string)$xmlData->id;
}
SimpleXMLdocs makes this ... well ... simple. The XML you've posted is missing a closing </enterprise> tag. Assuming the XML you're actually parsing includes the closing tag, consider:
$str = '<?xml version="1.0" encoding="UTF-8"?>
<enterprise>
<person>
<sourcedid>
<source>Spirit Awards</source>
<id>SP8675309</id>
</sourcedid>
<userid>...</userid>
<name>
<fn>...</fn>
</name>
<email>...</email>
</person>
</enterprise>
';
$xml = simplexml_load_string($str);
$var = (string) $xml->person->sourcedid->id;
echo $var; // outputs: SP8675309
function xmlParse() {
$fh = fopen('schools/' . $this->id . '/books/school_books.xml', 'a');
$xmlstr = "<?xml version='1.0' ?>\n" .
"<rows></rows>";
// create the SimpleXMLElement object with an empty <book> element
$xml = new SimpleXMLElement($xmlstr);
$x = 0;
// add some more child nodes
for ($i = 0; $i < sizeof($this->students); $i++) {
$this->students[$i]->getmyBooks();
for ($j = 0; $j < sizeof($this->students[$i]->myBooks); $j++) {
$row = $xml->addChild("row");
$row->addAttribute("id", $x);
$row->addChild("cell", $this->students[$i]->myBooks[$j]->owner);
$row->addChild("cell", $this->students[$i]->myBooks[$j]->title);
$row->addChild("cell", $this->students[$i]->myBooks[$j]->category);
$row->addChild("cell", $this->students[$i]->myBooks[$j]->price);
$row->addChild("cell", $this->students[$i]->myBooks[$j]->description);
$row->addChild("cell", "Test");
$x++;
fwrite($fh, $xml->asXML());
}
}
}
I know what the problem is: its fwrite($fh, $xml->asXML());
if I keep calling that within the loop it doesn't append it keeps starting the xml document from scratch and posting the tags again.
My Problem is that it keeps writing from all over again the xml tags... instead of just continuing with the xml. If i do it for just 1 student it works perfect, but instead when I try to loop through all my students it keeps printing the xml tags instead of continuing on to the next student.
<?xml version="1.0"?>
<rows>
<row id="0">
<cell>Owner</cell>
<cell>test</cell>
<cell>Math</cell>
<cell>11</cell>
<cell>test</cell>
<cell>Test</cell>
</row>
</rows>
<?xml version="1.0"?>
continues with the next one then it does xml tags again and again.
This is how its to look like for one student:
<rows>
<row id="0">
<cell>Owner</cell>
<cell>Calculus III</cell>
<cell>Math</cell>
<cell>82</cell>
<cell>This book is in great condition! Available asap.</cell>
<cell>Test</cell>
</row>
<row id="1">
<cell>Owner</cell>
<cell>Discrete Mathematics</cell>
<cell>Math</cell>
<cell>62</cell>
<cell>This book is in poor condition.</cell>
<cell>Test</cell>
</row>
<row id="2">
<cell>Owner</cell>
<cell>Calculus I</cell>
<cell>Math</cell>
<cell>12</cell>
<cell>Really good book.</cell>
<cell>Test</cell>
</row>
</rows>
You're really looking for file_put_contents($name, $contents). That function adds all of the content en masse, so you would call it once at the end of the loop.
The code might look like:
// after i >= sizeof($this->students)
file_put_contents('schools/' . $this->id . '/books/school_books.xml',
$xml->asXML());
fwrite on the other hand, appends a file every single time it is called. This means that it will add the contents of the XML to the file sizeof($this->students) times, which is what you're seeing right now.
By the way, depending on the size of sizeof($this->students), you may want to declare a local variable to cache that before you look, sizeof it will be called every time.
$studentSize = sizeof($this->students);
for ($i = 0; $i < $studentSize; $i++) {
On the other hand, you might want to change that to a foreach loop (can't describe how at the moment but I'll add that in later if I remember).
I've been tearing my hair out with this now for a few hours and thought I'd post it up here to see if anybody had any suggestions.
Basically I am receving some XML date via SOAP/Curl call which looks like this:
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<LocationAvailabilityResponse xmlns="">
<getAvailabilityReturn>
<errors />
<requestID>389851</requestID>
<hotels>
<hotels>
<hotel>
<apt>false</apt>
<distance>0</distance>
<fromPrice>18.5</fromPrice>
<hotelName>Britannia Hotel Stockport</hotelName>
<id>5165</id>
<images>
<images>
<hasThumbnail>true</hasThumbnail>
<height>187</height>
<thumbnailHeight>50</thumbnailHeight>
<thumbnailURL>http://static.superbreak.net/content/images/Hotel/thumbs/britannia_hotel_stockport_swimming_pool_1_swi_5165.JPG</thumbnailURL>
<thumbnailWidth>68</thumbnailWidth>
<title>Britannia Hotel Stockport</title>
<url>http://static.superbreak.net/content/images/Hotel/britannia_hotel_stockport_swimming_pool_1_swi_5165.JPG</url>
<width>257</width>
</images>
<images>
<hasThumbnail>false</hasThumbnail>
<height>187</height>
<thumbnailHeight>0</thumbnailHeight>
<thumbnailURL>http://static.superbreak.net/content/images/Hotel/thumbs/britannia_hotel_stockport_swimming_pool_2_swi_5165.JPG</thumbnailURL>
<thumbnailWidth>0</thumbnailWidth>
<title>Swimming Pool</title>
<url>http://static.superbreak.net/content/images/Hotel/britannia_hotel_stockport_swimming_pool_2_swi_5165.JPG</url>
<width>257</width>
</images>
<images>
<hasThumbnail>false</hasThumbnail>
<height>187</height>
<thumbnailHeight>0</thumbnailHeight>
<thumbnailURL>http://static.superbreak.net/content/images/Hotel/thumbs/britannia_hotel_stockport_hotel_entrance_1_ent_5165.JPG</thumbnailURL>
<thumbnailWidth>0</thumbnailWidth>
<title>Hotel Entrance</title>
<url>http://static.superbreak.net/content/images/Hotel/britannia_hotel_stockport_hotel_entrance_1_ent_5165.JPG</url>
<width>257</width>
</images>
<images>
<hasThumbnail>false</hasThumbnail>
<height>187</height>
<thumbnailHeight>0</thumbnailHeight>
<thumbnailURL>http://static.superbreak.net/content/images/Hotel/thumbs/britannia_hotel_stockport_hotel_gym_1_gym_5165.JPG</thumbnailURL>
<thumbnailWidth>0</thumbnailWidth>
<title>Hotel Gym</title>
<url>http://static.superbreak.net/content/images/Hotel/britannia_hotel_stockport_hotel_gym_1_gym_5165.JPG</url>
<width>257</width>
</images>
<images>
<hasThumbnail>false</hasThumbnail>
<height>187</height>
<thumbnailHeight>0</thumbnailHeight>
<thumbnailURL>http://static.superbreak.net/content/images/Hotel/thumbs/britannia_hotel_stockport_hotel_lounge_1_lou_5165.JPG</thumbnailURL>
<thumbnailWidth>0</thumbnailWidth>
<title>Hotel Lounge</title>
<url>http://static.superbreak.net/content/images/Hotel/britannia_hotel_stockport_hotel_lounge_1_lou_5165.JPG</url>
<width>257</width>
</images>
<images>
<hasThumbnail>false</hasThumbnail>
<height>187</height>
<thumbnailHeight>0</thumbnailHeight>
<thumbnailURL>http://static.superbreak.net/content/images/Hotel/thumbs/britannia_hotel_stockport_four_poster_bedroom_1_pst_5165.JPG</thumbnailURL>
<thumbnailWidth>0</thumbnailWidth>
<title>Four Poster Bedroom</title>
<url>http://static.superbreak.net/content/images/Hotel/britannia_hotel_stockport_four_poster_bedroom_1_pst_5165.JPG</url>
<width>257</width>
</images>
</images>
<latitude>53.398941</latitude>
<location>Stockport</location>
<longitude>-2.13463</longitude>
<starRating>3</starRating>
</hotel>
<roomUnits>
<roomUnits>
<allocation>1</allocation>
<boardCode>RO</boardCode>
<boardDescription>Room only</boardDescription>
<maxOccupancy>2</maxOccupancy>
<minOccupancy>1</minOccupancy>
<price>18.5</price>
<stdOccupancy>2</stdOccupancy>
<unitDescription>Double For 1-2</unitDescription>
<unitID>162</unitID>
</roomUnits>
<roomUnits>
<allocation>1</allocation>
<boardCode>RO</boardCode>
<boardDescription>Room only</boardDescription>
<maxOccupancy>2</maxOccupancy>
<minOccupancy>1</minOccupancy>
<price>18.5</price>
<stdOccupancy>2</stdOccupancy>
<unitDescription>Twin For 1-2</unitDescription>
<unitID>161</unitID>
</roomUnits>
<roomUnits>
<allocation>1</allocation>
<boardCode>RO</boardCode>
<boardDescription>Room only</boardDescription>
<maxOccupancy>2</maxOccupancy>
<minOccupancy>2</minOccupancy>
<price>23.5</price>
<stdOccupancy>2</stdOccupancy>
<unitDescription>Executive Double Room</unitDescription>
<unitID>65</unitID>
</roomUnits>
<roomUnits>
<allocation>1</allocation>
<boardCode>RO</boardCode>
<boardDescription>Room only</boardDescription>
<maxOccupancy>2</maxOccupancy>
<minOccupancy>2</minOccupancy>
<price>23.5</price>
<stdOccupancy>2</stdOccupancy>
<unitDescription>Executive Twin Room</unitDescription>
<unitID>64</unitID>
</roomUnits>
</roomUnits>
</hotels>
I'm attempting to iterate through each hotels hotels result and turn each result into a multi dimensional array. The code I'm using which isn't working as I'd like is below:
$doc = new DOMDocument();
if ($doc->loadXML($result)) {
$items = $doc->getElementsByTagName('hotels');
$hotelnames = array();
foreach($items as $item) {
$hotelname = array();
$hotelimages = array();
if($item->childNodes->length) {
foreach($item->childNodes as $i) {
$hotelname[$i->nodeName] = $i->nodeValue;
if($i->childNodes->length){
foreach($i->childNodes as $z) {
if($z->childNodes->length){
foreach($z->childNodes as $x) {
$hotelimage[$x->nodeName] = $x->nodeValue;
}
}
}
}
$hotelimages[] = $hotelimage;
}
}
$hotelnames[] = $hotelname;
}
}
I'm guessing the issues I'm facing are mostly caused by the fact that the child and parent nodes are named the same for hotels and for the images.
Any help or a nod in the right direction will be much appreciated.
I suggest you using xpath (for example in SimpleXML implementation http://php.net/manual/en/simplexmlelement.xpath.php) for loading the values you need.
Or if you need whole XML parsed to array, you can always use PEAR XML_Serializer package (http://pear.php.net/package/XML_Serializer) to unserialize your XML.
Instead of working directly with the DOM I would recommend that you, unless you actually do need access to the DOM, perform these tasks using SimpleXML
It makes it very easy to work with XML data and you can act on it almost like a normal array.
Example
<?php
$url = 'http://www.flickr.com/services/feeds/photos_public.gne';
foreach(simplexml_load_file($url)->entry as $entry) {
echo $entry->content;
}
?>
Quite few lines for that functionality :)
Good luck!
I am looking in to the XML and I noticed some XML tags are not correct
Ex: <images><images></images><images></images></images> same is the case with roomunit.
I think it should be like <images><image></image><image></image></images> this will help to iterate over XML tag in php.
I'm using the simplexml extentsion and AMFPHP to send xml data to flash.
Say I have this xml:
<?xml version="1.0" encoding="ISO-8859-1"?>
<people>
<person>
<name>bob</name>
</person>
</people>
And I load it in with simplexml_load_file().
When I do this:
$name = $xml->person[0]->name;
return $name;
it returns "<name>bob</name>".
Why is it returning the tags? When I do this with just php and not AMFPHP it works fine.
I wanna know the answer to this too
Ok, i found out how to solve this :
You basically have to type it to a string, float, double, int, or whatever you need...
$xml = simplexml_load_file(XML_FILE_LOCATION);
$start = (double)$xml->layer[2]['start'];
//$title = (string)$xml->layer[2]['title'];
return $start;