﻿<?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>Sviluppatore, Amministratore e Consulente Informatico &#187; php</title>
	<atom:link href="http://www.marcobruni.info/category/php/feed" rel="self" type="application/rss+xml" />
	<link>http://www.marcobruni.info</link>
	<description>Quando una professione è anche una passione</description>
	<lastBuildDate>Thu, 05 Aug 2010 09:02:37 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>PHP: LA CLASSE PHP PER CREARE URL CORTI TRAMITE LE API DI TINYURL, IS.GD, HEX.IO, TR.IM &amp; BIT.LY</title>
		<link>http://www.marcobruni.info/php-la-classe-php-per-creare-url-corti-tramite-le-api-di-tinyurl-is-gd-hex-io-tr-im-bit-ly.html</link>
		<comments>http://www.marcobruni.info/php-la-classe-php-per-creare-url-corti-tramite-le-api-di-tinyurl-is-gd-hex-io-tr-im-bit-ly.html#comments</comments>
		<pubDate>Wed, 10 Mar 2010 22:38:33 +0000</pubDate>
		<dc:creator>marco</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[programmazione]]></category>

		<guid isPermaLink="false">http://www.marcobruni.info/?p=531</guid>
		<description><![CDATA[I Short URL sono comunemente usati oggi per diversi motivi: evitare garbling url,
prendere uno spazio più piccolo soprattutto per le URL lunghe che devono essere inviati a
ad esempio in uno spazio limitato come Twitter, oppure vengono spesso utilizzati anche per nascondere l&#8217;url originale per fare phishing e gli annunci / affiliazione. 
Ci sono molti siti [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;"><a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.marcobruni.info%2Fphp-la-classe-php-per-creare-url-corti-tramite-le-api-di-tinyurl-is-gd-hex-io-tr-im-bit-ly.html"><img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.marcobruni.info%2Fphp-la-classe-php-per-creare-url-corti-tramite-le-api-di-tinyurl-is-gd-hex-io-tr-im-bit-ly.html" height="61" width="51" /></a></div><p>I Short URL sono comunemente usati oggi per diversi motivi: evitare garbling url,<br />
prendere uno spazio più piccolo soprattutto per le URL lunghe che devono essere inviati a<br />
ad esempio in uno spazio limitato come Twitter, oppure vengono spesso utilizzati anche per nascondere l&#8217;url originale per fare phishing e gli annunci / affiliazione. </p>
<p>Ci sono molti siti web che forniscono servizi per accorciare url come tinyurl, bit.ly, hex.io, ho creato questo script PHP per Creare url brevi &#8220;al volo&#8221;, attualmente il supporto: TinyURL, bit.ly, is.gd, tr.im e hex.io, ma si possono facilmente aggiungere altri fornitori.</p>
<p>Se hai delle domande scrivimi un commento oppure scrivimi direttamente tramite la pagina dei contatti.</p>
<p><span class="no_translate"></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
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> ShortUrl <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> static <span style="color: #000000; font-weight: bold;">function</span> create<span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #339933;">,</span><span style="color: #000088;">$provider</span><span style="color: #339933;">=</span><span style="color: #0000ff;">'tinyurl'</span><span style="color: #339933;">,</span><span style="color: #000088;">$user</span><span style="color: #339933;">=</span><span style="color: #0000ff;">''</span><span style="color: #339933;">,</span><span style="color: #000088;">$key</span><span style="color: #339933;">=</span><span style="color: #0000ff;">''</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$api_url</span> <span style="color: #339933;">=</span> <span style="color: #990000;">sprintf</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #004000;">api</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$provider</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span><span style="color: #990000;">urlencode</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: #000088;">$user</span><span style="color: #339933;">,</span><span style="color: #000088;">$key</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #004000;">inspect</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$provider</span><span style="color: #339933;">,</span><span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #004000;">execute</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$api_url</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: #000000; font-weight: bold;">private</span> static <span style="color: #000000; font-weight: bold;">function</span> execute<span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$ch</span> <span style="color: #339933;">=</span> <span style="color: #990000;">curl_init</span><span style="color: #009900;">&#40;</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;">$ch</span><span style="color: #339933;">,</span> CURLOPT_URL<span style="color: #339933;">,</span> <span style="color: #000088;">$url</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;">$ch</span><span style="color: #339933;">,</span> CURLOPT_RETURNTRANSFER<span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</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;">$ch</span><span style="color: #339933;">,</span> CURLOPT_CONNECTTIMEOUT<span style="color: #339933;">,</span> <span style="color: #cc66cc;">5</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000088;">$text</span> <span style="color: #339933;">=</span> <span style="color: #990000;">curl_exec</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #990000;">curl_close</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$text</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> static <span style="color: #000000; font-weight: bold;">function</span> inspect<span style="color: #009900;">&#40;</span><span style="color: #000088;">$provider</span><span style="color: #339933;">,</span><span style="color: #000088;">$xml</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: #339933;">!</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$xml</span><span style="color: #009900;">&#41;</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: #990000;">strtolower</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">trim</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$provider</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
				<span style="color: #b1b100;">case</span> <span style="color: #0000ff;">&quot;bitly&quot;</span><span style="color: #339933;">:</span>
					<span style="color: #000088;">$o</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> SimpleXMLElement<span style="color: #009900;">&#40;</span><span style="color: #000088;">$xml</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
					<span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span>string<span style="color: #009900;">&#41;</span><span style="color: #000088;">$o</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">results</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">nodeKeyVal</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">shortUrl</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;trim&quot;</span><span style="color: #339933;">:</span>
					<span style="color: #000088;">$o</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> SimpleXMLElement<span style="color: #009900;">&#40;</span><span style="color: #000088;">$xml</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
					<span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span>string<span style="color: #009900;">&#41;</span><span style="color: #000088;">$o</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">url</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;isgd&quot;</span><span style="color: #339933;">:</span>
				<span style="color: #b1b100;">case</span> <span style="color: #0000ff;">&quot;hexio&quot;</span><span style="color: #339933;">:</span>
				<span style="color: #b1b100;">default</span><span style="color: #339933;">:</span>
					<span style="color: #b1b100;">return</span> <span style="color: #000088;">$xml</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">private</span> static <span style="color: #000000; font-weight: bold;">function</span> api<span style="color: #009900;">&#40;</span><span style="color: #000088;">$provider</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: #990000;">strtolower</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">trim</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$provider</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
			<span style="color: #b1b100;">case</span> <span style="color: #0000ff;">&quot;bitly&quot;</span><span style="color: #339933;">:</span>
				<span style="color: #000088;">$return</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;http://api.bit.ly/shorten?version=2.0.1&amp;format=xml&amp;longUrl=<span style="color: #009933; font-weight: bold;">%s</span>&amp;login=<span style="color: #009933; font-weight: bold;">%s</span>&amp;apiKey=<span style="color: #009933; font-weight: bold;">%s</span>&quot;</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;isgd&quot;</span><span style="color: #339933;">:</span>
				<span style="color: #000088;">$return</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;http://is.gd/api.php?longurl=<span style="color: #009933; font-weight: bold;">%s</span>&quot;</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;hexio&quot;</span><span style="color: #339933;">:</span>
				<span style="color: #000088;">$return</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;http://hex.io/api-create.php?url=<span style="color: #009933; font-weight: bold;">%s</span>&quot;</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;digg&quot;</span><span style="color: #339933;">:</span>
				<span style="color: #000088;">$return</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;http://services.digg.com/url/short/create?url=<span style="color: #009933; font-weight: bold;">%s</span>&amp;appkey=<span style="color: #009933; font-weight: bold;">%s</span>&amp;type=xml&quot;</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;trim&quot;</span><span style="color: #339933;">:</span>
				<span style="color: #000088;">$return</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;http://api.tr.im/v1/trim_url.xml?url=<span style="color: #009933; font-weight: bold;">%s</span>&quot;</span><span style="color: #339933;">;</span>
				<span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">default</span><span style="color: #339933;">:</span>
				<span style="color: #000088;">$return</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;http://tinyurl.com/api-create.php?url=<span style="color: #009933; font-weight: bold;">%s</span>&quot;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #b1b100;">return</span> <span style="color: #000088;">$return</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p></span></p>
<p><span class="no_translate"></p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</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: #000088;">$url</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'http://www.chazzuka.com/blog/?p=192'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> ShortUrl<span style="color: #339933;">::</span><span style="color: #004000;">create</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'trim'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;hr /&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> ShortUrl<span style="color: #339933;">::</span><span style="color: #004000;">create</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'tinyurl'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;hr /&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> ShortUrl<span style="color: #339933;">::</span><span style="color: #004000;">create</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'isgd'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;hr /&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> ShortUrl<span style="color: #339933;">::</span><span style="color: #004000;">create</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'hexio'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;hr /&gt;'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> ShortUrl<span style="color: #339933;">::</span><span style="color: #004000;">create</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'bitly'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'your_user_name'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'your_api_key'</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></span></p>
<p class="akst_link"><a href="http://www.marcobruni.info/?p=531&amp;akst_action=share-this"  title="Invia il post via mail o salvalo in uno dei servizi di bookmarking sociale..." id="akst_link_531" class="akst_share_link" rel="nofollow">Condividi</a>
</p>]]></content:encoded>
			<wfw:commentRss>http://www.marcobruni.info/php-la-classe-php-per-creare-url-corti-tramite-le-api-di-tinyurl-is-gd-hex-io-tr-im-bit-ly.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
