0

Hello guys,

I am trying to learn working with Web services using PHP. I have found many methods to access and consume these services, but I've come to know Soap Client and cURL are the best way to do it. I decided to work on an API provided by a UK Money Saving Forum 'HUKD'

I used both (SOAP & cURL) and successfully retrieved data in XML format. which is pretty neat, so far. I get the following XML as a response. (don't be afraid its just a list of data)

file.xml

(I have attached the XML and not pasted here because of its really large text of 1000 something lines and its annoying to scroll down).

Now, this is a filtered response. My response can have many 'api_item' I found that a bit challenging to work with and handle.

So this is what I did.

<?php
include_once 'api_item.php';
$api_item[] = new api_item();
$file= 'file.xml';
$xml = simplexml_load_file($file);
$api_item[0]->put_title($xml->children()->children()->children()->__toString());
?>

and it gives me this
response.txt

the response is an array of all the xml data.

so I decided to create a class and I've planned to fetch the data and get the 'api_item' data from fetched data and dump it into the class array. (its just a theory. Is it something we can do?)

this is what I designed

<?php

class api_item{

    private $title;
    private $deal_link;
    private $mobile_deal_link;
    private $deal_image;
    private $description;
    private $submit_time;
    private $hot_time;
    private $poster_name;
    private $temperature;
    private $price;
    private $timestamp;
    private $expired;
    private $deal_image_highres;

    function get_title(){
        return $title;
    }
    function get_deal_link(){
        return $deal_link;
    }
    function get_mobile_deal_link(){
        return $mobile_deal_link;
    }
    function get_deal_image(){
        return $get_deal_image;
    }
    function get_description(){
        return $description;
    }
    function get_submit_time(){
        return $submit_time;
    }
    function get_hot_time(){
        return $hot_time;
    }
    function get_poster_name(){
        return $poster_name;
    }
    function get_temperature(){
        return $temperature;
    }
    function get_price(){
        return $price;
    }
    function get_timestamp(){
        return $timestamp;
    }
    function get_expired(){
        return $expired;
    }
    function get_deal_image_highres(){
        return $deal_image_highres;
    }

    function put_title($var_title){
        $title = $var_title;
    }   
    function put_deal_link($var_deal_link){
        $deal_link = $var_deal_link;
    }
    function put_mobile_deal_link($var_mobile_deal_link){
        $mobile_deal_link = $var_mobile_deal_link; 
    }
    function put_deal_image($put_deal_image){
        $deal_image = $var_deal_image;
    }
    function put_description($var_description){
        $description = $var_description;
    }
    function put_submit_time($var_submit_time){
        $submit_time = $var_submit_time;
    }
    function put_hot_time($var_hot_time){
        $hot_time = $var_hot_time;
    }
    function put_poster_name($var_poster_name){
        $poster_name = $var_poster_name;
    }
    function put_temperature($var_temperature){
        $temperature = $var_temperature;
    }
    function put_price($var_price){
        $price = $var_price;
    }
    function put_timestamp($var_timestamp){
        $timestamp = $var_timestamp;
    }
    function put_expired($var_expired){
        $expired = $var_expired;
    }
    function put_deal_image_highres($var_deal_image_highres){
        $deal_image_highres = $var_deal_image_highres;
    }
}

?>

now how do I traverse through the result and dump my array data into the class??

weird ideas my brain gives me :S

Attachments
object(SimpleXMLElement)[4]
  public 'api_item' => 
    array
      0 => 
        object(SimpleXMLElement)[3]
          public 'title' => string '
				The Verve Urban Hymns CD Preowned 1 at pound land!
			' (length=61)
          public 'deal_link' => string '
				http://www.hotukdeals.com/deals/verve-urban-hymns-cd-preowned-1-pound-land-1143545?aui=487
			' (length=99)
          public 'mobile_deal_link' => string '
				http://m.hotukdeals.com/deals/verve-urban-hymns-cd-preowned-1-pound-land-1143545?aui=487
			' (length=97)
          public 'deal_image' => string '
				http://www.hotukdeals.com/images/threads/1143545_1.jpg
			' (length=63)
          public 'description' => string '
				Only just remembered to post this!

Was in Birmingham yesterday and got the Verve Urban Hymnes for 1!

Bitter sweet symphony is such an awesome track!

It was the store by new street.

It said pre owned on it, but mine looks brand new. Perhaps the disc has been resurfaced? For a pound I don't care!
			' (length=310)
          public 'submit_time' => string '
				14 hours, 39 minutes ago
			' (length=33)
          public 'hot_time' => string '
				11 minutes ago
			' (length=23)
          public 'poster_name' => string '
				thearbiter65
			' (length=21)
          public 'temperature' => string '
				115.489997864
			' (length=22)
          public 'price' => string '
				1
			' (length=10)
          public 'timestamp' => string '
				1329173213
			' (length=19)
          public 'expired' => string '
				false
			' (length=14)
          public 'forum' => 
            object(SimpleXMLElement)[24]
              ...
          public 'category' => 
            object(SimpleXMLElement)[25]
              ...
          public 'merchant' => 
            object(SimpleXMLElement)[26]
              ...
          public 'tags' => 
            object(SimpleXMLElement)[27]
              ...
          public 'deal_image_highres' => string '
				http://www.hotukdeals.com/images/threads/high-res/1143545_1.jpg
			' (length=72)
      1 => 
        object(SimpleXMLElement)[5]
          public 'title' => string '
				Uncharted 3: Drake's Deception (PS3) for 24.99 @ Play.Com
			' (length=68)
          public 'deal_link' => string '
				http://www.hotukdeals.com/deals/uncharted-3-drake-s-deception-ps3-for-24-99-play-com-1143647?aui=487
			' (length=109)
          public 'mobile_deal_link' => string '
				http://m.hotukdeals.com/deals/uncharted-3-drake-s-deception-ps3-for-24-99-play-com-1143647?aui=487
			' (length=107)
          public 'deal_image' => string 'http://www.hotukdeals.com/images/threads/1143647_1.jpg' (length=54)
          public 'description' => string '
				A search for the fabled "Atlantis of the Sands" propels fortune hunter Nathan Drake on a daring trek into the heart of the Arabian Desert, a journey that pits him and his mentor Victor Sullivan against the occult treachery of a shadowy clandestine organization and its ruthless leader. When the terrible secrets of this lost city are unearthed, Drake's quest descends into a desperate bid for survival that strains the limits of his endurance and forces him to confront his deepest fears.' (length=493)
          public 'submit_time' => string '5 hours, 20 minutes ago' (length=23)
          public 'hot_time' => string '16 minutes ago' (length=14)
          public 'poster_name' => string 'Syzable' (length=7)
          public 'temperature' => string '130.770004272' (length=13)
          public 'price' => string '24.99' (length=5)
          public 'timestamp' => string '1329206774' (length=10)
          public 'expired' => string 'false' (length=5)
          public 'forum' => 
            object(SimpleXMLElement)[30]
              ...
          public 'category' => 
            object(SimpleXMLElement)[31]
              ...
          public 'merchant' => 
            object(SimpleXMLElement)[32]
              ...
          public 'tags' => 
            object(SimpleXMLElement)[33]
              ...
          public 'deal_image_highres' => string 'http://www.hotukdeals.com/images/threads/high-res/1143647_1.jpg' (length=63)
      2 => 
        object(SimpleXMLElement)[6]
          public 'title' => string 'Kindle Keyboard Wifi Refurb 73.59 from Amazon Warehouse' (length=57)
          public 'deal_link' => string 'http://www.hotukdeals.com/deals/kindle-keyboard-wifi-refurb-73-59-from-amazon-warehouse-1143850?aui=487' (length=103)
          public 'mobile_deal_link' => string 'http://m.hotukdeals.com/deals/kindle-keyboard-wifi-refurb-73-59-from-amazon-warehouse-1143850?aui=487' (length=101)
          public 'deal_image' => string 'http://www.hotukdeals.com/images/threads/1143850_1.jpg' (length=54)
          public 'description' => string 'Dropped from 79. One year warranty.


				The one my mother got to replace her Kindle 4 that had a low contrast screen was indistinguishable from new.
			' (length=154)
          public 'submit_time' => string '
				1 hour, 22 minutes ago' (length=27)
          public 'hot_time' => string '
				30 minutes ago' (length=19)
          public 'poster_name' => string '
				pjk' (length=8)
          public 'temperature' => string '
				195.029998779' (length=18)
          public 'price' => string '
				73.59' (length=10)
          public 'timestamp' => string '
				1329221060' (length=15)
          public 'expired' => string '
				false' (length=10)
          public 'forum' => 
            object(SimpleXMLElement)[37]
              ...
          public 'category' => 
            object(SimpleXMLElement)[38]
              ...
          public 'merchant' => 
            object(SimpleXMLElement)[39]
              ...
          public 'tags' => 
            object(SimpleXMLElement)[40]
              ...
          public 'deal_image_highres' => string '
				http://www.hotukdeals.com/images/threads/high-res/1143850_1.jpg
			' (length=72)
      3 => 
        object(SimpleXMLElement)[7]
          public 'title' => string '
				Monster Munch Monster Sized bags only 39p @ Home Bargains
			' (length=66)
          public 'deal_link' => string '
				http://www.hotukdeals.com/deals/monster-munch-monster-sized-bags-only-39p-home-bargains-1143767?aui=487
			' (length=112)
          public 'mobile_deal_link' => string '
				http://m.hotukdeals.com/deals/monster-munch-monster-sized-bags-only-39p-home-bargains-1143767?aui=487
			' (length=110)
          public 'deal_image' => string '
				http://www.hotukdeals.com/images/threads/1143767_1.jpg
			' (length=63)
          public 'description' => string '
				98g Bags only 39p typically 99p elsewhere

In Beef and Pickled Onion flavours

Nom nom nom
			' (length=99)
          public 'submit_time' => string '
				2 hours, 53 minutes ago
			' (length=32)
          public 'hot_time' => string '
				1 hour, 2 minutes ago
			' (length=30)
          public 'poster_name' => string '
				deanos
			' (length=15)
          public 'temperature' => string '
				244.130004883
			' (length=22)
          public 'price' => string '
				0.39
			' (length=13)
          public 'timestamp' => string '
				1329215580
			' (length=19)
          public 'expired' => string '
				false
			' (length=14)
          public 'forum' => 
            object(SimpleXMLElement)[44]
              ...
          public 'category' => 
            object(SimpleXMLElement)[45]
              ...
          public 'merchant' => 
            object(SimpleXMLElement)[46]
              ...
          public 'tags' => 
            object(SimpleXMLElement)[47]
              ...
          public 'deal_image_highres' => string '
				http://www.hotukdeals.com/images/threads/high-res/1143767_1.jpg
			' (length=72)
      4 => 
        object(SimpleXMLElement)[8]
          public 'title' => string '
				Apple iPad 2 16GB Tablet WiFi White for 369 @ Play.Com
			' (length=65)
          public 'deal_link' => string '
				http://www.hotukdeals.com/deals/apple-ipad-2-16gb-tablet-wifi-white-for-369-play-com-1143780?aui=487
			' (length=109)
          public 'mobile_deal_link' => string '
				http://m.hotukdeals.com/deals/apple-ipad-2-16gb-tablet-wifi-white-for-369-play-com-1143780?aui=487
			' (length=107)
          public 'deal_image' => string '
				http://www.hotukdeals.com/images/threads/1143780_1.jpg
			' (length=63)
          public 'description' => string '
				Play took 30 off the price just for today.


There's more to it. And even less of it.
Two cameras for FaceTime and HD video recording. The Apple dual-core A5 chip. The same 10-hour battery life. All in a thinner, lighter design. Now iPad is even more amazing. And even less like anything else

Thinner, lighter and full of great ideas.
Once you pick up iPad 2, it'll be hard to put down. That's the idea behind the all-new design. It's 33 per cent thinner and up to 15 per cent lighter, so it feels even m'... (length=3223)
          public 'submit_time' => string '
				2 hours, 38 minutes ago
			' (length=32)
          public 'hot_time' => string '
				1 hour, 17 minutes ago
			' (length=31)
          public 'poster_name' => string '
				Rudess
			' (length=15)
          public 'temperature' => string '
				264.640014648
			' (length=22)
          public 'price' => string '
				369
			' (length=12)
          public 'timestamp' => string '
				1329216507
			' (length=19)
          public 'expired' => string '
				false
			' (length=14)
          public 'forum' => 
            object(SimpleXMLElement)[48]
              ...
          public 'category' => 
            object(SimpleXMLElement)[49]
              ...
          public 'merchant' => 
            object(SimpleXMLElement)[50]
              ...
          public 'tags' => 
            object(SimpleXMLElement)[51]
              ...
          public 'deal_image_highres' => string '
				http://www.hotukdeals.com/images/threads/high-res/1143780_1.jpg
			' (length=72)
      5 => 
        object(SimpleXMLElement)[9]
          public 'title' => string '
				The Walking
2
Contributors
14
Replies
15
Views
5 Years
Discussion Span
Last Post by diafol
0
$xmlstring = file_get_contents("file.xml");
$apis = new SimpleXMLElement($xmlstring);
$json = json_encode($apis->deals);
$array = json_decode($json,TRUE);

That puts your data into an usable array ($array). If you want to make a class to deal with the data. I'm a total noob at OOP. But I'll have a look later on. :)

//EDIT

Urgh! Seems the file is a bit odd - there seem to be nested <api_item> tags. Is that right? When I tried to run a few class functions, they returned strange number of api_items (17), but when I text searched for number of "<api_item>" it gave a different number (65).

So, the question is, is this a well formed XML file?

Edited by diafol: n/a

0

Had a little play. OOP aficionados will shoot me down here I'm sure, but here goes:

BTW, I'm keeping the data as an array, as opposed to a single 'record'. Don't know if that's what you're looking for.

<?php
$xmlstring = file_get_contents("file.xml");
$apis = new SimpleXMLElement($xmlstring);
$node = $apis->deals;
$json = json_encode($node);
$array = json_decode($json,TRUE);
$toClass = $array['api_item'];

class XMLArr{
	private $main;
	public function __construct($array){
		$this->main = (is_array($array)) ? $array : array();	
	}
	public function getRow($id){
		if(isset($this->main[$id]))return $this->main[$id];
	}
	public function getProperty($id,$field){
		if(isset($this->main[$id][$field]))return $this->main[$id][$field];	
	}
	public function getCount(){
		return count($this->main);	
	}
	public function setProperty($id,$field,$value){
		if(isset($this->main[$id][$field])){
			$this->main[$id][$field] = $value;
			return true;
		}
	}
}


$m = new XMLArr($toClass);

echo $m->getCount();

if($m->setProperty(2,'title','hello this is a new Title'))echo $m->getProperty(2,'title');

?>

** the setProperty / getProperty don't work with nested items **

Edited by diafol: n/a

0

Hello ardav,

thanks for your reply.

I don't think that the XML is well formed. Well, the code you've written is close to what I need.

The API also has an option to recieve data in JSON format. But I explicitly am not using it as I want to use XML so I could learn how things work.

now what I actually want to do is dump the data into dynamic array (becayse I dont't know the size of incoming data) automatically I get into a sensible array where I can get the data.

0

Well, json was there just for a convenient 'middle man' to get where I wanted.

the $toClass array should be what you need though?

if you do:

<pre>
  <?php print_r($toClass);?>
</pre>

It should show all the api_items.

Edited by diafol: n/a

0

so this is what I came up with.

$xmlstring = file_get_contents("file.xml");
	$apis = new SimpleXMLElement($xmlstring);
	$node = $apis->deals;
	$json = json_encode($node);
	$array = json_decode($json,TRUE);
	$MainClass= $array['api_item']; //aray of node 'api_item'
	$new_array = array(); //new blank array
	
	foreach($MainClass as $MainClassObj) { //traverse through the array
		if(is_array($MainClassObj)) { //if it has children which itself is an array
			foreach($MainClassObj as $MainClassChild) { //traverse through the child array
				if(is_array($MainClassChild)){ //if the child has array too
					foreach($MainClassObj as $MainClassChildObj) {//traverse through the grand child array
						print_r($MainClassChildObj); // dump the data
					}
				}
			}
		}

	}

This works in some sort of way, but I think I'm just looping the print_r for 'n' number of array items in the variable.
the more I think about it, the more it hurts my brain.

i'm stuck again :(

Edited by emclondon: n/a

0

You need may a recursive function here. I'm not sure what you need as you already have all the data in a nested array (the way it should be).

Do you need to create some sort of tree? What do you intent doing with the data?

If you have a template/placeholders for html output, you will probably need to know all the keys before hand so that you can extract the data directly.

Looking at the first api_item:

<api_item> 
	<title> The Verve Urban Hymns CD Preowned £1 at pound land! </title>
	<deal_link> http://www.hotukdeals.com/deals/verve-urban-hymns-cd-preowned-1-pound-land-1143545?aui=487 </deal_link>
	<mobile_deal_link> http://m.hotukdeals.com/deals/verve-urban-hymns-cd-preowned-1-pound-land-1143545?aui=487 </mobile_deal_link>
	<deal_image> http://www.hotukdeals.com/images/threads/1143545_1.jpg </deal_image>
	<description> Only just remembered to post this! Was in Birmingham yesterday and got the Verve Urban Hymnes for £1! Bitter sweet symphony is such an awesome track! It was the store by new street. It said pre owned on it, but mine looks brand new. Perhaps the disc has been resurfaced? For a pound I don't care! </description>
	<submit_time> 14 hours, 39 minutes ago </submit_time>
	<hot_time> 11 minutes ago </hot_time>
	<poster_name> thearbiter65 </poster_name>
	<temperature> 115.489997864 </temperature>
	<price> 1 </price>
	<timestamp> 1329173213 </timestamp>
	<expired> false </expired> 
	<forum>
		<name> Deals </name>
		<url_name> deals </url_name>
	</forum>
	<category>
		<name> Entertainment </name>
		<url_name> entertainment </url_name>
	</category> 
	<merchant>
		<name> PoundLand </name>
		<url_name> poundland.co.uk </url_name>
	</merchant>
	<tags>
		<api_item>
			<name> the verve </name>
		</api_item>
		<api_item>
			<name> pound land </name>
		</api_item>
	</tags>
	<deal_image_highres> http://www.hotukdeals.com/images/threads/high-res/1143545_1.jpg </deal_image_highres>
</api_item>

WHICH GIVES

title (single, level 1)
deal_link (single, level 1)
mobile_deal_link (single, level 1)
deal_image (single, level 1)
description (single, level 1)
submit_time (single, level 1)
hot_time (single, level 1)
poster_name (single, level 1)
temperature (single, level 1)
price (single, level 1)
timestamp (single, level 1)
expired (single, level 1)
forum  (single, level 1, container)
  name (single, level 2)
  url_name (single, level 2)
category (single, level 1, container)
  name (single, level 2)
  url_name (single, level 2)
merchant (single, level 1, container)
  name (single, level 2)
  url_name (single, level 2)
tags (single, level 1, container) 
  api_item ([B]multiple[/B], level 2, container)
    name (single, level 3)

Is this the extent of it? It doesn't look too bad

0

I'm trying to auto-populate a tree/list based on the XML I recieve so that I can use it to display data on to a page.

All I want with that data is to display it on to my ui. sommething like this

Attachments OgPxO.png 3.16 KB
0

I'm trying to auto-populate a tree/list based on the XML I recieve so that I can use it to display data on to a page.

You need may a recursive function here. I'm not sure what you need as you already have all the data in a nested array (the way it should be). . . . . . . . WHICH GIVES

title (single, level 1)
deal_link (single, level 1)
mobile_deal_link (single, level 1)
deal_image (single, level 1)
description (single, level 1)
submit_time (single, level 1)
hot_time (single, level 1)
poster_name (single, level 1)
temperature (single, level 1)
price (single, level 1)
timestamp (single, level 1)
expired (single, level 1)
forum  (single, level 1, container)
  name (single, level 2)
  url_name (single, level 2)
category (single, level 1, container)
  name (single, level 2)
  url_name (single, level 2)
merchant (single, level 1, container)
  name (single, level 2)
  url_name (single, level 2)
tags (single, level 1, container) 
  api_item (<strong>multiple</strong>, level 2, container)
    name (single, level 3)

Is this the extent of it? It doesn't look too bad

EXACTLY!

This is the reason I created my class this way:Hello guys,
.
.
.
.
.
.
.
.
.
this is what I designed

<?php

	class api_item{

		private $title;
		private $deal_link;
		private $mobile_deal_link;
		private $deal_image;
		private $description;
		private $submit_time;
		private $hot_time;
		private $poster_name;
		private $temperature;
		private $price;
		private $timestamp;
		private $expired;
		private $deal_image_highres;
		
		function get_title(){
			return $title;
		}
		function get_deal_link(){
			return $deal_link;
		}
		function get_mobile_deal_link(){
			return $mobile_deal_link;
		}
		function get_deal_image(){
			return $get_deal_image;
		}
		function get_description(){
			return $description;
		}
		function get_submit_time(){
			return $submit_time;
		}
		function get_hot_time(){
			return $hot_time;
		}
		function get_poster_name(){
			return $poster_name;
		}
		function get_temperature(){
			return $temperature;
		}
		function get_price(){
			return $price;
		}
		function get_timestamp(){
			return $timestamp;
		}
		function get_expired(){
			return $expired;
		}
		function get_deal_image_highres(){
			return $deal_image_highres;
		}
		
		function put_title($var_title){
			$title = $var_title;
		}	
		function put_deal_link($var_deal_link){
			$deal_link = $var_deal_link;
		}
		function put_mobile_deal_link($var_mobile_deal_link){
			$mobile_deal_link = $var_mobile_deal_link; 
		}
		function put_deal_image($put_deal_image){
			$deal_image = $var_deal_image;
		}
		function put_description($var_description){
			$description = $var_description;
		}
		function put_submit_time($var_submit_time){
			$submit_time = $var_submit_time;
		}
		function put_hot_time($var_hot_time){
			$hot_time = $var_hot_time;
		}
		function put_poster_name($var_poster_name){
			$poster_name = $var_poster_name;
		}
		function put_temperature($var_temperature){
			$temperature = $var_temperature;
		}
		function put_price($var_price){
			$price = $var_price;
		}
		function put_timestamp($var_timestamp){
			$timestamp = $var_timestamp;
		}
		function put_expired($var_expired){
			$expired = $var_expired;
		}
		function put_deal_image_highres($var_deal_image_highres){
			$deal_image_highres = $var_deal_image_highres;
		}
	}
?>


now how do I traverse through the result and dump my array data into the class??

weird ideas my brain gives me :S

All I want with that data is to display it on to my ui. sommething like this

Edited by emclondon: n/a

Attachments OgPxO.png 3.16 KB
0

OK, now I get you. Once you have the array, it should be straightforward if the data is consistent. So you need the following to be extracted from the array:

title
description
temperature
deal_image
deal_image_highres

(all level 1)

then just the tags -> api_item -> name (level 3)

I'll have a look - I'll come back to you.

0

OK, used some CSS too (as you can see I was bored!) ;)

<?php
$xmlstring = file_get_contents("trial.xml");
$apis = new SimpleXMLElement($xmlstring);
$node = $apis->deals;
$json = json_encode($node);
$array = json_decode($json,TRUE);
$toClass = $array['api_item'];
 
class XMLArr{
	private $main;
	
	public function __construct($array){
		$this->main = (is_array($array)) ? $array : array();	
	}
		
	public function getCount(){
		return count($this->main);	
	}
	
	private function getProperty($id,$field,$t=NULL){
		if(isset($this->main[$id][$field])){
			return ($t) ? mb_strimwidth($this->main[$id][$field], 0, $t + 3, "...") : $this->main[$id][$field];	
		}
			
	}
	
	private function getTags($id){
		if(isset($this->main[$id]['tags']['api_item'])){
			foreach($this->main[$id]['tags']['api_item'] as $item){
				$keeper[] = trim($item['name']);	
			}
			return implode(", ",$keeper);
		}
	}
	
	public function makeBox($id,$truncateTitle=NULL,$truncateDescription=NULL){
		$output = "<div class=\"box\">";
		$output .= "<div class=\"titlebanner\">" . $this->getProperty($id,'title',$truncateTitle) . "<span class=\"temp\">" . $this->getProperty($id,'temperature') . "</span></div>";
		$output .= "<div class=\"picbox\"><a href=\"" . $this->getProperty($id,'deal_image_highres') . "\" target=\"_blank\"><img width=\"50\" height=\"50\" src=\"". $this->getProperty($id,'deal_image') . "\" /></a></div>";
		$output .= "<div class=\"desc\">" . $this->getProperty($id,'description',$truncateDescription); 
		$output .= "<div class=\"tags\">" . $this->getTags($id) . "</div>";
		$output .= "</div></div>";
		return $output;	
	}
	
	
}
?>

<?php
header ('Content-type: text/html; charset=utf-8');
$m = new XMLArr($toClass);
$output = ""; 
for($x=0;$x<$m->getCount();$x++){ 
  $output .= $m->makeBox($x,20,100);
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<style>
	.box{
		border: black 2px solid;
		background-color:#CCCCCC;
		color: black;	
		margin: 20px;
		width: 500px;
		padding: 20px;
		-moz-box-shadow: 10px 10px 5px #888;
		-webkit-box-shadow: 10px 10px 5px #888;
		box-shadow: 10px 10px 5px #888;
	}
	.picbox{
		border: black 2px solid;
		width: 60px;
		height: 60px;
		position:relative;
		top: -50px;
		left: 440px;
		background-color: white; 
		text-align: center;
		padding-top: 5px; 	
	}
	
	.titlebanner{
		background-color:#666666;
		color: white;
		font-family:Verdana, Geneva, sans-serif;
		font-size: 20px;	
		padding: 0 10px;
	}
	.temp{
		display:block;
		float: right;
		margin-right: 60px;	
	}
	.tags{
		border: black 2px solid;
		background-color: white;
		color: #999;
		font-weight:bold;
		font-size:12px;	
	}
</style>
</head>
<?php
echo $output;
?>

<body>
</body>
</html>


The multiple php sections are just there to show that they could (should) be in different files. Don't laugh at the OOP guys :(

You could wrap it all up so that one function does the whole lot - with no need for client loop. I added the truncate parameters as an after thought. I noticed that the title was really big in some entries. Anyway, easy enough to take off.

Chrome gives me this:

Edited by diafol: n/a

Attachments Screenshot_3.png 37.17 KB
0

Was fiddling with it earlier:

Noticed that some items didn't have tags, but a blank box still appeared. Fixed. Added tooltip for title as title could be truncated.

Had a spare half hour before picking up the son, so had a bigger fiddle with the css.

Here's the changed file:
trial_open.txt

Edited by diafol: n/a

Attachments Screenshot_7.png 159.34 KB
<?php
$xmlstring = file_get_contents("trial.xml");
$apis = new SimpleXMLElement($xmlstring);
$node = $apis->deals;
$json = json_encode($node);
$array = json_decode($json,TRUE);
$toClass = $array['api_item'];
 
class XMLArr{
	private $main;
	
	public function __construct($array){
		$this->main = (is_array($array)) ? $array : array();	
	}
		
	public function getCount(){
		return count($this->main);	
	}
	
	private function getProperty($id,$field,$t=NULL){
		if(isset($this->main[$id][$field])){
			return ($t) ? mb_strimwidth(trim($this->main[$id][$field]), 0, $t + 3, "...") : trim($this->main[$id][$field]);	
		}
			
	}
	
	private function getTags($id){
		if(isset($this->main[$id]['tags']['api_item'])){
			foreach($this->main[$id]['tags']['api_item'] as $item){
				$keeper[] = trim($item['name']);	
			}
			return implode(", ",$keeper);
		}
	}
	
	public function makeBox($id,$truncateTitle=NULL,$truncateDescription=NULL){
		$output = "<div class=\"box\">";
		$output .= "<div class=\"titlebanner\"><span title=\"" . str_replace('"','&quot;',$this->getProperty($id,'title')) . "\">" . $this->getProperty($id,'title',$truncateTitle) . "</span><span class=\"temp\">" . round($this->getProperty($id,'temperature'),3) . "</span></div>";
		$output .= "<div class=\"desc\"><div class=\"picbox\"><a href=\"" . $this->getProperty($id,'deal_image_highres') . "\" target=\"_blank\"><img width=\"50\" height=\"50\" src=\"". $this->getProperty($id,'deal_image') . "\" /></a></div>" . $this->getProperty($id,'description',$truncateDescription); 
		if($this->getTags($id))$output .= "<div class=\"tags\">" . $this->getTags($id) . "</div>";
		$output .= "</div></div>";
		return $output;	
	}
	
	
}
?>

<?php
header ('Content-type: text/html; charset=utf-8');
$m = new XMLArr($toClass);
$output = ""; 
for($x=0;$x<$m->getCount();$x++){ 
  $output .= $m->makeBox($x,55);
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<style>
	.box{		
		font-family:Verdana, Geneva, sans-serif;
		border: black 2px solid;
		background-color:#F5F5F5;
		color: black;	
		margin: 20px;
		width: 500px;
		-moz-box-shadow: 10px 10px 5px #888;
		-webkit-box-shadow: 10px 10px 5px #888;
		box-shadow: 10px 10px 5px #888;
		font-size: 12px;
		
	}
	.picbox{
		border: black 2px solid;
		width: 60px;
		height: 60px;
		margin-top: -20px;
		float:right;
		background-color: white; 
		text-align: center;
		padding-top: 5px; 	
	}
	
	.titlebanner{
		background-color:#CCC;
		color: black;
		margin: 20px 0;	
		padding: 5px 10px;
		font-weight: bold;
	}
	.desc{
		border: #666666 2px solid;
		margin: 20px;
		padding: 10px;
		background-color:#CCC;	
	}
	.temp{
		display:block;
		float: right;
		color: white;
		background-color:#666666;
		margin-top: -5px;
		padding: 5px;	
	}
	.tags{
		border: black 2px solid;
		background-color: white;
		color: #999;
		margin-top: 10px;
		font-weight:bold;
		padding: 5px;
	}
</style>
</head>
<?php
echo $output;
?>

<body>
</body>
</html>
0

hmm.. sorry for being silent for too long. I think I've deviated from my track here.

indeed your solution is marvellous. lets forget 'getTags' for a moment and lets assume that my xml has like around more than 15000 elements, then wouldn't it be time consuming for the script to render the details on to the screen?

I have thought of a similar solution earlier, and the same thought occurred to me. That's the main reason which made me think of alternative solution to dump the data into a temporary 'db' and accessing it later at my own leisure.

also can the same result be achieved without json?

0

This is as far as I could get onto the code

process.php

<?php
	if(isset($_POST['submit'])) {
		include_once 'CurlREST.php';
		$category = urlencode($_POST['category']);
		$display = urlencode($_POST['display']);
		$sort = urlencode($_POST['sort']);
		$key = "12345678932156487"; // the key has been changed here
		
		$request = array();
		$request['$category']=$category;
		$request['$display']=$display;
		$request['$sort']=$sort;
		$request['$key']=$key;
		$myClient = new CurlREST();
		$response  = $myClient->getResponse($request); // this is the array I get

		if($response) {

			$doc = new DOMDocument();
			$doc->loadXML($response);
			
			$api = $doc->getElementsByTagName("api_item");

			foreach ($api as $item){
				
				$title = $item->getElementsByTagName("title");
				$deal_link = $item->getElementsByTagName("deal_link");
				$mobile_deal_link = $item->getElementsByTagName("mobile_deal_link");
				$deal_image = $item->getElementsByTagName("deal_image");
				$description = $item->getElementsByTagName("description");
				$submit_time = $item->getElementsByTagName("submit_time");
				$hot_time = $item->getElementsByTagName("hot_time");
				$poster_name = $item->getElementsByTagName("poster_name");
				$temperature = $item->getElementsByTagName("temperature");
				$price = $item->getElementsByTagName("price");
				$timestamp = $item->getElementsByTagName("timestamp");
				$expired = $item->getElementsByTagName("expired");
				$deal_image_highres = $item->getElementsByTagName("deal_image_highres");
				
				if(isset($deal_image_highres->item(0)->nodeValue))
				{
					$time = date("m-d-Y, g:i a",$timestamp->item(0)->nodeValue);
					echo "<table><tr><td><img src=\"".$deal_image->item(0)->nodeValue."\"/></td><td><a href=\"".$deal_link->item(0)->nodeValue."\">".iconv('UTF-8', 'ASCII//TRANSLIT',$title->item(0)->nodeValue)."</a></td><td>Posted by ".$poster_name->item(0)->nodeValue." (".$time.").</td></tr></table><br /><br />";
				}
				else
				{
					echo "Element Not found!<br />";
				}
			}
		}
		else {
			echo "Bad Selection";			
		}
	}
	else {
		header("HTTP/1.1 403 Forbidden");
	}
?>

CurlREST.php

<?php
	Class CurlREST {
		public function getResponse(array $params) {
			
			$category = $params['$category'];
			$display = $params['$display'];
			$sort = $params['$sort'];
			$key = $params['$key'];
			$option_error  ="bad options selected";
			$invalid_error = "Invalid Request; Please try again!";
			$url= 'http://api.hotukdeals.com/rest_api/v2?key='.$key.'&forum='.$category.'&results_per_page='.$display.'&order='.$sort;
			$curl_handler = curl_init();
			curl_setopt($curl_handler, CURLOPT_URL,$url);
			curl_setopt($curl_handler, CURLOPT_RETURNTRANSFER, 1);
			curl_setopt ($curl_handler, CURLOPT_HEADER, 0);
			$response = curl_exec($curl_handler);
			$response_code = curl_getinfo($curl_handler, CURLINFO_HTTP_CODE);
			
			if($response_code == '200') {
				curl_close($curl_handler);
				return $response;
			}
			else {
				curl_close($curl_handler);
				return NULL;
			}
		}
	}
?>

Edited by emclondon: n/a

0

That's the main reason which made me think of alternative solution to dump the data into a temporary 'db' and accessing it later at my own leisure.

also can the same result be achieved without json?

You can do what you like with it. Put it in a file, put it in a DB, put it in an XML file of your own. As I mentioned before, the 'json' is a 'middle-man', it just acts as an intermediate to help format the array structure. If you think you can do without it, do so.

I think I've deviated from my track here.

Yep. You certainly have. Good luck with it.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.