<?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>dev/null &#187; REST</title>
	<atom:link href="http://devnull.fuoriradio.com/tag/rest/feed/" rel="self" type="application/rss+xml" />
	<link>http://devnull.fuoriradio.com</link>
	<description>appunti interessanti e non di un programmatore</description>
	<lastBuildDate>Tue, 19 Apr 2011 15:22:09 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Dialogare con un server REST remoto in xml con php e JQuery.</title>
		<link>http://devnull.fuoriradio.com/2010/01/22/dialogare-con-un-server-rest-remoto-in-xml-con-php-e-jquery/</link>
		<comments>http://devnull.fuoriradio.com/2010/01/22/dialogare-con-un-server-rest-remoto-in-xml-con-php-e-jquery/#comments</comments>
		<pubDate>Fri, 22 Jan 2010 12:42:47 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Programmazione]]></category>
		<category><![CDATA[Ajax]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[jParse]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[REST]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://devnull.fuoriradio.com/?p=14</guid>
		<description><![CDATA[Uno dei problemi più comuni che si presentano ai programmatori del web 2.0 è dialogare con un server REST in xml o in JSON a seconda dei casi. La prima cosa da fare è creare uno script PHP che si occupi di fare da Proxi tra il server REST e quello che contiene la nostra [...]]]></description>
			<content:encoded><![CDATA[<p>Uno dei problemi più comuni che si presentano ai programmatori del web 2.0 è dialogare con un server REST in xml o in JSON a seconda dei casi.</p>
<p><img style="float:left; margin: 8px" src="http://snapcasa.com/get.aspx?code=9019&size=l&url=www.jquery.com"/ ></p>
<p>La prima cosa da fare è creare uno script PHP che si occupi di fare da Proxi tra il server REST e quello che contiene la nostra applicazione, infatti la richiesta XmlHttpRequest di Javascript è per motivi di sicurezza legata esclusivamente al domino su cui viene effettuata.</p>
<p>Scriviamo quindi per prima cosa la parte PHP che ci è necessaria, utilizzando CURL è semplicissimo:</p>
<p><span id="more-14"></span></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
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">// Per sicurezza è meglio che l'hostname del server rest non sia un parametro</span>
<span style="color: #666666; font-style: italic;">// ma sia scritto staticamente.</span>
<span style="color: #990000;">define</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'HOSTNAME'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'http://www.wheretherestserveris.com/'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// Prende il path del file rest</span>
<span style="color: #000088;">$query</span> <span style="color: #339933;">=</span> <span style="color: #990000;">urlencode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'query'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$url</span> <span style="color: #339933;">=</span> HOSTNAME<span style="color: #339933;">.</span><span style="color: #000088;">$query</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// Apre Curl</span>
<span style="color: #000088;">$mycurl</span> <span style="color: #339933;">=</span> <span style="color: #990000;">curl_init</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// Setto curl per restituire solo il contenuto senza header</span>
<span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$mycurl</span><span style="color: #339933;">,</span> CURLOPT_HEADER<span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$mycurl</span><span style="color: #339933;">,</span> CURLOPT_RETURNTRANSFER<span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$myxml</span> <span style="color: #339933;">=</span> <span style="color: #990000;">curl_exec</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$mycurl</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// Imposto gli header XML</span>
<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Content-Type: text/xml&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$myxml</span><span style="color: #339933;">;</span>
<span style="color: #990000;">curl_close</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$mycurl</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>Salviamo il nostro file proxi sul nostro dominio con il nome desiderato (Es. myrestserver.php) , e poi utilizziamo <a href="http://jquery.com/">jQuery</a> e il plugin <a href="http://jparse.kylerush.net/">jParse</a> per interpretare comodamente l&#8217;xml, ecco uno snippet di esempio del codice javascript necessario:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#divchevoglio'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">jParse</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
ajaxOpts<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>url<span style="color: #339933;">:</span> <span style="color: #3366CC;">'myrestserver.php?query=iwantthis.xml'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
elementTag<span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'field1'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'field2'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'field3'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
output<span style="color: #339933;">:</span> <span style="color: #3366CC;">' &lt;div&gt;&lt;ul&gt;&lt;li&gt;jpet0&lt;/li&gt;&lt;li&gt;jpet1&lt;/li&gt;&lt;li&gt;jpet2&lt;/li&gt;&lt;/div&gt;'</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Per approfondimenti su opzioni, callback etc. guardate il sito ufficiale di <a href="http://jparse.kylerush.net/">jParse</a></p>
<p>Spero di essere stato utile.</p>
<p><div style="text-align: center"><script type="text/javascript"><!--
google_ad_client = "pub-9763589373223061";
/* 468x60, devnull */
google_ad_slot = "9717154544";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div></p>
<p class='fb-like'><iframe src='http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fdevnull.fuoriradio.com%2F2010%2F01%2F22%2Fdialogare-con-un-server-rest-remoto-in-xml-con-php-e-jquery%2F&amp;layout=button_count&amp;show_faces=true&amp;width=450&amp;action=like&amp;colorscheme=light&amp;height=65&amp;font=lucida+grande' scrolling='no' frameborder='0' allowTransparency='true' style='border:none; overflow:hidden; width:450px; height:65px'></iframe></p>]]></content:encoded>
			<wfw:commentRss>http://devnull.fuoriradio.com/2010/01/22/dialogare-con-un-server-rest-remoto-in-xml-con-php-e-jquery/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

