troubleshooting Question

Trying to parse xml into database, but it stops.

Avatar of kgp43
kgp43Flag for Denmark asked on
PHP
9 Comments1 Solution333 ViewsLast Modified:
Hi,

I have a small problem with this function.
It need to parse an xml file and put the required details into my database, but it stops.

I know the reason, but don't know why my coding does not work (think it should)... and it's driving me insane.

# SMS XML FEED
function sms_xml() {
	
	$xml_url = '.....';

	# load the xml file
	$xml_data = file_get_contents($xml_url);

	# parse the file into an SimpleXML object
	$xml = new SimpleXMLElement($xml_data);

	# Create array with USD exchange rates
	$USD_exchange_rate = array();
	
	$USD_query = mysql_query("SELECT * FROM currency_exchange_rates WHERE base='USD' ") or die(mysql_error());
	while($USD_fetch = mysql_fetch_array($USD_query)) {

		$USD_exchange_rate[$USD_fetch['country']] = $USD_fetch['value'];
	}
	
	# let's fill the country array
	foreach ($xml->service->countries->country as $country) {
		$operators = array();
		
		# skip countries, that have not been approved yet
		//if ($country['approved'] == 'false') continue;
		
		# get all pricepoints, message profiles and operators
		foreach ($country->prices->price as $price_level) {
		
			$price = reset($price_level['amount']); // make array into string
			$currency = reset($price_level['currency']); // make array into string
			
			foreach ($price_level->message_profile as $profile) {
				$keyword = $profile['keyword'];
				$shortcode = $profile['shortcode'];
				
				foreach ($profile->operator as $operator) {
					$name = $operator['name'];
					$revenue = floatval($operator['revenue']);
					$billing_type = $operator['billing_type'];
					
					# PRICE
					$price_USD = round($price/$USD_exchange_rate[$currency], 2);
					
					# REVENUE
					$revenue_USD = round($revenue/$USD_exchange_rate[$currency], 2);
					
					# Insert operators into table
					mysql_query("INSERT INTO sms_operators (id, name, country, country_code, price, price_USD, revenue, revenue_USD, currency, billing_type, keyword, shortcode)
						values ('', '$name', '{$country['name']}', '{$country['code']}', '$price', '$price_USD', '$revenue', '$revenue_USD', '$currency', '$billing_type', '$keyword', '$shortcode' )") or die(mysql_error());
				}
			}
		}
		
		# Insert countries into table
		mysql_query("INSERT INTO sms_countries (id, code, name, vat, approved, text)
			values ('', '{$country['code']}', '{$country['name']}', '{$country['vat']}', '{$country['approved']}', '{$country->promotional_text->local}' )") or die(mysql_error());
	}
}

My function stops (nothing in the database) right before Russia.
The reason is probably the different prices (countries before Russia only have 1 price).

XML:
<services_api_response version="2.0">
	<status>
		<code>0</code>
		<message>OK</message>
	</status>
	<service id="gdfg445454hhggddfgf">
		<countries>
			<country code="RU" vat="18.00" name="Russia" approved="false">
				<prices>
					<price amount="168.99" all_operators="false" vat_included="true" currency="RUB">
						<message_profile all_operators="false" shortcode="1111" keyword="FOR DOWNLOAD">
							<operator code="Ul'yanovsk GSM" billing_type="MO" default_billing_status="OK" revenue="63.01" name="Ul'yanovsk GSM"/>
							<operator code="BaicalWestCom" billing_type="MO" default_billing_status="OK" revenue="63.01" name="BaicalWestCom"/>
							<operator code="Tele2 RU" billing_type="MO" default_billing_status="OK" revenue="71.61" name="Tele2 RU"/>
							<operator code="Megafon" billing_type="MO" default_billing_status="OK" revenue="63.01" name="Megafon"/>
							<operator code="MTS" billing_type="MO" default_billing_status="OK" revenue="60.15" name="MTS"/>
						</message_profile>
					</price>
					<price amount="169.00" all_operators="false" vat_included="true" currency="RUB">
						<message_profile all_operators="false" shortcode="1111" keyword="FOR DOWNLOAD">
							<operator code="Yaroslavl' GSM" billing_type="MO" default_billing_status="OK" revenue="57.29" name="Yaroslavl' GSM"/>
						</message_profile>
					</price>
					<price amount="170.00" all_operators="false" vat_included="true" currency="RUB">
						<message_profile all_operators="false" shortcode="1111" keyword="FOR DOWNLOAD">
							<operator code="Beeline" billing_type="MO" default_billing_status="OK" revenue="67.71" name="Beeline"/>
							<operator code="Smarts" billing_type="MO" default_billing_status="OK" revenue="25.93" name="Smarts"/>
						</message_profile>
					</price>
					<price amount="171.10" all_operators="false" vat_included="true" currency="RUB">
						<message_profile all_operators="false" shortcode="1111" keyword="FOR DOWNLOAD">
							<operator code="NTC" billing_type="MO" default_billing_status="OK" revenue="46.40" name="NTC"/>
						</message_profile>
					</price>
					<price amount="175.82" all_operators="false" vat_included="true" currency="RUB">
						<message_profile all_operators="false" shortcode="1111" keyword="FOR DOWNLOAD">
							<operator code="Motiv" billing_type="MO" default_billing_status="OK" revenue="59.60" name="Motiv"/>
							<operator code="Astrakhan' GSM" billing_type="MO" default_billing_status="OK" revenue="59.60" name="Astrakhan' GSM"/>
						</message_profile>
					</price>
				</prices>
				<promotional_text>
					<local>
						dfggfgdfgdgfg
					</local>
					<english>
						dfgdgfdggdfgdg
					</english>
				</promotional_text>
			</country>

(the xml code above is from my xml feed, other countries has been removed etc)
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 9 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 9 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros