2 Replies - 488 Views - Last Post: 31 May 2012 - 07:02 AM Rate Topic: -----

#1 Metzzger  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 30-May 12

PHP XML Parsing

Posted 30 May 2012 - 08:11 AM

I don't think there could be a one answer for this, but any advice on what could be going on here would be awesome.

I'm editing a Wordpress PHP Plugin to fit my site more specifically. It already makes posts to a site automatically as an RSS feed updates. I'm also working towards combining multiple feeds with Yahoo Pipes. Right now, giving the plugin the RSS that's been in use for a little while or a locally generated XML in the same format correctly makes the posts to the site. However, Yahoo Pipes after conversion doesn't do the trick.

The plugin is specifically for posting coupons. It creates a table.

		$table_name = $wpdb->prefix . "couponfeeder";

		$sql =  "CREATE TABLE " . $table_name . " (
			id mediumint(9) NOT NULL AUTO_INCREMENT,
			post_id text NOT NULL,
			couponid text NOT NULL,
			expdate text NOT NULL,
			image text NOT NULL,
			link text NOT NULL,
			cfdesc text NOT NULL,
			majcat text NOT NULL,
			mincat text NOT NULL,
			brand text NOT NULL,
			value text NOT NULL,
			UNIQUE KEY id (id));";


Then it gets the XML file from a URL and extracts values.

		$xml = simplexml_load_file($url);

		$size = sizeof($xml->item);

		$coupon_count = 0;

		for ($loop = 0; $loop < $size; $loop++) {

			$couponid = $xml->item[$loop]->couponid;
			$link = $xml->item[$loop]->link;
			$description = htmlspecialchars($xml->item[$loop]->description, ENT_QUOTES);
			$image = $xml->item[$loop]->image;
			$activedate = $xml->item[$loop]->activedate;
			$shutoff = $xml->item[$loop]->shutoff;
			$expiration = $xml->item[$loop]->expiration;
			$majorCategory = $xml->item[$loop]->majorCategory;
			$minorCategory = $xml->item[$loop]->minorCategory;
			$brand = htmlspecialchars($xml->item[$loop]->brand, ENT_QUOTES);
			$value = $xml->item[$loop]->value;
			$geotarget = $xml->item[$loop]->geotarget;

			$sql = 'SELECT COUNT(*) FROM '.$wpdb->prefix.'couponfeeder WHERE couponid = '.$couponid;

			$coupon_posted = $wpdb->get_var($wpdb->prepare($sql));

			$exp_notime = reset(explode(' ', $expiration));
			$exp_year = end(explode('/', $exp_notime));
			$exp_month = reset(explode('/', $exp_notime));
			$exp_day = array_search(1,array_flip(explode('/', $exp_notime)));
			$exp_date = $exp_year.'-'.$exp_month.'-'.$exp_day;
			$exp_timestamp = strtotime($exp_date);


It then makes some checks to see if a couponid is already in the system, appends the data to a post, and submits it.

So...

Yahoo Pipes automatically strips RSS tags to conform it to standards, so I'm outputting it as serialized PHP then converting it with PEAR XML Serializer.

		$url = 'http://pipes.yahoo.com/pipes/pipe.run?_id=..............&_render=php';

		$response = file_get_contents($url);
		
		if ($response === false) {
			die('Request failed');
		}
		
		// Convert serialized PHP to XML
		$phpobj = unserialize($response);
		$xmlobj = $this->array_to_xml($phpobj);

	function array_to_xml($array) {

		 $options = array (
		  'addDecl' => TRUE,
		  'encoding' => 'iso-8859-1',
		  'indent' => '  ',
		  'rootName' => 'coupons',
		  'mode' => 'simplexml',
		  'defaultTagName' => 'item'
		);
		
		$serializer = new XML_Serializer($options);

		if ($serializer->serialize($array)) {
			return $serializer->getSerializedData();
		}
		else {
			return null;
		}
	}


And then there's a block of preg_replace() and str_replace() functions that hack up the resulting XML to get it in exactly the same format as the originally used RSS. But the new resulting $xml value does not get the plugin to post.

Any thoughts on what in the conversion might be preventing this?

Is This A Good Question/Topic? 0
  • +

Replies To: PHP XML Parsing

#2 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3037
  • View blog
  • Posts: 10,613
  • Joined: 08-August 08

Re: PHP XML Parsing

Posted 30 May 2012 - 08:23 AM

Yikes! Are you creating multiple tables with the same fields??? Fix that before moving on.
Was This Post Helpful? 0
  • +
  • -

#3 Metzzger  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 30-May 12

Re: PHP XML Parsing

Posted 31 May 2012 - 07:02 AM

I'm not quite sure what's up with that as I didn't write the original plugin, but it can correctly make posts as is from local XML files.

If I take what's been converted by XML Serializer and save it locally, then input that new file to the plugin, it works properly. I guess PHP is probably seeing the XML as a string instead at the moment.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1