I want to parse the xml recieved from the Kunaki API into a Drop Down Selection Box. I have this code so far but the box keeps remaining empty, so my question is this how do I achieve this? I keep getting an empty drop down selection box everytime.
<?php
$context = stream_context_create(array('http' => array('header' => 'Accept: application/xml')));
$url = 'http://kunaki.com/HTTPService.ASP?RequestType=ShippingOptions&State_Province=NY&PostalCode=11204&Country=United+States&ProductId=PX0012345&Quantity=1&ProductId=PX04444444&Quantity=1&ResponseType=xml ';
$xml = file_get_contents($url, false, $context);
$xml2 = simplexml_load_string($xml);
?>
<html>
<head>
</head>
<body>
<select>
<Option Value=<?php echo $value = (string)$xml2->Description[0]." Delivery Time ".(string)$xml2pt->DeliveryTime[0].(string)$xml2->Price[0];?></Option>
</select>
</body>
</html>
After some fiddling, I believe this is what you're after.
<?php
// Setup your context for file_get_contents
$oContext = stream_context_create(array("http" => array("header" => "Accept: application/xml")));
// Put in your URL here to get the XML from
$sURL = "http://kunaki.com/HTTPService.ASP?RequestType=ShippingOptions&State_Province=NY&PostalCode=11204&Country=United+States&ProductId=PX0012345&Quantity=1&ProductId=PX04444444&Quantity=1&ResponseType=xml";
// Parse the XML
$oXML = simplexml_load_string(file_get_contents($sURL, false, $oContext));
// Grab the data that we need from the XML
$pOptions = isset($oXML->Option) && isset($oXML->Option[0]) ? $oXML->Option : array("error" => true);
// Display everything to the page
echo "<html>";
echo "<head>";
echo "<title>XML Test</title>";
echo "</head>";
echo "<body>";
// If there was an error, display it to the page
if(isset($pOptions["error"]))
{
echo "<h3>Sorry, wrong data returned.";
var_dump($pOptions);
}
else
{
// Loop through the array and display the dropdown
echo "<select>";
for($i = 0, $iCount = count($pOptions); $i < $iCount; ++$i)
{
$sString = $pOptions[$i]->Description.", ";
$sString .= "Delivery Time: ".$pOptions[$i]->DeliveryTime." ";
$sString .= "($".$pOptions[$i]->Price.")";
echo "<option>".$sString."</option>";
}
echo "</select>";
}
echo "</body>";
echo "</html>";
?>
I reformatted the display of the dropdown text a little bit so that it was easier to read. In this example, it makes this dropdown:
<select>
<option>
USPS First Class Mail, Delivery Time: 2-5 business days ($0.66)
</option>
<option>
UPS Ground, Delivery Time: 1-5 business days ($17.17)
</option>
<option>
UPS 2nd Day Air, Delivery Time: 2 business days ($30.42)
</option>
<option>
UPS Next Day Air Saver, Delivery Time: 1 business day ($50.17)
</option>
</select>
One of your issues was, you were putting the data only into the value attribute of the dropdown items.
Example:
<option value="test"></option>
Which would only display:
<select>
<option value="test"></option>
</select>
So what you were after, was something more like this:
<select>
<option>Test</option>
</select>
Other than that, you were only trying to display the first returned option, not all of them. That's where that for() loop comes into play, it will loop through and display all of the returned shipping options, irregardless of how many there are.
Hope this helps!