<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Seventh Sense &#187; XML</title>
	<atom:link href="http://sgowtham.net/blog/category/technology/xml/feed/" rel="self" type="application/rss+xml" />
	<link>http://sgowtham.net/blog</link>
	<description>Rambling about life&#039;s little things, in 7 ≡ 1 (mod 6) fashion</description>
	<lastBuildDate>Thu, 22 Dec 2011 14:48:38 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
<image>
	<title>Seventh Sense</title>
	<url>http://sgowtham.net/images/sg_logo_rss.png</url>
	<link>http://sgowtham.net/blog</link>
	<width>32</width>
	<height>32</height>
	</image>
		<item>
		<title>Importing Comments From flickr.com Into Pixelpost</title>
		<link>http://sgowtham.net/blog/2008/07/04/importing-comments-from-flickrcom-into-pixelpost/</link>
		<comments>http://sgowtham.net/blog/2008/07/04/importing-comments-from-flickrcom-into-pixelpost/#comments</comments>
		<pubDate>Fri, 04 Jul 2008 14:21:48 +0000</pubDate>
		<dc:creator>Gowtham</dc:creator>
				<category><![CDATA[Flickr]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[PixelPost]]></category>
		<category><![CDATA[RSS]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://sgowtham.net/blog/?p=316</guid>
		<description><![CDATA[Disclaimer These instructions/steps worked for me running CentOS. It may very well work for you on Red Hat-like or other distributions. Please note that if you decide to use these instructions on your machine, you are doing so entirely at your very own discretion and that neither this site, sgowtham.net, nor its author is responsible [...]]]></description>
			<content:encoded><![CDATA[<h3 class="blog">Disclaimer</h3>
<p>These instructions/steps worked for me running CentOS. It may very well work for you on Red Hat-like or other distributions. Please note that if you decide to use these instructions on your machine, you are doing so entirely at your very own discretion and that neither this site, <a href="http://sgowtham.net/" target="_blank">sgowtham.net</a>, nor its author is responsible for any/all damage &#8211; intellectual or otherwise.</p>
<p><br clear="all"><br />
Following my previous post, about <a href="http://sgowtham.net/blog/2008/07/01/importing-comments-from-flickrcom/" target="_blank">importing comments from flickr.com</a> to my home-made <a href="http://sgowtham.net/showcase/" target="_blank">photoblog</a>, buddy <a href="http://flickr.com/photos/aragirn/" target="_blank">Kyle</a> thought it would be useful to have a similar API which can import comments from <a href="http://flickr.com/" target="_blank">flickr.com</a> into a photoblog powered by <a href="http://pixelpost.org/" target="_blank">Pixelpost</a>. Although posting the same pictures on two different platforms &#8211; once in a personal photoblog and once in <em>flickr.com</em> &#8211; can seem painstakingly time-consuming, it does have its advantages. For one, the latter is a multi-user platform with users ranging from novice/beginners to advanced professionals. As such, there is a better chance for attracting useful comments. As easy as it might seem to manually enter comments from <em>flickr.com</em> to  photoblog when there are only few comments, it can become quite tedious and even more time consuming with time. To that effect, I did some Google! search to find an XML/RSS parser, modified it to meet Pixelpost-imposed requirements. The procedure/edits follow some MUST DO things for it all to work in a seamless fashion:</p>
<p><br clear="all"></p>
<blockquote><ol>
<li>The image names in Pixelpost database MUST BE UNIQUE. If you are wondering how to accomplish that, you may refer to one of my <a href="http://sgowtham.net/blog/2008/03/28/mac-address-for-cameras/" target="_blank" class="underline"><font color="#ff0000">previous posts</font></a>.</li>
<li>Back up the MySQL database used for Pixelpost &#8211; if something goes wrong, you will have something to revert back to. Step by step instructions for doing so are <a href="http://sgowtham.net/blog/2008/04/04/backing-up-and-restoring-mysql-databases/" target="_blank"  class="underline"><font color="#ff0000">here</font></a>.</li>
<li>When posting images in <em>flickr.com</em>, make sure the title for that post is NOTHING BUT the filename, EXCEPT the extension (jpg or gif or png or something else).</li>
</ol>
</blockquote>
<p><br clear="all"></p>
<h3 class="blog">XML/RSS Parser with PHP</h3>
<p><a href="http://flickr.com/" target="_blank">flickr.com</a> generates an RSS feed for comments that others (or I) make on my photos. This RSS feed contains all the required information &#8211; name, date-time, comment, title of the image, etc. The way I designed my photoblog, the image_name is the unique identifier and when I upload the images to <em>flickr.com</em>, I keep the image name as part of the title. The following script &#8211; <b>flickr2pixelpost.php</b>, not originally written by me but parts of it heavily modified, does exactly what I want &#8211; extract the information from RSS feed and arrange it in a manner that it can be incorporated into a photoblog powered by Pixelpost (one may refer to the <a href="http://www.sitepoint.com/article/php-xml-parsing-rss-1-0" target="_blank">original script &amp; its documentation</a>).</p>
<p><br clear="all"></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">#! /usr/bin/php
<span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;"># Connect to the database
</span><span style="color: #000088;">$host</span>     <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;localhost&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$dbuser</span>   <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;MYSQL_PIXELPOST_USERID&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$dbpasswd</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;MYSQL_PIXELPOST_PASSWD&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$database</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;MYSQL_PIXELPOST_DB&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$connect</span>  <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_connect</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$host</span><span style="color: #339933;">,</span> <span style="color: #000088;">$dbuser</span><span style="color: #339933;">,</span> <span style="color: #000088;">$dbpasswd</span><span style="color: #009900;">&#41;</span> or <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">mysql_error</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">mysql_select_db</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$database</span><span style="color: #339933;">,</span><span style="color: #000088;">$connect</span><span style="color: #009900;">&#41;</span> or <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">mysql_error</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> FlickrRSSParser <span style="color: #009900;">&#123;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;"># In Flickr's RSS file, all the information needed is contained in the &lt;item&gt; 
</span>  <span style="color: #666666; font-style: italic;"># tags in the document. So the first global variable defined will be $insideitem, 
</span>  <span style="color: #666666; font-style: italic;"># which will be set to true when entering an &lt;item&gt; tag and false when exiting one.
</span>  <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$insideitem</span>  <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$tag</span>         <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$title</span>       <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$description</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$link</span>        <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$pubdate</span>     <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;"># This function will be called by the XML parser whenever an opening tag is 
</span>  <span style="color: #666666; font-style: italic;"># encountered
</span>  <span style="color: #666666; font-style: italic;"># $parser will be passed a reference to the XML parser that is being used to 
</span>  <span style="color: #666666; font-style: italic;"># parse the document
</span>  <span style="color: #666666; font-style: italic;"># $tagName is the ALL-UPPERCASE (the PHP manual calls this 'case-folded')
</span>  <span style="color: #666666; font-style: italic;"># version of the name of the opening tag that triggered the event
</span>  <span style="color: #666666; font-style: italic;"># $attrs is an associative array of the attributes that are present in the tag
</span>  <span style="color: #666666; font-style: italic;"># that triggered the event
</span>  <span style="color: #000000; font-weight: bold;">function</span> startElement<span style="color: #009900;">&#40;</span><span style="color: #000088;">$parser</span><span style="color: #339933;">,</span> <span style="color: #000088;">$tagName</span><span style="color: #339933;">,</span> <span style="color: #000088;">$attrs</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">insideitem</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">tag</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$tagName</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">elseif</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$tagName</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">&quot;ITEM&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">insideitem</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;"># $parser will be passed a reference to the XML parser that is being used to
</span>  <span style="color: #666666; font-style: italic;"># parse the document
</span>  <span style="color: #666666; font-style: italic;"># $tagName is the case-folded name of the closing tag that triggered the event
</span>  <span style="color: #000000; font-weight: bold;">function</span> endElement<span style="color: #009900;">&#40;</span><span style="color: #000088;">$parser</span><span style="color: #339933;">,</span> <span style="color: #000088;">$tagName</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$tagName</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">&quot;ITEM&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
      <span style="color: #666666; font-style: italic;"># Image name: flickr.com displays the title as 'Comment on dsc_100-1234'
</span>      <span style="color: #666666; font-style: italic;"># The filename is the 3rd array element
</span>      <span style="color: #000088;">$title</span>        <span style="color: #339933;">=</span> <span style="color: #990000;">htmlspecialchars</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">trim</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">title</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000088;">$title</span>        <span style="color: #339933;">=</span> <span style="color: #990000;">explode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot; &quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$title</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000088;">$filename</span>     <span style="color: #339933;">=</span> <span style="color: #000088;">$title</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #666666; font-style: italic;"># Date/Time the comment was made (yyyy-mm-dd hh:mm:ss format)
</span>      <span style="color: #000088;">$pubdate</span>      <span style="color: #339933;">=</span> <span style="color: #990000;">htmlspecialchars</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">trim</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">pubdate</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000088;">$pubdate</span>      <span style="color: #339933;">=</span> <span style="color: #990000;">strtotime</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$pubdate</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000088;">$pubdate</span>      <span style="color: #339933;">=</span> <span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Y-m-d H:i:s&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$pubdate</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #000088;">$paragraphs</span>   <span style="color: #339933;">=</span> <span style="color: #990000;">htmlspecialchars</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">trim</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">description</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000088;">$paragraphs</span>   <span style="color: #339933;">=</span> <span style="color: #990000;">explode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;&amp;lt;/p&amp;gt;&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$paragraphs</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #666666; font-style: italic;"># The description contains the link to comment-author's flickr profile and 
</span>      <span style="color: #666666; font-style: italic;"># comment-author's name (first &lt;p&gt;&lt;/p&gt; section)
</span>      <span style="color: #000088;">$paragraph0</span>   <span style="color: #339933;">=</span> <span style="color: #000088;">$paragraphs</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
      <span style="color: #000088;">$authorurl</span>    <span style="color: #339933;">=</span> <span style="color: #990000;">explode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;&amp;quot;&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$paragraph0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000088;">$author_url</span>   <span style="color: #339933;">=</span> <span style="color: #000088;">$authorurl</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #000088;">$authorname0</span>  <span style="color: #339933;">=</span> <span style="color: #990000;">explode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;&amp;gt;&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$paragraph0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000088;">$authorname1</span>  <span style="color: #339933;">=</span> <span style="color: #000088;">$authorname0</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
      <span style="color: #000088;">$authorname2</span>  <span style="color: #339933;">=</span> <span style="color: #990000;">explode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;&amp;lt;&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$authorname1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000088;">$author_name</span>  <span style="color: #339933;">=</span> <span style="color: #000088;">$authorname2</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #666666; font-style: italic;"># The description also contains the comment-text (second &lt;p&gt;&lt;/p&gt; section)
</span>      <span style="color: #666666; font-style: italic;"># Basic substitutions are done, via ereg_replace(), to get the appropriate part
</span>      <span style="color: #666666; font-style: italic;"># mysql_real_escape_string() is used to make sure comment_text is in MySQL friendly fashion
</span>      <span style="color: #000088;">$paragraph1</span>   <span style="color: #339933;">=</span> <span style="color: #000088;">$paragraphs</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
      <span style="color: #000088;">$commenttext</span>  <span style="color: #339933;">=</span> <span style="color: #990000;">explode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;&amp;lt;p&amp;gt;&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$paragraph1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000088;">$comment_text</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$commenttext</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
      <span style="color: #000088;">$comment_text</span> <span style="color: #339933;">=</span> <span style="color: #990000;">ereg_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;&amp;lt;br /&amp;gt;&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;&lt;br&gt;<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$comment_text</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000088;">$comment_text</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_real_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$comment_text</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #666666; font-style: italic;"># The description also contains a link to image-thumbnail (second &lt;p&gt;&lt;/p&gt; section)
</span>      <span style="color: #666666; font-style: italic;"># but it's not required in this process - as such, it's ignored
</span> 
      <span style="color: #666666; font-style: italic;"># flickr.com's IP address
</span>      <span style="color: #000088;">$flickr_ip</span>    <span style="color: #339933;">=</span> <span style="color: #990000;">gethostbyname</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'www.flickr.com'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #666666; font-style: italic;"># Table structure for table `pixelpost_comments`
</span>      <span style="color: #666666; font-style: italic;">#
</span>      <span style="color: #666666; font-style: italic;"># CREATE TABLE IF NOT EXISTS `pixelpost_comments` (
</span>      <span style="color: #666666; font-style: italic;">#   `id` int(11) NOT NULL auto_increment,
</span>      <span style="color: #666666; font-style: italic;">#   `parent_id` int(11) NOT NULL default '0',
</span>      <span style="color: #666666; font-style: italic;">#   `datetime` datetime NOT NULL default '0000-00-00 00:00:00',
</span>      <span style="color: #666666; font-style: italic;">#   `ip` varchar(20) NOT NULL default '',
</span>      <span style="color: #666666; font-style: italic;">#   `message` text NOT NULL,
</span>      <span style="color: #666666; font-style: italic;">#   `name` varchar(30) default NULL,
</span>      <span style="color: #666666; font-style: italic;">#   `url` varchar(70) default NULL,
</span>      <span style="color: #666666; font-style: italic;">#   `email` varchar(100) default NULL,
</span>      <span style="color: #666666; font-style: italic;">#   `publish` char(3) NOT NULL default 'yes',
</span>      <span style="color: #666666; font-style: italic;">#   `spaminess` float default NULL,
</span>      <span style="color: #666666; font-style: italic;">#   `signature` varchar(55) default NULL,
</span>      <span style="color: #666666; font-style: italic;">#   PRIMARY KEY  (`id`),
</span>      <span style="color: #666666; font-style: italic;">#   KEY `parent_id` (`parent_id`)
</span>      <span style="color: #666666; font-style: italic;"># ) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
</span>      <span style="color: #666666; font-style: italic;">#
</span>      <span style="color: #666666; font-style: italic;"># To make sure that the comments from flickr.com are not duplicated,
</span>      <span style="color: #666666; font-style: italic;"># add the following to 'pixelpost_comments' table - via command-line or
</span>      <span style="color: #666666; font-style: italic;"># PHPMyAdmin.
</span>      <span style="color: #666666; font-style: italic;">#
</span>      <span style="color: #666666; font-style: italic;"># CREATE UNIQUE INDEX author_datetime ON `pixelpost_comments` (`name`,`datetime`);
</span>      <span style="color: #666666; font-style: italic;">#
</span>
&nbsp;
      <span style="color: #666666; font-style: italic;"># First, check if the flickr-image is in PIXELPOST database 
</span>      <span style="color: #666666; font-style: italic;"># If exists, then get the corresponding POST ID (PARENT ID)
</span>      <span style="color: #000088;">$sql_q0</span>       <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT id, image FROM `MYSQL_DB`.`pixelpost_pixelpost` &quot;</span><span style="color: #339933;">;</span>
      <span style="color: #000088;">$sql_q0</span>      <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;WHERE image='<span style="color: #006699; font-weight: bold;">$filename</span>.jpg' ORDER BY id DESC LIMIT 1 &quot;</span><span style="color: #339933;">;</span>
      <span style="color: #000088;">$result0</span>      <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql_q0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #666666; font-style: italic;"># Proceed further, to make an entry into comments database, iff the
</span>      <span style="color: #666666; font-style: italic;"># above query was successful
</span>      <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$result0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Invalid query: '</span> <span style="color: #339933;">.</span> <span style="color: #990000;">mysql_error</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
&nbsp;
        <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$myrow</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result0</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
          <span style="color: #000088;">$parent_id</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$myrow</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'id'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;"># Enter into the comments_table in database
</span>        <span style="color: #666666; font-style: italic;"># INSERT IGNORE makes sure that duplicate entries, when exist, 
</span>        <span style="color: #666666; font-style: italic;"># are ignored during insertion
</span>        <span style="color: #000088;">$sql_q1</span>       <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;INSERT IGNORE INTO `MYSQL_DB`.`pixelpost_comments` &quot;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$sql_q1</span>      <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;VALUES ('', '<span style="color: #006699; font-weight: bold;">$parent_id</span>', '<span style="color: #006699; font-weight: bold;">$pubdate</span>', '<span style="color: #006699; font-weight: bold;">$flickr_ip</span>', &quot;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$sql_q1</span>      <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;'<span style="color: #006699; font-weight: bold;">$comment_text</span>', '<span style="color: #006699; font-weight: bold;">$author_name</span>', '<span style="color: #006699; font-weight: bold;">$author_url</span>', &quot;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$sql_q1</span>      <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;'flickr@your-domain.com', 'yes', '', ''); &quot;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$result1</span>      <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql_q1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$result1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
          <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Invalid query: '</span> <span style="color: #339933;">.</span> <span style="color: #990000;">mysql_error</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">title</span>       <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">description</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">link</span>        <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">pubdate</span>     <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">insideitem</span>  <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;"># $parser will be passed a reference to the XML parser that is being used to
</span>  <span style="color: #666666; font-style: italic;"># parse the document
</span>  <span style="color: #666666; font-style: italic;"># $data is a string of text appearing between XML tags in the document. 
</span>  <span style="color: #666666; font-style: italic;"># The text between two tags will not necessarily trigger a single event. 
</span>  <span style="color: #666666; font-style: italic;"># Blocks of text spread over multiple lines will cause one event per line, 
</span>  <span style="color: #666666; font-style: italic;"># with each event being passed the $data for that line.
</span>  <span style="color: #000000; font-weight: bold;">function</span> characterData<span style="color: #009900;">&#40;</span><span style="color: #000088;">$parser</span><span style="color: #339933;">,</span> <span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">insideitem</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">switch</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">tag</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">case</span> <span style="color: #0000ff;">&quot;TITLE&quot;</span><span style="color: #339933;">:</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">title</span> <span style="color: #339933;">.=</span> <span style="color: #000088;">$data</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">case</span> <span style="color: #0000ff;">&quot;DESCRIPTION&quot;</span><span style="color: #339933;">:</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">description</span> <span style="color: #339933;">.=</span> <span style="color: #000088;">$data</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">case</span> <span style="color: #0000ff;">&quot;PUBDATE&quot;</span><span style="color: #339933;">:</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">pubdate</span> <span style="color: #339933;">.=</span> <span style="color: #000088;">$data</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">case</span> <span style="color: #0000ff;">&quot;LINK&quot;</span><span style="color: #339933;">:</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">link</span> <span style="color: #339933;">.=</span> <span style="color: #000088;">$data</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Create an XML parser
</span><span style="color: #666666; font-style: italic;"># Just as one must create a database connection in PHP to interact with a database, 
</span><span style="color: #666666; font-style: italic;"># one must create an XML parser to read in an XML file. In this case, a reference to 
</span><span style="color: #666666; font-style: italic;"># the parser is stored in $xml_parser.
</span><span style="color: #000088;">$xml_parser</span> <span style="color: #339933;">=</span> <span style="color: #990000;">xml_parser_create</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$rss_parser</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> FlickrRSSParser<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #990000;">xml_set_object</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$xml_parser</span><span style="color: #339933;">,&amp;</span><span style="color: #000088;">$rss_parser</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># This function specifies the functions that an XML parser should 
</span><span style="color: #666666; font-style: italic;"># use to process the events generated opening and closing tags. 
</span><span style="color: #666666; font-style: italic;"># In this case, the parser is the one stored in our $xml_parser variable, 
</span><span style="color: #666666; font-style: italic;"># while the functions are called startElement() and endElement()
</span> 
<span style="color: #990000;">xml_set_element_handler</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$xml_parser</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;startElement&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;endElement&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># This function specifies the function that the XML parser should use 
</span><span style="color: #666666; font-style: italic;"># to process character data appearing between tags in an XML document. 
</span><span style="color: #666666; font-style: italic;"># The function chosen to process character data is called characterData()
</span><span style="color: #990000;">xml_set_character_data_handler</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$xml_parser</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;characterData&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Flickr's RSS Feed Comments URL must be entered here
</span><span style="color: #000088;">$rss</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;FLICKR_RSS_FEED_FOR_COMMENTS&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Open the specified URL for reading
</span><span style="color: #000088;">$fp</span> <span style="color: #339933;">=</span> <span style="color: #990000;">fopen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$rss</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;r&quot;</span><span style="color: #009900;">&#41;</span>
  or <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Error reading RSS data.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #990000;">fread</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">4096</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #666666; font-style: italic;"># This function sends all or part of an XML document to the parser for it 
</span>  <span style="color: #666666; font-style: italic;"># to process. The endOfDocument parameter should be set to true if the 
</span>  <span style="color: #666666; font-style: italic;"># data marks the end of of XML document, or false if more of the document 
</span>  <span style="color: #666666; font-style: italic;"># will follow in a subsequent call to xml_parse(). This allows the parser to 
</span>  <span style="color: #666666; font-style: italic;"># correctly catch unclosed tags at the end of the document and so forth. 
</span>  <span style="color: #666666; font-style: italic;"># In this case, the parser is once again $xml_parser. The $data variable 
</span>  <span style="color: #666666; font-style: italic;"># (up to 4KB in size) retrieved from the file with fread() is passed as the 
</span>  <span style="color: #666666; font-style: italic;"># data to be processed, while the feof() is used to determine whether 
</span>  <span style="color: #666666; font-style: italic;"># PHP has reached the end of the XML file or not, thus providing the 
</span>  <span style="color: #666666; font-style: italic;"># required endOfDocument parameter. If an error occurs in the parsing of 
</span>  <span style="color: #666666; font-style: italic;"># the document, the error message is printed out along with the line of the 
</span>  <span style="color: #666666; font-style: italic;"># file at which it occurs with xml_error_string, xml_get_error_code() and 
</span>  <span style="color: #666666; font-style: italic;"># xml_get_current_line_number()
</span>  <span style="color: #990000;">xml_parse</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$xml_parser</span><span style="color: #339933;">,</span> <span style="color: #000088;">$data</span><span style="color: #339933;">,</span> <span style="color: #990000;">feof</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
    or <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">sprintf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;XML error: <span style="color: #009933; font-weight: bold;">%s</span> at line <span style="color: #009933; font-weight: bold;">%d</span>&quot;</span><span style="color: #339933;">,</span>
      <span style="color: #990000;">xml_error_string</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">xml_get_error_code</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$xml_parser</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
      <span style="color: #990000;">xml_get_current_line_number</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$xml_parser</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #990000;">fclose</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Although all memory resources are freed at the end of a PHP script, 
</span><span style="color: #666666; font-style: italic;"># one may wish to free up the memory used by the XML parser if the 
</span><span style="color: #666666; font-style: italic;"># script will perform other potentially memory-intensive tasks after it 
</span><span style="color: #666666; font-style: italic;"># parses the XML data. This function destroys the specified XML parser, 
</span><span style="color: #666666; font-style: italic;"># thus freeing up resources and memory it may have allocated for parsing.
</span><span style="color: #990000;">xml_parser_free</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$xml_parser</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p><br clear="all"><br />
To make sure that one doesn&#8217;t miss any comments, this above script may be run via a cron-job, twice a day or so. I no longer use Pixelpost (except on a test server) and as such, cannot demonstrate it &#8211; but if someone who uses this can send me links, I will most certainly update this post with those links. As always, if you find bugs/errors, do post them as comments and I will work on them.</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fsgowtham.net%2Fblog%2F2008%2F07%2F04%2Fimporting-comments-from-flickrcom-into-pixelpost%2F&amp;title=Importing%20Comments%20From%20flickr.com%20Into%20Pixelpost" id="wpa2a_2"><img src="http://sgowtham.net/blog/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://sgowtham.net/blog/2008/07/04/importing-comments-from-flickrcom-into-pixelpost/feed/</wfw:commentRss>
		<slash:comments>33</slash:comments>
		</item>
		<item>
		<title>Importing Comments From flickr.com</title>
		<link>http://sgowtham.net/blog/2008/07/01/importing-comments-from-flickrcom/</link>
		<comments>http://sgowtham.net/blog/2008/07/01/importing-comments-from-flickrcom/#comments</comments>
		<pubDate>Tue, 01 Jul 2008 14:36:58 +0000</pubDate>
		<dc:creator>Gowtham</dc:creator>
				<category><![CDATA[Flickr]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[RSS]]></category>
		<category><![CDATA[Showcase]]></category>
		<category><![CDATA[XML]]></category>

		<guid isPermaLink="false">http://sgowtham.net/blog/?p=315</guid>
		<description><![CDATA[Disclaimer These instructions/steps worked for me in CentOS 4.0. It may very well work for you on Red Hat-like or other distributions. Please note that if you decide to use these instructions on your machine, you are doing so entirely at your very own discretion and that neither this site, sgowtham.net, nor its author is [...]]]></description>
			<content:encoded><![CDATA[<h3 class="blog">Disclaimer</h3>
<p>These instructions/steps worked for me in CentOS 4.0. It may very well work for you on Red Hat-like or other distributions. Please note that if you decide to use these instructions on your machine, you are doing so entirely at your very own discretion and that neither this site, <a href="http://sgowtham.net/" target="_blank">sgowtham.net</a>, nor its author is responsible for any/all damage &#8211; intellectual or otherwise.</p>
<p><br clear="all"><br />
As much as I would like to see my photographs being commented in my personally satisfying <a href="http://sgowtham.net/showcase/" target="_blank">photoblog</a>, I have recently come to realize (thanks to suggestions from several friends &#8211; <a href="http://flickr.com/photos/nsaligrama/" target="_blank">Nagesh</a> &amp; <a href="http://flickr.com/photos/aragirn/" target="_blank">Kyle</a>) that it&#8217;s not a bad idea to post the same pictures on <a href="http://flickr.com/" target="_blank">flickr.com</a>. For one, the latter is a multi-user platform with users ranging from novice/beginners to advanced professionals. As such, there is a better chance for attracting useful comments. As easy as it might seem to manually enter comments from <em>flickr.com</em>  to my photoblog when there are only few comments, it can become quite tedious and time consuming (at least I expect it to be) with time. To that effect, I did some Google! search to find an XML/RSS parser, modified it to meet my requirements. The procedure/edits follow:</p>
<p><br clear="all"></p>
<h3 class="blog">XML/RSS Parser with PHP</h3>
<p><a href="http://flickr.com/" target="_blank">flickr.com</a> generates an RSS feed for comments that others (or I) make on my photos. This RSS feed contains all the required information &#8211; name, date-time, comment, title of the image, etc. The way I designed my photoblog, the image_name is the unique identifier and when I upload the images to <em>flickr.com</em>, I keep the image name as part of the title. The following script &#8211; <b>flickr2showcase.php</b>, not originally written by me but parts of it heavily modified, does exactly what I want &#8211; extract the information from RSS feed and arrange it in a manner that it can be incorporated into my photoblog (one may refer to the <a href="http://www.sitepoint.com/article/php-xml-parsing-rss-1-0" target="_blank">original script &amp; its documentation</a>).</p>
<p><br clear="all"></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">#! /usr/bin/php
<span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;"># Connect to the database
</span><span style="color: #000088;">$host</span>     <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;localhost&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$dbuser</span>   <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;MYSQL_USERID&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$dbpasswd</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;MYSQL_PASSWD&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$database</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;MYSQL_DB&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$connect</span>  <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_connect</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$host</span><span style="color: #339933;">,</span> <span style="color: #000088;">$dbuser</span><span style="color: #339933;">,</span> <span style="color: #000088;">$dbpasswd</span><span style="color: #009900;">&#41;</span> or <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">mysql_error</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">mysql_select_db</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$database</span><span style="color: #339933;">,</span><span style="color: #000088;">$connect</span><span style="color: #009900;">&#41;</span> or <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">mysql_error</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> FlickrRSSParser <span style="color: #009900;">&#123;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;"># In Flickr's RSS file, all the information needed is contained in the &lt;item&gt; 
</span>  <span style="color: #666666; font-style: italic;"># tags in the document. So the first global variable defined will be $insideitem, 
</span>  <span style="color: #666666; font-style: italic;"># which will be set to true when entering an &lt;item&gt; tag and false when exiting one.
</span>  <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$insideitem</span>  <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$tag</span>         <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$title</span>       <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$description</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$link</span>        <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$pubdate</span>     <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;"># This function will be called by the XML parser whenever an opening tag is 
</span>  <span style="color: #666666; font-style: italic;"># encountered
</span>  <span style="color: #666666; font-style: italic;"># $parser will be passed a reference to the XML parser that is being used to 
</span>  <span style="color: #666666; font-style: italic;"># parse the document
</span>  <span style="color: #666666; font-style: italic;"># $tagName is the ALL-UPPERCASE (the PHP manual calls this 'case-folded')
</span>  <span style="color: #666666; font-style: italic;"># version of the name of the opening tag that triggered the event
</span>  <span style="color: #666666; font-style: italic;"># $attrs is an associative array of the attributes that are present in the tag
</span>  <span style="color: #666666; font-style: italic;"># that triggered the event
</span>  <span style="color: #000000; font-weight: bold;">function</span> startElement<span style="color: #009900;">&#40;</span><span style="color: #000088;">$parser</span><span style="color: #339933;">,</span> <span style="color: #000088;">$tagName</span><span style="color: #339933;">,</span> <span style="color: #000088;">$attrs</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">insideitem</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">tag</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$tagName</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">elseif</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$tagName</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">&quot;ITEM&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">insideitem</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;"># $parser will be passed a reference to the XML parser that is being used to
</span>  <span style="color: #666666; font-style: italic;"># parse the document
</span>  <span style="color: #666666; font-style: italic;"># $tagName is the case-folded name of the closing tag that triggered the event
</span>  <span style="color: #000000; font-weight: bold;">function</span> endElement<span style="color: #009900;">&#40;</span><span style="color: #000088;">$parser</span><span style="color: #339933;">,</span> <span style="color: #000088;">$tagName</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$tagName</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">&quot;ITEM&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
      <span style="color: #666666; font-style: italic;"># Image name: flickr.com displays the title as 'Comment on dsc_100-1234'
</span>      <span style="color: #666666; font-style: italic;"># The filename is the 3rd array element
</span>      <span style="color: #000088;">$title</span>        <span style="color: #339933;">=</span> <span style="color: #990000;">htmlspecialchars</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">trim</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">title</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000088;">$title</span>        <span style="color: #339933;">=</span> <span style="color: #990000;">explode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot; &quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$title</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000088;">$filename</span>     <span style="color: #339933;">=</span> <span style="color: #000088;">$title</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #666666; font-style: italic;"># Date/Time the comment was made (yyyy-mm-dd hh:mm:ss format)
</span>      <span style="color: #000088;">$pubdate</span>      <span style="color: #339933;">=</span> <span style="color: #990000;">htmlspecialchars</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">trim</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">pubdate</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000088;">$pubdate</span>      <span style="color: #339933;">=</span> <span style="color: #990000;">strtotime</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$pubdate</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000088;">$pubdate</span>      <span style="color: #339933;">=</span> <span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Y-m-d H:i:s&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$pubdate</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #000088;">$paragraphs</span>   <span style="color: #339933;">=</span> <span style="color: #990000;">htmlspecialchars</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">trim</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">description</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000088;">$paragraphs</span>   <span style="color: #339933;">=</span> <span style="color: #990000;">explode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;&amp;lt;/p&amp;gt;&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$paragraphs</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #666666; font-style: italic;"># The description contains the link to comment-author's flickr profile and 
</span>      <span style="color: #666666; font-style: italic;"># comment-author's name (first &lt;p&gt;&lt;/p&gt; section)
</span>      <span style="color: #000088;">$paragraph0</span>   <span style="color: #339933;">=</span> <span style="color: #000088;">$paragraphs</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
      <span style="color: #000088;">$authorurl</span>    <span style="color: #339933;">=</span> <span style="color: #990000;">explode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;&amp;quot;&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$paragraph0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000088;">$author_url</span>   <span style="color: #339933;">=</span> <span style="color: #000088;">$authorurl</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #000088;">$authorname0</span>  <span style="color: #339933;">=</span> <span style="color: #990000;">explode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;&amp;gt;&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$paragraph0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000088;">$authorname1</span>  <span style="color: #339933;">=</span> <span style="color: #000088;">$authorname0</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
      <span style="color: #000088;">$authorname2</span>  <span style="color: #339933;">=</span> <span style="color: #990000;">explode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;&amp;lt;&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$authorname1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000088;">$author_name</span>  <span style="color: #339933;">=</span> <span style="color: #000088;">$authorname2</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #666666; font-style: italic;"># The description also contains the comment-text (second &lt;p&gt;&lt;/p&gt; section)
</span>      <span style="color: #666666; font-style: italic;"># Basic substitutions are done, via ereg_replace(), to get the appropriate part
</span>      <span style="color: #666666; font-style: italic;"># mysql_real_escape_string() is used to make sure comment_text is in MySQL friendly fashion
</span>      <span style="color: #000088;">$paragraph1</span>   <span style="color: #339933;">=</span> <span style="color: #000088;">$paragraphs</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
      <span style="color: #000088;">$commenttext</span>  <span style="color: #339933;">=</span> <span style="color: #990000;">explode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;&amp;lt;p&amp;gt;&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$paragraph1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000088;">$comment_text</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$commenttext</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
      <span style="color: #000088;">$comment_text</span> <span style="color: #339933;">=</span> <span style="color: #990000;">ereg_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;&amp;lt;br /&amp;gt;&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;&lt;br&gt;<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$comment_text</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000088;">$comment_text</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_real_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$comment_text</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #666666; font-style: italic;"># The description also contains a link to image-thumbnail (second &lt;p&gt;&lt;/p&gt; section)
</span>      <span style="color: #666666; font-style: italic;"># but it's not required in this process - as such, it's ignored
</span>
      <span style="color: #666666; font-style: italic;"># flickr.com's IP address
</span>      <span style="color: #000088;">$flickr_ip</span>    <span style="color: #339933;">=</span> <span style="color: #990000;">gethostbyname</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'www.flickr.com'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #666666; font-style: italic;"># comments_table structure
</span>      <span style="color: #666666; font-style: italic;"># CREATE TABLE IF NOT EXISTS `comments_table` (
</span>      <span style="color: #666666; font-style: italic;">#  `id`              int(11)      NOT NULL auto_increment,
</span>      <span style="color: #666666; font-style: italic;">#  `datetime`        DATETIME     NOT NULL default '0000-00-00 00:00:00',
</span>      <span style="color: #666666; font-style: italic;">#  `imagename`       varchar(254) NOT NULL,
</span>      <span style="color: #666666; font-style: italic;">#  `authorip`        varchar(254) NOT NULL default '000.000.000.000',
</span>      <span style="color: #666666; font-style: italic;">#  `authorhostname`  varchar(254) NOT NULL default 'localhost.localdomain',
</span>      <span style="color: #666666; font-style: italic;">#  `authorname`      varchar(254) NOT NULL default 'Unknown',
</span>      <span style="color: #666666; font-style: italic;">#  `authoremail`     varchar(254) NOT NULL default 'Unknown',
</span>      <span style="color: #666666; font-style: italic;">#  `authorurl`       varchar(254) NOT NULL default 'Unknown',
</span>      <span style="color: #666666; font-style: italic;">#  `comments`        text,
</span>      <span style="color: #666666; font-style: italic;">#  `status`          varchar(3) NOT NULL default 'No',
</span>      <span style="color: #666666; font-style: italic;">#
</span>      <span style="color: #666666; font-style: italic;">#  UNIQUE KEY `id` (`id`)
</span>      <span style="color: #666666; font-style: italic;"># ) ENGINE=MyISAM DEFAULT CHARSET latin1 AUTO_INCREMENT=1 ;
</span>      <span style="color: #666666; font-style: italic;">#
</span>      <span style="color: #666666; font-style: italic;"># CREATE UNIQUE INDEX author_datetime ON `comments_table` (`authorname`,`datetime`);
</span>      <span style="color: #666666; font-style: italic;">#
</span>
      <span style="color: #666666; font-style: italic;"># Enter into the comments_table in database
</span>      <span style="color: #666666; font-style: italic;"># INSERT IGNORE makes sure that duplicate entries, when exist, 
</span>      <span style="color: #666666; font-style: italic;"># are ignored during insertion
</span>      <span style="color: #000088;">$sql_q</span>        <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;INSERT IGNORE INTO `MYSQL_DB`.`comments_table` &quot;</span><span style="color: #339933;">;</span>
      <span style="color: #000088;">$sql_q</span>       <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;VALUES ('', '<span style="color: #006699; font-weight: bold;">$pubdate</span>', '<span style="color: #006699; font-weight: bold;">$filename</span>.jpg', '<span style="color: #006699; font-weight: bold;">$flickr_ip</span>', &quot;</span><span style="color: #339933;">;</span>
      <span style="color: #000088;">$sql_q</span>       <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;'flickr.com', '<span style="color: #006699; font-weight: bold;">$author_name</span>', 'flickr@your-domain.com', &quot;</span><span style="color: #339933;">;</span>
      <span style="color: #000088;">$sql_q</span>       <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;'<span style="color: #006699; font-weight: bold;">$author_url</span>', '<span style="color: #006699; font-weight: bold;">$comment_text</span>', 'Yes'); &quot;</span><span style="color: #339933;">;</span>
      <span style="color: #000088;">$result</span>       <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql_q</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Invalid query: '</span> <span style="color: #339933;">.</span> <span style="color: #990000;">mysql_error</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
&nbsp;
      <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">title</span>       <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
      <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">description</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
      <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">link</span>        <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
      <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">pubdate</span>     <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
      <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">insideitem</span>  <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;"># $parser will be passed a reference to the XML parser that is being used to
</span>  <span style="color: #666666; font-style: italic;"># parse the document
</span>  <span style="color: #666666; font-style: italic;"># $data is a string of text appearing between XML tags in the document. 
</span>  <span style="color: #666666; font-style: italic;"># The text between two tags will not necessarily trigger a single event. 
</span>  <span style="color: #666666; font-style: italic;"># Blocks of text spread over multiple lines will cause one event per line, 
</span>  <span style="color: #666666; font-style: italic;"># with each event being passed the $data for that line.
</span>  <span style="color: #000000; font-weight: bold;">function</span> characterData<span style="color: #009900;">&#40;</span><span style="color: #000088;">$parser</span><span style="color: #339933;">,</span> <span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">insideitem</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">switch</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">tag</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">case</span> <span style="color: #0000ff;">&quot;TITLE&quot;</span><span style="color: #339933;">:</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">title</span> <span style="color: #339933;">.=</span> <span style="color: #000088;">$data</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">case</span> <span style="color: #0000ff;">&quot;DESCRIPTION&quot;</span><span style="color: #339933;">:</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">description</span> <span style="color: #339933;">.=</span> <span style="color: #000088;">$data</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">case</span> <span style="color: #0000ff;">&quot;PUBDATE&quot;</span><span style="color: #339933;">:</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">pubdate</span> <span style="color: #339933;">.=</span> <span style="color: #000088;">$data</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">case</span> <span style="color: #0000ff;">&quot;LINK&quot;</span><span style="color: #339933;">:</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">link</span> <span style="color: #339933;">.=</span> <span style="color: #000088;">$data</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Create an XML parser
</span><span style="color: #666666; font-style: italic;"># Just as one must create a database connection in PHP to interact with a database, 
</span><span style="color: #666666; font-style: italic;"># one must create an XML parser to read in an XML file. In this case, a reference to 
</span><span style="color: #666666; font-style: italic;"># the parser is stored in $xml_parser.
</span><span style="color: #000088;">$xml_parser</span> <span style="color: #339933;">=</span> <span style="color: #990000;">xml_parser_create</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$rss_parser</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> FlickrRSSParser<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #990000;">xml_set_object</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$xml_parser</span><span style="color: #339933;">,&amp;</span><span style="color: #000088;">$rss_parser</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># This function specifies the functions that an XML parser should 
</span><span style="color: #666666; font-style: italic;"># use to process the events generated opening and closing tags. 
</span><span style="color: #666666; font-style: italic;"># In this case, the parser is the one stored in our $xml_parser variable, 
</span><span style="color: #666666; font-style: italic;"># while the functions are called startElement() and endElement()
</span>
<span style="color: #990000;">xml_set_element_handler</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$xml_parser</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;startElement&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;endElement&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># This function specifies the function that the XML parser should use 
</span><span style="color: #666666; font-style: italic;"># to process character data appearing between tags in an XML document. 
</span><span style="color: #666666; font-style: italic;"># The function chosen to process character data is called characterData()
</span><span style="color: #990000;">xml_set_character_data_handler</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$xml_parser</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;characterData&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Flickr's RSS Feed Comments URL must be entered here
</span><span style="color: #000088;">$rss</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;FLICKR_RSS_FEED_FOR_COMMENTS&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Open the specified URL for reading
</span><span style="color: #000088;">$fp</span> <span style="color: #339933;">=</span> <span style="color: #990000;">fopen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$rss</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;r&quot;</span><span style="color: #009900;">&#41;</span>
  or <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Error reading RSS data.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #990000;">fread</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">4096</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #666666; font-style: italic;"># This function sends all or part of an XML document to the parser for it 
</span>  <span style="color: #666666; font-style: italic;"># to process. The endOfDocument parameter should be set to true if the 
</span>  <span style="color: #666666; font-style: italic;"># data marks the end of of XML document, or false if more of the document 
</span>  <span style="color: #666666; font-style: italic;"># will follow in a subsequent call to xml_parse(). This allows the parser to 
</span>  <span style="color: #666666; font-style: italic;"># correctly catch unclosed tags at the end of the document and so forth. 
</span>  <span style="color: #666666; font-style: italic;"># In this case, the parser is once again $xml_parser. The $data variable 
</span>  <span style="color: #666666; font-style: italic;"># (up to 4KB in size) retrieved from the file with fread() is passed as the 
</span>  <span style="color: #666666; font-style: italic;"># data to be processed, while the feof() is used to determine whether 
</span>  <span style="color: #666666; font-style: italic;"># PHP has reached the end of the XML file or not, thus providing the 
</span>  <span style="color: #666666; font-style: italic;"># required endOfDocument parameter. If an error occurs in the parsing of 
</span>  <span style="color: #666666; font-style: italic;"># the document, the error message is printed out along with the line of the 
</span>  <span style="color: #666666; font-style: italic;"># file at which it occurs with xml_error_string, xml_get_error_code() and 
</span>  <span style="color: #666666; font-style: italic;"># xml_get_current_line_number()
</span>  <span style="color: #990000;">xml_parse</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$xml_parser</span><span style="color: #339933;">,</span> <span style="color: #000088;">$data</span><span style="color: #339933;">,</span> <span style="color: #990000;">feof</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
    or <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">sprintf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;XML error: <span style="color: #009933; font-weight: bold;">%s</span> at line <span style="color: #009933; font-weight: bold;">%d</span>&quot;</span><span style="color: #339933;">,</span>
      <span style="color: #990000;">xml_error_string</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">xml_get_error_code</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$xml_parser</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
      <span style="color: #990000;">xml_get_current_line_number</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$xml_parser</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #990000;">fclose</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;"># Although all memory resources are freed at the end of a PHP script, 
</span><span style="color: #666666; font-style: italic;"># one may wish to free up the memory used by the XML parser if the 
</span><span style="color: #666666; font-style: italic;"># script will perform other potentially memory-intensive tasks after it 
</span><span style="color: #666666; font-style: italic;"># parses the XML data. This function destroys the specified XML parser, 
</span><span style="color: #666666; font-style: italic;"># thus freeing up resources and memory it may have allocated for parsing.
</span><span style="color: #990000;">xml_parser_free</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$xml_parser</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p><br clear="all"><br />
To make sure that I don&#8217;t miss any comments, I run this above script via a cron-job, twice a day. To see this in action, one may compare the same image entries in my photoblog as well as on<em> flickr</em>: <a href="http://sgowtham.net/showcase/2008/06/21/great-blue-heron" target="_blank">Photoblog Entry</a> | <a href="http://www.flickr.com/photos/sgowtham/2613670334/" target="_blank">Flickr.com Entry</a></p>
<p>Things seem to be working without any problem so far but that doesn&#8217;t necessarily mean the code/work is without errors and/or bugs. As they show up, I will try to post work-around for them.</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fsgowtham.net%2Fblog%2F2008%2F07%2F01%2Fimporting-comments-from-flickrcom%2F&amp;title=Importing%20Comments%20From%20flickr.com" id="wpa2a_4"><img src="http://sgowtham.net/blog/wp-content/plugins/add-to-any/share_save_120_16.png" width="120" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://sgowtham.net/blog/2008/07/01/importing-comments-from-flickrcom/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>

