<?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>rafaelWendel</title>
	<atom:link href="http://www.rafaelwendel.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.rafaelwendel.com</link>
	<description>blog sobre programação e tecnologia</description>
	<lastBuildDate>Wed, 16 May 2012 13:53:58 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>Crie um flip book com jquery</title>
		<link>http://www.rafaelwendel.com/2012/05/crie-um-flip-book-com-jquery/</link>
		<comments>http://www.rafaelwendel.com/2012/05/crie-um-flip-book-com-jquery/#comments</comments>
		<pubDate>Wed, 16 May 2012 13:53:58 +0000</pubDate>
		<dc:creator>Rafael Wendel Pinheiro</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[plugin]]></category>
		<category><![CDATA[Sem categoria]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[flip]]></category>

		<guid isPermaLink="false">http://www.rafaelwendel.com/?p=366</guid>
		<description><![CDATA[No post de hoje vou mostrar pra vocês um forma bem legal de se criar flip books utilizando jquery e o plugin turn.js. Para quem não sabe, flip book é aquele efeito que simula um livro, revista ou catálogo e no qual é possível ir folheando as páginas de forma bem real. Ao término desse [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;"><a href="http://www.rafaelwendel.com/wp-content/uploads/2012/05/flip.jpg"><img class="alignright  wp-image-369" title="flip" src="http://www.rafaelwendel.com/wp-content/uploads/2012/05/flip.jpg" alt="" width="132" height="178" /></a>No post de hoje vou mostrar pra vocês um forma bem legal de se criar <strong>flip books </strong>utilizando <strong>jquery</strong> e o plugin <strong>turn</strong>.<strong>js. </strong>Para quem não sabe, <strong>flip book</strong> é aquele efeito que simula um livro, revista ou catálogo e no qual é possível ir folheando as páginas de forma bem real.</p>
<p style="text-align: justify;">Ao término desse tutorial teremos criado algo semelhante à esse. <a title="Veja o exemplo" href="http://files.rafaelwendel.com/flip" target="_blank">(Clique Aqui)</a>. E para construirmos nossa revista virtual, devemos baixar o <a title="Baixe o Jquery" href="http://files.rafaelwendel.com/jquery.js" target="_blank"><strong>jquery</strong> </a>e o <a title="Baixe o Turn.js" href="http://files.rafaelwendel.com/turn.min.js" target="_blank"><strong>turn.js</strong>.</a></p>
<p style="text-align: justify;"><span id="more-366"></span>Antes de iniciar, vamos criar 4 imagens, todas em tamanho <strong>400/533 pixels.</strong> Cada uma dessas imagens será uma página na revista. Crie um diretório <strong>img </strong>e salve as imagens dentro dele.</p>
<p style="text-align: justify;">Vamos implementar o arquivo <strong>index.html</strong></p>
<p style="text-align: justify;">

<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
</pre></td><td class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #00bbdd;">&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">html</span> xmlns<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://www.w3.org/1999/xhtml&quot;</span> <span style="color: #000066;">dir</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;ltr&quot;</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">head</span> <span style="color: #000066;">profile</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://gmpg.org/xfn/11&quot;</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">meta</span> <span style="color: #000066;">http-equiv</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Content-Type&quot;</span> <span style="color: #000066;">content</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/html; charset=UTF-8&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">title</span>&gt;</span>Criando um flip book com JQuery (www.rafaelwendel.com)<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">title</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;jquery.js&quot;</span> &gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;turn.min.js&quot;</span> &gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span>&gt;</span>
$(document).ready(function(){
    $('#magazine').turn();
});
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">style</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/css&quot;</span>&gt;</span>
    #magazine{
        width: 800px;
        height: 533px;
    }
&nbsp;
    #magazine .turn-page{
        width: 400px;
        height: 533px;
        background-color: #ccc;
    }
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">style</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">head</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">h2</span>&gt;</span>Criando um flip book com JQuery (www.rafaelwendel.com)<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">h2</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;magazine&quot;</span>&gt;</span>
        <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">img</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;img/imagem1.jpg&quot;</span> <span style="color: #66cc66;">/</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
        <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">img</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;img/imagem2.jpg&quot;</span> <span style="color: #66cc66;">/</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
        <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">img</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;img/imagem3.jpg&quot;</span> <span style="color: #66cc66;">/</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
        <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">img</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;img/imagem4.jpg&quot;</span> <span style="color: #66cc66;">/</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">html</span>&gt;</span></pre></td></tr></table></div>

</p>
<p style="text-align: justify;">Para definir uma <strong>div </strong>do documento como sendo <strong>flip</strong> é simples. Basta chamar a função <strong>turn </strong>(linha 10). Na estilização a largura eu defino o dobro da largura da imagem (para a revista aberta). E a altura é a mesma da imagem, pois se trata de cada página. Na classe <strong>turn-page </strong>coloco exatamente as dimensões das imagens.</p>
<p style="text-align: justify;">Para passar a página, basta ir no <strong>canto inferior direito</strong> e arrastar com o mouse.</p>
<p style="text-align: justify;">Para visualizar o exemplo, <a title="Veja o exemplo" href="http://files.rafaelwendel.com/flip" target="_blank">clique aqui.</a></p>
<p style="text-align: justify;">Para baixar os arquivos do exemplo, <a title="Baixe os arquivos" href="http://files.rafaelwendel.com/flip.rar" target="_blank">clique aqui.</a></p>
<p style="text-align: justify;">Site oficial do turn.js, <a title="Site oficial do Turn.js" href="http://www.turnjs.com/" target="_blank">clique aqui.</a></p>
<p style="text-align: justify;">Qualquer dúvida, é só comentar.</p>
<p style="text-align: justify;">Abs!</p>
<p>Siga-me no twitter: <a href="http://www.twitter.com/rafaelwendel" target="blank">@rafaelwendel</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.rafaelwendel.com/2012/05/crie-um-flip-book-com-jquery/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Função para somar data em php</title>
		<link>http://www.rafaelwendel.com/2012/05/funcao-para-somar-data-em-php/</link>
		<comments>http://www.rafaelwendel.com/2012/05/funcao-para-somar-data-em-php/#comments</comments>
		<pubDate>Tue, 08 May 2012 02:12:30 +0000</pubDate>
		<dc:creator>Rafael Wendel Pinheiro</dc:creator>
				<category><![CDATA[dica rápida]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[datas]]></category>
		<category><![CDATA[função]]></category>

		<guid isPermaLink="false">http://www.rafaelwendel.com/?p=362</guid>
		<description><![CDATA[Olá pessoal! No post de hoje vou mostrar pra vocês uma função que com certeza já lhe fez falta algum dia ou que ainda lhe será útil em um projeto futuro. Trata-se de uma função para somar datas. Com ela é possível se obter uma nova data através da inserção de dias, meses e anos. [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">Olá pessoal! No post de hoje vou mostrar pra vocês uma função que com certeza já lhe fez falta algum dia ou que ainda lhe será útil em um projeto futuro.</p>
<p style="text-align: justify;">Trata-se de uma função para <strong>somar datas. </strong>Com ela é possível se obter uma nova data através da inserção de dias, meses e anos.</p>
<p style="text-align: justify;"><span id="more-362"></span>Vamos à implementação da função:</p>
<p style="text-align: justify;">

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</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: #000000; font-weight: bold;">function</span> somar_data<span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #339933;">,</span> <span style="color: #000088;">$dias</span><span style="color: #339933;">,</span> <span style="color: #000088;">$meses</span><span style="color: #339933;">,</span> <span style="color: #000088;">$ano</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$data</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;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #000088;">$resData</span> <span style="color: #339933;">=</span> <span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;d/m/Y&quot;</span><span style="color: #339933;">,</span> <span style="color: #990000;">mktime</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #000088;">$data</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;">$meses</span><span style="color: #339933;">,</span> <span style="color: #000088;">$data</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;">$dias</span><span style="color: #339933;">,</span> <span style="color: #000088;">$data</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;">$ano</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">return</span> <span style="color: #000088;">$resData</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

</p>
<p style="text-align: justify;">Função extremamente fácil de ser utilizada. Basta passar uma data qualquer no formato <strong>dd/mm/aaaa </strong>e os <strong>dias, meses </strong>e <strong>anos </strong>a se contabilizar (respectivamente).</p>
<p style="text-align: justify;">Veja os exemplos à seguir:</p>
<p style="text-align: justify;">

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</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;">/* Somar 45 dias ao dia 07/05/2012 */</span>
<span style="color: #b1b100;">echo</span> somar_data<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'07/05/2012'</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">45</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">/* Imprime 21/06/2012 */</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/* Somar 6 meses ao dia 28/02/2012  */</span>
<span style="color: #b1b100;">echo</span> somar_data<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'28/02/2012'</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">6</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">/* Imprime 28/08/2012 */</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/* Somar 2 anos, 3 meses e 10 dias ao dia 01/01/2012 */</span>
<span style="color: #b1b100;">echo</span> somar_data<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'01/01/2012'</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">/* Imprime 11/04/2014 */</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

</p>
<p style="text-align: justify;">Desfrutem sem moderação.</p>
<p style="text-align: justify;">Abs!</p>
<p>Siga-me no twitter: <a href="http://www.twitter.com/rafaelwendel" target="blank">@rafaelwendel</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.rafaelwendel.com/2012/05/funcao-para-somar-data-em-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Validando formulários com JQuery</title>
		<link>http://www.rafaelwendel.com/2012/04/validando-formularios-com-jquery/</link>
		<comments>http://www.rafaelwendel.com/2012/04/validando-formularios-com-jquery/#comments</comments>
		<pubDate>Wed, 18 Apr 2012 13:15:23 +0000</pubDate>
		<dc:creator>Rafael Wendel Pinheiro</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[plugin]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[validar formulários]]></category>
		<category><![CDATA[validate]]></category>

		<guid isPermaLink="false">http://www.rafaelwendel.com/?p=352</guid>
		<description><![CDATA[Fala pessoal! Infelizmente o tempo para adicionar conteúdo novo aqui no blog está muito escasso, mas hoje consegui uma &#8220;folga&#8221; para escrever um post bem interessante que aborda a validação de formulários utilizando jquery. Com o plugin validate vocês verão como é fácil verificar se os campos obrigatórios foram preenchidos, se o campo de email [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">Fala pessoal! Infelizmente o tempo para adicionar conteúdo novo aqui no blog está muito escasso, mas hoje consegui uma &#8220;folga&#8221; para escrever um post bem interessante que aborda a <strong>validação de formulários utilizando jquery.</strong></p>
<p style="text-align: justify;"><strong></strong>Com o plugin <strong>validate </strong>vocês verão como é fácil verificar se os <strong>campos obrigatórios</strong> foram preenchidos, se o campo de email possui um <strong>email válido</strong>, se um arquivo possui a extensão permitida, etc.</p>
<p style="text-align: justify;"><span id="more-352"></span></p>
<p style="text-align: justify;">Para acompanhar o post, recomendo que façam o download do <a title="Baixe o JQuery 1.6.2" href="http://www.rafaelwendel.com/outros/validate/jquery-1.6.2.js" target="_blank"><strong>jquery</strong></a> e do plugin <strong><a title="Baixe o publigin JQuery Validate" href="http://www.rafaelwendel.com/outros/validate/jquery.validate.js" target="_blank">validate</a>.</strong></p>
<p style="text-align: justify;">Feito isso, vamos organizar os arquivos do nosso projeto da seguinte maneira</p>
<ul style="text-align: justify;">
<li>index.html</li>
<li>jquery-1.6.2.js</li>
<li>jquery.validate.js</li>
<li>validar.js</li>
</ul>
<p style="text-align: justify;">No arquivo <strong>index.html </strong>colocaremos o formulário e no <strong>validar.js</strong> criaremos as regras de validação do mesmo.</p>
<p style="text-align: justify;">Implementação do arquivo index.html</p>
<p style="text-align: justify;">

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #00bbdd;">&lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot;&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">html</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">head</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">title</span>&gt;</span>Validando Formulários com JQuery<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">title</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">meta</span> <span style="color: #000066;">http-equiv</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Content-Type&quot;</span> <span style="color: #000066;">content</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/html; charset=UTF-8&quot;</span>&gt;</span>
        <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;jquery-1.6.2.js&quot;</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
        <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;jquery.validate.js&quot;</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
        <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;validar.js&quot;</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
        <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">style</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/css&quot;</span>&gt;</span>
            /* Estilizar os alertas */
            label.error{
                padding-left: 5px;
                color: red;
                font-weight: bold;
            }
        <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">style</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">head</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">h1</span>&gt;</span>Validando Formulário com JQuery<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">h1</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">form</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;form1&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;form1&quot;</span> <span style="color: #000066;">method</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;post&quot;</span> <span style="color: #000066;">enctype</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;multipart/form-data&quot;</span>&gt;</span>
        <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">label</span> <span style="color: #000066;">for</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;nome&quot;</span>&gt;</span>Nome:<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">label</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">br</span> <span style="color: #66cc66;">/</span>&gt;</span>
        <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;nome&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;nome&quot;</span> <span style="color: #000066;">size</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;45&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
        <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">br</span> <span style="color: #66cc66;">/</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">br</span> <span style="color: #66cc66;">/</span>&gt;</span>
        <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">label</span> <span style="color: #000066;">for</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;email&quot;</span>&gt;</span>Email:<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">label</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">br</span> <span style="color: #66cc66;">/</span>&gt;</span>
        <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;email&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;email&quot;</span> <span style="color: #000066;">size</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;45&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
        <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">br</span> <span style="color: #66cc66;">/</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">br</span> <span style="color: #66cc66;">/</span>&gt;</span>
        <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">label</span> <span style="color: #000066;">for</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;idade&quot;</span>&gt;</span>Idade:<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">label</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">br</span> <span style="color: #66cc66;">/</span>&gt;</span>
        <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;idade&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;idade&quot;</span> <span style="color: #000066;">size</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;2&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
        <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">br</span> <span style="color: #66cc66;">/</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">br</span> <span style="color: #66cc66;">/</span>&gt;</span>
        <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">label</span> <span style="color: #000066;">for</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;foto&quot;</span>&gt;</span>Foto:<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">label</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">br</span> <span style="color: #66cc66;">/</span>&gt;</span>
        <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;file&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;foto&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;foto&quot;</span> <span style="color: #000066;">size</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;45&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
        <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">br</span> <span style="color: #66cc66;">/</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">br</span> <span style="color: #66cc66;">/</span>&gt;</span>
        <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;submit&quot;</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Enviar Dados&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">form</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">p</span> <span style="color: #000066;">align</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;center&quot;</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://www.rafaelwendel.com&quot;</span> <span style="color: #000066;">title</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Visite meu blog periodicamente&quot;</span>&gt;</span>www.rafaelwendel.com<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">p</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">html</span>&gt;</span></pre></div></div>

</p>
<p style="text-align: justify;">No cabeçalho faço a inclusão dos 3 arquivos javascripts e também coloco uma regra de estilo <strong>css </strong>para deixar os alertas melhores apresentados. Crio o formulário (<strong>form1</strong>) e defino 3 campos de textos dentro dele (<strong>nome, email </strong>e <strong>idade</strong>) e 1 campo de arquivo (<strong>file</strong>).</p>
<p style="text-align: justify;">Agora vamos definir algumas regras de validação para cada um desses campos.  Por exemplo: o campo <strong>nome </strong>será obrigatório e deverá ter ao menos 5 caracteres. O campo <strong>email </strong>também será obrigatório e deverá ser um email válido. O campo <strong>idade </strong>só aceitará valores acima de 18. E o campo <strong>foto </strong>só aceita arquivos com extensão de imagem (<strong>jpg </strong>e <strong>png</strong>).</p>
<p style="text-align: justify;">Veja o arquivo <strong>validar.js</strong></p>
<p style="text-align: justify;"><strong></strong></p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><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;">&quot;#form1&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">validate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #009966; font-style: italic;">/* REGRAS DE VALIDAÇÃO DO FORMULÁRIO */</span>
		rules<span style="color: #339933;">:</span><span style="color: #009900;">&#123;</span>
			nome<span style="color: #339933;">:</span><span style="color: #009900;">&#123;</span>
				required<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span> <span style="color: #009966; font-style: italic;">/* Campo obrigatório */</span>
                                minlength<span style="color: #339933;">:</span> <span style="color: #CC0000;">5</span>    <span style="color: #009966; font-style: italic;">/* No mínimo 5 caracteres */</span>
			<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
			email<span style="color: #339933;">:</span><span style="color: #009900;">&#123;</span>
				required<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span> <span style="color: #009966; font-style: italic;">/* Campo obrigatório */</span>
                                email<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">true</span>     <span style="color: #009966; font-style: italic;">/* Deverá ser um email válido */</span>
			<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
                        idade<span style="color: #339933;">:</span><span style="color: #009900;">&#123;</span>
                                required<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span> <span style="color: #009966; font-style: italic;">/* Campo obrigatório */</span>
                                min<span style="color: #339933;">:</span> <span style="color: #CC0000;">18</span>         <span style="color: #009966; font-style: italic;">/* Valor mínimo permitido: 18 */</span>
                        <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
                        foto<span style="color: #339933;">:</span><span style="color: #009900;">&#123;</span>
                                accept<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;jpg|png&quot;</span> <span style="color: #009966; font-style: italic;">/* Só aceita arquivos com extensão .jpg e .png */</span>
                        <span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
		<span style="color: #009966; font-style: italic;">/* DEFINIÇÃO DAS MENSAGENS DE ERRO */</span>
		messages<span style="color: #339933;">:</span><span style="color: #009900;">&#123;</span>
			nome<span style="color: #339933;">:</span><span style="color: #009900;">&#123;</span>
				required<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Preencha o campo &lt;u&gt;Nome&lt;/u&gt;&quot;</span><span style="color: #339933;">,</span>
				minlength<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;O campo &lt;u&gt;Nome&lt;/u&gt; deve conter no mínimo 5 caracteres&quot;</span>
			<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
			email<span style="color: #339933;">:</span><span style="color: #009900;">&#123;</span>
				required<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Preencha o campo &lt;u&gt;Email&lt;/u&gt;&quot;</span><span style="color: #339933;">,</span>
				email<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;O campo &lt;u&gt;Email&lt;/u&gt; só aceita emails válidos&quot;</span>
			<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
                        idade<span style="color: #339933;">:</span><span style="color: #009900;">&#123;</span>
                                required<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Preencha o campo &lt;u&gt;Idade&lt;/u&gt;&quot;</span><span style="color: #339933;">,</span>
                                min<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Você deve ser maior de idade&quot;</span>
                        <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
                        foto<span style="color: #339933;">:</span><span style="color: #009900;">&#123;</span>
                                accept<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Extensão de arquivo inválida&quot;</span>
                        <span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</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></div></div>

</p>
<p style="text-align: justify;">Implementação bem simples. Na primeira parte definimos todas as regras para os campos dos formulários. E na segunda personalizamos as <strong>mensagens/alertas</strong> de erro para cada uma das regras.</p>
<p style="text-align: justify;">Além dos métodos que utilizamos em nosso exemplo (<strong>required, email, minlength, min</strong> e <strong>accept</strong>) o plugin ainda oferece diversos outros. Veja:</p>
<ul>
<li><strong><a title="Plugins/Validation/Methods/maxlength" href="http://docs.jquery.com/Plugins/Validation/Methods/maxlength#length">maxlength( length )</a></strong>: Máximo de caracteres</li>
<li><strong><a title="Plugins/Validation/Methods/rangelength" href="http://docs.jquery.com/Plugins/Validation/Methods/rangelength#range">rangelength( range )</a></strong>: Faz com que o elemento requer um intervalo de valores dado</li>
<li><strong><a title="Plugins/Validation/Methods/max" href="http://docs.jquery.com/Plugins/Validation/Methods/max#value">max( value )</a></strong>: Valor máximo permitido</li>
<li><strong><a title="Plugins/Validation/Methods/url" href="http://docs.jquery.com/Plugins/Validation/Methods/url">url( )</a></strong>: URL válida</li>
<li><strong><a title="Plugins/Validation/Methods/date" href="http://docs.jquery.com/Plugins/Validation/Methods/date">date( )</a></strong>: Data válida</li>
<li><strong><a title="Plugins/Validation/Methods/dateISO" href="http://docs.jquery.com/Plugins/Validation/Methods/dateISO">dateISO( )</a></strong>: Data ISO válida</li>
<li><strong><a title="Plugins/Validation/Methods/number" href="http://docs.jquery.com/Plugins/Validation/Methods/number">number( )</a></strong>: Campo numérico</li>
<li><strong><a title="Plugins/Validation/Methods/digits" href="http://docs.jquery.com/Plugins/Validation/Methods/digits">digits( )</a></strong>: Só aceita dígitos</li>
<li><strong><a title="Plugins/Validation/Methods/creditcard" href="http://docs.jquery.com/Plugins/Validation/Methods/creditcard">creditcard( )</a></strong>: Um número de cartão de crédito</li>
<li><strong><a title="Plugins/Validation/Methods/equalTo" href="http://docs.jquery.com/Plugins/Validation/Methods/equalTo#other">equalTo( other )</a></strong>: igual à um determinado valor</li>
</ul>
<p>Para ver como ficou o nosso exemplo, <a title="Documentação do Plugin Validate" href="http://www.rafaelwendel.com/outros/validate/" target="_blank">clique aqui</a>. Para mais informações sobre o plugin, visitem <a title="Documentação do Plugin Validate" href="http://docs.jquery.com/Plugins/Validation" target="_blank">http://docs.jquery.com/Plugins/Validation</a></p>
<p>Espero que tenham gostado.</p>
<p>Abs!</p>
<p style="text-align: justify;">Siga-me no twitter: <a href="http://www.twitter.com/rafaelwendel" target="blank">@rafaelwendel</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.rafaelwendel.com/2012/04/validando-formularios-com-jquery/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Vídeo-aula: Criando um webservice em php [Aula II]</title>
		<link>http://www.rafaelwendel.com/2012/03/video-aula-criando-um-webservice-em-php-aula-2/</link>
		<comments>http://www.rafaelwendel.com/2012/03/video-aula-criando-um-webservice-em-php-aula-2/#comments</comments>
		<pubDate>Sat, 24 Mar 2012 14:39:09 +0000</pubDate>
		<dc:creator>Rafael Wendel Pinheiro</dc:creator>
				<category><![CDATA[api]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[video-aulas]]></category>
		<category><![CDATA[xml]]></category>
		<category><![CDATA[webservice]]></category>

		<guid isPermaLink="false">http://www.rafaelwendel.com/?p=348</guid>
		<description><![CDATA[Fala galera, Seguindo com a parte 2 das vídeo-aulas sobre como criar um webservice em php. Nessa aula configuro a o webservice implementando o retorno xml através da classe que crimos na aula I. Faço também o upload dos arquivos e os testes. Qualquer dúvida utilize a caixa de comentários. Siga-me no twitter: @rafaelwendel]]></description>
			<content:encoded><![CDATA[<p>Fala galera,</p>
<p style="text-align: justify;">Seguindo com a parte 2 das vídeo-aulas sobre como criar um webservice em php. Nessa aula configuro a o webservice implementando o retorno xml através da classe que crimos na aula I. Faço também o upload dos arquivos e os testes.</p>
<p style="text-align: justify;"><span id="more-348"></span></p>
<p style="text-align: justify;">Qualquer dúvida utilize a caixa de comentários.</p>
<p style="text-align: justify;"><iframe src="http://www.youtube.com/embed/fHPYasJzcw8?hl=pt&amp;fs=1" frameborder="0" width="425" height="349"></iframe></p>
<p>Siga-me no twitter: <a href="http://www.twitter.com/rafaelwendel" target="blank">@rafaelwendel</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.rafaelwendel.com/2012/03/video-aula-criando-um-webservice-em-php-aula-2/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Vídeo-aula: Criando um webservice em php [Aula I]</title>
		<link>http://www.rafaelwendel.com/2012/03/video-aula-criando-um-webservice-em-php-aula-1/</link>
		<comments>http://www.rafaelwendel.com/2012/03/video-aula-criando-um-webservice-em-php-aula-1/#comments</comments>
		<pubDate>Fri, 23 Mar 2012 20:44:16 +0000</pubDate>
		<dc:creator>Rafael Wendel Pinheiro</dc:creator>
				<category><![CDATA[api]]></category>
		<category><![CDATA[classes]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[xml]]></category>
		<category><![CDATA[webservice]]></category>

		<guid isPermaLink="false">http://www.rafaelwendel.com/?p=345</guid>
		<description><![CDATA[Fala galera! Depois de algum tempo sem vídeo-aulas hoje ela está de volta. E com um assunto que com certeza despertará a atenção de muitos usuários. Nela eu ensino a montar um webservice em php. Isso mesmo, uma API onde é possível passar um parâmetro e receber um retorno em forma de XML para ser [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">Fala galera! Depois de algum tempo sem <strong>vídeo-aulas</strong> hoje ela está de volta. E com um assunto que com certeza despertará a atenção de muitos usuários. Nela eu ensino a<strong> montar um webservice em php.</strong></p>
<p style="text-align: justify;">Isso mesmo, uma <strong>API</strong> onde é possível passar um parâmetro e receber um retorno em forma de XML para ser utilizado em qualquer linguagem.</p>
<p style="text-align: justify;"><span id="more-345"></span></p>
<p style="text-align: justify;">Nessa aula I montamos o banco de dados e criamos a classe <strong>XML</strong> que nos auxilia a criar a estrutura da linguagem de marcação de forma fácil, orientada a objetos e com código identado.</p>
<p style="text-align: justify;">Utilizei como exemplo um webservice que recebe o código de um determinado produto e retorna o nome e o valor desse produto a partir de uma consulta em B.D MySQL.</p>
<p style="text-align: justify;"><iframe src="http://www.youtube.com/embed/uhdIDUFS4qs?hl=pt&amp;fs=1" frameborder="0" width="425" height="349"></iframe></p>
<p>Siga-me no twitter: <a href="http://www.twitter.com/rafaelwendel" target="blank">@rafaelwendel</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.rafaelwendel.com/2012/03/video-aula-criando-um-webservice-em-php-aula-1/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Método mágico autoload</title>
		<link>http://www.rafaelwendel.com/2012/03/metodo-magico-autoload/</link>
		<comments>http://www.rafaelwendel.com/2012/03/metodo-magico-autoload/#comments</comments>
		<pubDate>Wed, 21 Mar 2012 13:35:38 +0000</pubDate>
		<dc:creator>Rafael Wendel Pinheiro</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.rafaelwendel.com/?p=338</guid>
		<description><![CDATA[Nada mais desgastante para um programador quando ele está desenvolvendo um projeto que fatalmente possui inúmeras classes e em certos locais ele precisa utilizar 5, 6 ou até 10 dessas classes e para importar todas as classes ele precisa de muitos requires ou includes. Mas a partir do PHP 5 a resolução desse problema ficou [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">Nada mais desgastante para um programador quando ele está desenvolvendo um projeto que fatalmente possui inúmeras classes e em certos locais ele precisa utilizar 5, 6 ou até 10 dessas classes e para importar todas as classes ele precisa de muitos <strong>requires </strong>ou <strong>includes.</strong></p>
<p style="text-align: justify;">Mas a partir do <strong>PHP 5</strong> a resolução desse problema ficou relativamente simples graças ao método mágico<strong> __autoload</strong>. Esse método faz as importações das classes por demanda de acordo com a instanciação dos obejtos. Ou seja, ele fará automaticamente o carregamento da classe <strong>X</strong> apenas quando um objeto da mesma for instanciada. Isso gera menos dores de cabeça ao programador que não precisa importar manualmente todas as classes que for utilizar e ainda deixa o sistema mais leve uma vez que apenas as classes que estão sendo utilizadas serão importadas.</p>
<p style="text-align: justify;"><span id="more-338"></span></p>
<p style="text-align: justify;">Para utlizar o método <strong>__autoload</strong> é fácil. Vamos imaginar que estou trabalhando em um arquivo qualquer na raiz do meu projeto e tenho todas as minhas classes implementadas dentro do diretórios <strong>Classes. </strong></p>
<p style="text-align: justify;"><strong></strong>No exemplo abaixo eu faço a função mágica e logo depois já instancio uma de minhas classes.</p>
<p style="text-align: justify;">

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</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: #000000; font-weight: bold;">function</span> __autoload<span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</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: #990000;">file_exists</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Classes/'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$file</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'.php'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            <span style="color: #b1b100;">require_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Classes/'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$file</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'.php'</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>
            <span style="color: #990000;">exit</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'O arquivo '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$file</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">' não foi encontrado!'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$Animal</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Animal<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Cachorro'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

</p>
<p style="text-align: justify;">Quando eu instancio uma classe, automaticamente o método é executado. Ele verifica se o arquivo existe e faz o <strong>require</strong> dele. Caso não consiga encontrar ele finaliza exibindo uma mensagem.</p>
<p style="text-align: justify;">Você pode fazer os ajustes que desejar dentro da função para adequá-la às disposições de seus diretórios e classes.</p>
<p style="text-align: justify;">Até a próxima!</p>
<p style="text-align: justify;">Abs.</p>
<p style="text-align: justify;">
<p style="text-align: justify;">Siga-me no twitter: <a href="http://www.twitter.com/rafaelwendel" target="blank">@rafaelwendel</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.rafaelwendel.com/2012/03/metodo-magico-autoload/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Utilizando AJAX com JQUERY</title>
		<link>http://www.rafaelwendel.com/2012/03/utilizando-ajax-com-jquery/</link>
		<comments>http://www.rafaelwendel.com/2012/03/utilizando-ajax-com-jquery/#comments</comments>
		<pubDate>Mon, 12 Mar 2012 21:19:38 +0000</pubDate>
		<dc:creator>Rafael Wendel Pinheiro</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[plugin]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[ajax]]></category>

		<guid isPermaLink="false">http://www.rafaelwendel.com/?p=336</guid>
		<description><![CDATA[Salve galera! Hoje vai rolar um post sobre dois assuntos que são indispensáveis no desenvolvimento web atual. São eles o AJAX (Asynchronous Javascript and XML) e o plugin JQUERY (Write Less, Do More). Na verdade &#8220;meio&#8221; que englobaremos ambos os assuntos em um só. Veremos como utilizar as famosas requisições assíncronas conhecidas como AJAX utilizando [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">Salve galera! Hoje vai rolar um post sobre dois assuntos que são indispensáveis no desenvolvimento web atual. São eles o AJAX (<em>Asynchronous Javascript and XML</em>) e o plugin <a title="Visite a página do plugin" href="http://jquery.com/" target="_blank">JQUERY </a>(Write Less, Do More).</p>
<p style="text-align: justify;">Na verdade &#8220;meio&#8221; que englobaremos ambos os assuntos em um só. Veremos como utilizar as famosas <strong>requisições assíncronas </strong>conhecidas como <strong>AJAX </strong>utilizando o plugin javascript mais famoso do mundo!</p>
<p style="text-align: justify;"><span id="more-336"></span></p>
<p style="text-align: justify;">Antes de começarmos com os <strong>códigos</strong>, vale lembrar que AJAX são técnicas aplicadas à sites/sistemas web para tornar a aplicação mais amigável ao usuário. Através dele é possível trocar informações entre o <strong>browser (cliente) </strong>e o <strong>servidor </strong>de forma invisível, instantânea e <strong>sem carregamento de páginas</strong>.</p>
<p style="text-align: justify;">A utilização de AJAX pode ser feita através do javascript puro, através de plugins/frameworks js ou através de classes/bibliotecas de linguagens <em>server-sides </em>como o <a title="Veja o site oficial do projeto" href="http://xajaxproject.org/" target="_blank">XAJAX</a> (escrito em PHP). Na minha opinião a forma <strong>mais fácil</strong> é através do <strong>JQUERY.</strong></p>
<p style="text-align: justify;"><strong></strong>E é exemplificando ele que vou mostrar para vocês como fazer uma inserção em uma tabela do banco de dados utilizando AJAX.</p>
<p style="text-align: justify;">Crie um banco de dados com a tabela <strong>usuario(idusuario </strong>e <strong>nome). </strong>Organize seus arquivos da seguinte maneira.</p>
<ul>
<li><strong>index.php</strong> (Arquivo principal onde vai o formulário)</li>
<li><strong>ajax.js </strong>(arquivo javascript onde vai a requisição)</li>
<li><strong>inserir.php </strong>(arquivo que insere as informações no banco de dados)</li>
</ul>
<p style="text-align: justify;">Vamos iniciar pela implementação do arquivo <strong>index.php</strong></p>
<p style="text-align: justify;">

<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
</pre></td><td class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">html</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">head</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">meta</span> <span style="color: #000066;">http-equiv</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Content-Type&quot;</span> <span style="color: #000066;">content</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/html; charset=UTF-8&quot;</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">title</span>&gt;</span>AJAX com JQuery<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">title</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://code.jquery.com/jquery-1.7.1.js&quot;</span> &gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;ajax.js&quot;</span> &gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">head</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">h1</span>&gt;</span>AJAX com Jquery<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">h1</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">form</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;form1&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;form1&quot;</span>&gt;</span>
    Código: <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">br</span> <span style="color: #66cc66;">/</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;idusuario&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;idusuario&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">br</span> <span style="color: #66cc66;">/</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">br</span> <span style="color: #66cc66;">/</span>&gt;</span>
    Nome: <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">br</span> <span style="color: #66cc66;">/</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;nome&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;nome&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">br</span> <span style="color: #66cc66;">/</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;button&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;enviar&quot;</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Enviar Dados&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">form</span>&gt;</span>
&nbsp;
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;resultado&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
&nbsp;
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">html</span>&gt;</span></pre></td></tr></table></div>

</p>
<p style="text-align: justify;">Nessa página colocamos o formulário com os campos <strong>Código </strong>e <strong>Nome. </strong>Vale observar que no cabeçalho do documento eu <strong>importo </strong>o <strong>jquery </strong>(do site oficial) e o <strong>ajax.js. </strong>Logo abaixo do <em>form</em> coloco uma <strong>div</strong> de <strong>id</strong> &#8220;resultado&#8221; que será onde a resposta de nossa requisição será exibida.</p>
<p style="text-align: justify;">Feito isso, vamos à implementação do arquivo <strong>ajax.js</strong></p>
<p style="text-align: justify;">

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">//arquivo ajax.js</span>
$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><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;">'#enviar'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><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: #660066;">ajax</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
            url <span style="color: #339933;">:</span> <span style="color: #3366CC;">'inserir.php'</span><span style="color: #339933;">,</span>
            type <span style="color: #339933;">:</span> <span style="color: #3366CC;">'POST'</span><span style="color: #339933;">,</span>
            data <span style="color: #339933;">:</span> <span style="color: #3366CC;">'idusuario='</span> <span style="color: #339933;">+</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#idusuario'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&amp;nome='</span> <span style="color: #339933;">+</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#nome'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
            success<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#resultado'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</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>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span></pre></td></tr></table></div>

</p>
<p style="text-align: justify;">Nesse arquivo defino que a ação deverá ser executada quando o botão <strong>#enviar</strong> for clicado. Na função utilizamos a variável <strong>$.ajax </strong>passando como parâmetro 4 informações bem importantes.</p>
<ul>
<li><strong>url </strong>(documento que será requisitado)</li>
<li><strong>type</strong> (tipo da requisição: GET, POST, PUT ou DELETE)</li>
<li><strong>data</strong> (os dados que serão enviados )</li>
<li><strong>success</strong> (ação que deve executar em caso de sucesso na requisição)</li>
</ul>
<p style="text-align: justify;">Por fim, nos falta implementar o arquivo <strong>inserir.php</strong></p>
<p style="text-align: justify;"><strong></strong></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
</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;">//arquivo inserir.php</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/* CONECTA COM O BANCO DE DADOS */</span>
<span style="color: #990000;">mysql_connect</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'localhost'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'usuario'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'senha'</span><span style="color: #009900;">&#41;</span> or <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Erro ao conectar com o servidor'</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: #0000ff;">'nome_do_banco'</span><span style="color: #009900;">&#41;</span> or <span style="color: #990000;">die</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Erro ao selecionar o banco de dados'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/* VARIÁVEL PARA VALIDAR O FORMULÁRIO */</span>
<span style="color: #000088;">$valida</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/* VERIFICA SE O CAMPO CODIGO FOI PREENCHIDO */</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'idusuario'</span><span style="color: #009900;">&#93;</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;">$retorno</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;Preencha o campo Código &lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$valida</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>
&nbsp;
<span style="color: #666666; font-style: italic;">/* VERIFICA SE O CAMPO NOME FOI PREENCHIDO */</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'nome'</span><span style="color: #009900;">&#93;</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;">$retorno</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;Preencha o campo Nome &lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$valida</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>
&nbsp;
<span style="color: #666666; font-style: italic;">/* SE OS VALORES FOREM VÁLIDOS, INSERE NO BANCO DE DADOS  */</span>
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$valida</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$idusuario</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'idusuario'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$nome</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'nome'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;INSERT INTO usuario(idusuario, nome) VALUES ('<span style="color: #006699; font-weight: bold;">$idusuario</span>', '<span style="color: #006699; font-weight: bold;">$nome</span>')&quot;</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: #990000;">mysql_affected_rows</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$retorno</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'Dados inseridos com sucesso!'</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">else</span><span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$retorno</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'Erro ao inserir os dados'</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;">/* IMPRIME RETORNO */</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$retorno</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

</p>
<p style="text-align: justify;">Fazemos a conexão com o banco de dados e depois uma simples validação dos dados do formulário. Se a validação estiver &#8220;OK&#8221; executamos a <em>query </em>no banco de dados e fazemos a inserção. Por fim, damos um <em>echo </em>na variável <strong>$retorno</strong> para exibir as mensagens de <strong>sucesso/</strong><strong>erro </strong>ou os <strong>alertas</strong>.</p>
<p style="text-align: justify;">E pronto! Sua inserção de dados no banco via AJAX já está funcionando perfeitamente.</p>
<p style="text-align: justify;">Espero que tenham gostado</p>
<p style="text-align: justify;">Abs.</p>
<p>Siga-me no twitter: <a href="http://www.twitter.com/rafaelwendel" target="blank">@rafaelwendel</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.rafaelwendel.com/2012/03/utilizando-ajax-com-jquery/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Classe php para integrar com correios e recuperar informações de frete</title>
		<link>http://www.rafaelwendel.com/2012/03/classe-php-para-integrar-com-correios-e-recuperar-informacoes-de-frete/</link>
		<comments>http://www.rafaelwendel.com/2012/03/classe-php-para-integrar-com-correios-e-recuperar-informacoes-de-frete/#comments</comments>
		<pubDate>Thu, 08 Mar 2012 20:54:51 +0000</pubDate>
		<dc:creator>Rafael Wendel Pinheiro</dc:creator>
				<category><![CDATA[api]]></category>
		<category><![CDATA[classes]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[xml]]></category>
		<category><![CDATA[classe]]></category>
		<category><![CDATA[correios]]></category>
		<category><![CDATA[web service]]></category>

		<guid isPermaLink="false">http://www.rafaelwendel.com/?p=329</guid>
		<description><![CDATA[Olá galera! O post de hoje é dedicado aos developers voltados para a área de e-commerce ou mesmo para aqueles que interessam entrar nela. Trata-se de uma pequena classe que implementei e que é voltada para a integração de uma aplicação php com o web service dos Correios. Através dessa classe você passa o serviço, [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">Olá galera! O post de hoje é dedicado aos <em>developers </em>voltados para a área de <em>e-commerce</em> ou mesmo para aqueles que interessam entrar nela.</p>
<p style="text-align: justify;">Trata-se de uma pequena classe que implementei e que é voltada para a integração de uma aplicação php com o <em>web service </em>dos <a title="Visite a página dos correios" href="http://www.correios.com.br" target="_blank"><strong>Correios</strong></a>. Através dessa classe você passa o serviço, o cep de origem e de destino o peso da encomenda e ele te retorna via <strong>XML</strong> informações relevantes como <strong>valor do frete </strong>e<strong> prazo de entrega</strong>.</p>
<p style="text-align: justify;">Então vamos ao código da classe e exemplo de como utilizá-la.</p>
<p style="text-align: justify;"><span id="more-329"></span></p>
<p style="text-align: justify;">

<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
</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: #009933; font-style: italic;">/**
 * Description of Frete.class.php
 *
 * @author Rafael Wendel Pinheiro
 */</span>
<span style="color: #000000; font-weight: bold;">class</span> Frete <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$cod_servico</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$cep_origem</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$cep_destino</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$peso</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$altura</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'4'</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$largura</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'12'</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$comprimento</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'16'</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$valor</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'10.00'</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$erro</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getCep_origem<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">cep_origem</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setCep_origem<span style="color: #009900;">&#40;</span><span style="color: #000088;">$cep_origem</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;">cep_origem</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$cep_origem</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getCep_destino<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">cep_destino</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setCep_destino<span style="color: #009900;">&#40;</span><span style="color: #000088;">$cep_destino</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;">cep_destino</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$cep_destino</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getPeso<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">peso</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setPeso<span style="color: #009900;">&#40;</span><span style="color: #000088;">$peso</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;">peso</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$peso</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getAltura<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">altura</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setAltura<span style="color: #009900;">&#40;</span><span style="color: #000088;">$altura</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;">altura</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$altura</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getLargura<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">largura</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setLargura<span style="color: #009900;">&#40;</span><span style="color: #000088;">$largura</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;">largura</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$largura</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getComprimento<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">comprimento</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setComprimento<span style="color: #009900;">&#40;</span><span style="color: #000088;">$comprimento</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;">comprimento</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$comprimento</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getValor<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">valor</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setValor<span style="color: #009900;">&#40;</span><span style="color: #000088;">$valor</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;">valor</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$valor</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getErro<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">erro</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setErro<span style="color: #009900;">&#40;</span><span style="color: #000088;">$erro</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;">erro</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$erro</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> defineServico<span style="color: #009900;">&#40;</span><span style="color: #000088;">$servico</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$servico</span> <span style="color: #339933;">=</span> <span style="color: #990000;">strtolower</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$servico</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$servico</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'pac'</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;">cod_servico</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'41106'</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;">$servico</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'sedex'</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;">cod_servico</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'40010'</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;">$servico</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'sedex a cobrar'</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;">cod_servico</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'40045'</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;">$servico</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'sedex 10'</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;">cod_servico</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'40215'</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #b1b100;">else</span><span style="color: #009900;">&#123;</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>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> calcular<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$ws_correios</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;http://ws.correios.com.br/calculador/CalcPrecoPrazo.aspx?nCdEmpresa=&amp;sDsSenha=&amp;sCepOrigem=&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">cep_origem</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;&amp;sCepDestino=&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">cep_destino</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;&amp;nVlPeso=&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">peso</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;&amp;nCdFormato=1&amp;nVlComprimento=&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">comprimento</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;&amp;nVlAltura=&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">altura</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;&amp;nVlLargura=&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">largura</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;&amp;sCdMaoPropria=n&amp;nVlValorDeclarado=&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">valor</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;&amp;sCdAvisoRecebimento=n&amp;nCdServico=&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">cod_servico</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;&amp;nVlDiametro=0&amp;StrRetorno=xml&quot;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$xml</span> <span style="color: #339933;">=</span> <span style="color: #990000;">simplexml_load_file</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ws_correios</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$xml</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">cServico</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">Erro</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'0'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            <span style="color: #b1b100;">return</span> <span style="color: #000088;">$xml</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">cServico</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #b1b100;">else</span><span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setErro</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$xml</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">cServico</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">MsgErro</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</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>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

</p>
<p style="text-align: justify;">Tirando os métodos <strong>get </strong>e <strong>set</strong> dos aributos privados da classe, ela se resume a 2 funções básicas: a definição do serviço (se é PAC, SEDEX, SEDEX 10 ou SEDEX A COBRAR) e o método de execução que faz a requisição no WS dos correios e captura os resultados.</p>
<p style="text-align: justify;">O WS exige que passamos uma série de parâmetros referentes ao envio. Alguns desses parâmetros já vem com valores padrão como a <strong>altura, largura, comprimento </strong>e <strong>valor</strong>(valor declarado: É o serviço adicional pelo qual o cliente declara o valor de um objeto postado sob registro, para fins de ressarcimento, em caso de extravio ou espoliação). Devemos ainda passar o <strong>cep de origem, cep de destino, o serviço </strong>e o <strong>peso </strong>da encomenda.</p>
<p style="text-align: justify;">No exemplo abaixo faço uma simulação de envio de uma encomenda de um CEP de SP com destino ao RJ. Veja:</p>
<p style="text-align: justify;">

<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
</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: #b1b100;">require_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Frete.class.php'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$frete</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Frete<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$frete</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setCep_origem</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'07500000'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$frete</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setCep_destino</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'20000000'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">/*
 * TIPO DE SERVIÇO
 * VALORES VÁLIDOS:
 *  pac
 *  sedex
 *  sedex 10
 *  sedex a cobrar
 */</span>
<span style="color: #000088;">$frete</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">defineServico</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'sedex'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/*PESO EM KG (EX: SE FOR 500gr INFORME 0.5)*/</span>
<span style="color: #000088;">$frete</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setPeso</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'1'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$retorno</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$frete</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">calcular</span><span style="color: #009900;">&#40;</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: #000088;">$frete</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getErro</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</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;">&quot;Ocorreu um erro: &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$frete</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getErro</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: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Informacoes do frete com origem &quot;</span><span style="color: #339933;">.</span> <span style="color: #000088;">$frete</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getCep_origem</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span><span style="color: #0000ff;">&quot; e destino &quot;</span><span style="color: #339933;">.</span> <span style="color: #000088;">$frete</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getCep_destino</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span><span style="color: #0000ff;">&quot; &lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Valor do frete: <span style="color: #006699; font-weight: bold;">$retorno-&gt;Valor</span> reais &lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Prazo para entrega: <span style="color: #006699; font-weight: bold;">$retorno-&gt;PrazoEntrega</span> dias &lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Entrega domiciliar? <span style="color: #006699; font-weight: bold;">$retorno-&gt;EntregaDomiciliar</span> &lt;br /&gt;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Entrega sabado? <span style="color: #006699; font-weight: bold;">$retorno-&gt;EntregaSabado</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

</p>
<p style="text-align: justify;">A execução desse código me retorna algo semelhante ao <strong>print </strong>à seguir:</p>
<p style="text-align: justify;"><a href="http://www.rafaelwendel.com/wp-content/uploads/2012/03/print_frete1.jpg"><img class="alignnone size-full wp-image-331" title="print_frete" src="http://www.rafaelwendel.com/wp-content/uploads/2012/03/print_frete1.jpg" alt="" width="413" height="136" /></a></p>
<p style="text-align: justify;">Utlização muito fácil da classe e você pode melhorá-la se desejar. E qualquer dúvida estou à disposição. É só deixar o seu comentário.</p>
<p style="text-align: justify;">Abs.</p>
<p style="text-align: justify;">
<p>Siga-me no twitter: <a href="http://www.twitter.com/rafaelwendel" target="blank">@rafaelwendel</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.rafaelwendel.com/2012/03/classe-php-para-integrar-com-correios-e-recuperar-informacoes-de-frete/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Principais funções php para trabalhar com arrays</title>
		<link>http://www.rafaelwendel.com/2012/02/principais-funcoes-php-para-trabalhar-com-arrays/</link>
		<comments>http://www.rafaelwendel.com/2012/02/principais-funcoes-php-para-trabalhar-com-arrays/#comments</comments>
		<pubDate>Wed, 29 Feb 2012 16:35:51 +0000</pubDate>
		<dc:creator>Rafael Wendel Pinheiro</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[arrays]]></category>

		<guid isPermaLink="false">http://www.rafaelwendel.com/?p=326</guid>
		<description><![CDATA[Sem muito tempo para colocar conteúdo novo aqui no blog, hoje felizmente arrumei uma brecha aqui em meio a tantos projetos e estudos para trazer um post sobre as funções nativas do php para trabalhar com arrays. Creio que será assunto importante para muitos desenvolvedores já que na implementação de um projeto seja ele pequeno, [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">Sem muito tempo para colocar conteúdo novo aqui no blog, hoje felizmente arrumei uma brecha aqui em meio a tantos projetos e estudos para trazer um post sobre as funções nativas do <strong>php</strong> para trabalhar com <strong>arrays</strong>.</p>
<p style="text-align: justify;">Creio que será assunto importante para muitos desenvolvedores já que na implementação de um projeto seja ele pequeno, médio ou grande, a utilização de arrays é sempre indispensável.</p>
<p style="text-align: justify;"><span id="more-326"></span></p>
<p style="text-align: justify;"><strong>1º -</strong> <strong>is_array($array)</strong></p>
<p style="text-align: justify;">A função <strong>is_array </strong>serve para verificar se uma determinada variável é um array. Em caso afirmativo a função retorna <strong>true, </strong>senão retorna <strong>false.</strong></p>
<p style="text-align: justify;">

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$array</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'laranja'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'banana'</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: #990000;">is_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$array</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'É array'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #666666; font-style: italic;">//Escreve: &quot;É array&quot;</span></pre></td></tr></table></div>

</p>
<p style="text-align: justify;">
<p style="text-align: justify;"><strong>2º &#8211; in_array($valor, $array)</strong></p>
<p style="text-align: justify;">Verifica se um determinado valor existe em alguma posição do array. Deve ser passado 2 parâmetros (o valor a ser buscado e o array que deve ser pesquisado). Retorna <strong>true </strong>se existir o valor e <strong>false </strong>caso contrário.</p>
<p style="text-align: justify;">

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$array</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'pêra'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'uva'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'melão'</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: #990000;">in_array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'uva'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$array</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Valor encontrado'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #666666; font-style: italic;">//Escreve: &quot;Valor encontrado&quot;</span></pre></td></tr></table></div>

</p>
<p style="text-align: justify;">
<p style="text-align: justify;"><strong>3º &#8211; array_keys($array)</strong></p>
<p style="text-align: justify;"><strong></strong>Retorna um novo array com as chaves (índices) do array passado como parâmetro.</p>
<p style="text-align: justify;">

<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: #000088;">$array</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'cidade'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Londres&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$array</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'pais'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Inglaterra&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$keys</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array_keys</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$array</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$keys</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/* Retorna
(
    [0] =&gt; cidade
    [1] =&gt; pais
)
*/</span></pre></td></tr></table></div>

</p>
<p style="text-align: justify;">
<p style="text-align: justify;"><strong>4º &#8211; array_values($array)</strong></p>
<p style="text-align: justify;"><strong></strong>Retorna um novo array com os valores do array passado como parâmetro.</p>
<p style="text-align: justify;">

<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: #000088;">$array</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'cidade'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Londres&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$array</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'pais'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Inglaterra&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$values</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array_values</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$array</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$values</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/* Retorna
(
    [0] =&gt; Londres
    [1] =&gt; Inglaterra
)
*/</span></pre></td></tr></table></div>

</p>
<p style="text-align: justify;">
<p style="text-align: justify;"><strong>5º &#8211; array_count_values($array)</strong></p>
<p style="text-align: justify;">Retorna um novo array onde os índices são os valores do array passado como parâmetro e os valores são o número de ocorrências de cada valor.</p>
<p style="text-align: justify;">

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$array</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'laranja'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'banana'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'maçã'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'laranja'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array_count_values</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$array</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">/* Retorna
(
    [laranja] =&gt; 2
    [banana] =&gt; 1
    [maçã] =&gt; 1
)
*/</span></pre></td></tr></table></div>

</p>
<p style="text-align: justify;">
<p style="text-align: justify;"><strong>6º &#8211; array_merge($array1, $array2)</strong></p>
<p style="text-align: justify;">Agrega o conteúdo dos dois arrays passados como parâmetro e gera um novo array com todos os valores.</p>
<p style="text-align: justify;">

<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
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$array1</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'maça'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'melão'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'goiaba'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$array2</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'laranja'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'banana'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'melancia'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array_merge</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$array1</span><span style="color: #339933;">,</span> <span style="color: #000088;">$array2</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">/* Retorna
(
    [0] =&gt; maça
    [1] =&gt; melão
    [2] =&gt; goiaba
    [3] =&gt; laranja
    [4] =&gt; banana
    [5] =&gt; melancia
)
*/</span></pre></td></tr></table></div>

</p>
<p style="text-align: justify;">
<p style="text-align: justify;"><strong>7º &#8211; array_pop($array)</strong></p>
<p style="text-align: justify;"><strong></strong>Exclui a última posição do array passado como parâmetro</p>
<p style="text-align: justify;">

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$array</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'banana'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'melancia'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'laranja'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">array_pop</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$array</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">//Exclui o elemento &quot;laranja&quot;</span></pre></td></tr></table></div>

</p>
<p style="text-align: justify;">
<p style="text-align: justify;"><strong>8º &#8211; array_shift($array)</strong></p>
<p style="text-align: justify;"><strong></strong>Exclui a primeira posição do array passado como parâmetro</p>
<p style="text-align: justify;">

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$array</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'banana'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'melancia'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'laranja'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">array_shift</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$array</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">//Exclui o elemento &quot;banana&quot;</span></pre></td></tr></table></div>

</p>
<p style="text-align: justify;">
<p style="text-align: justify;"><strong>9º &#8211; array_combine($keys, $values)</strong></p>
<p style="text-align: justify;">Mescla os dois arrays passados como parâmetro e gera um novo array onde as chaves serão os valores do primeiro array e os valores os valores do segundo array.</p>
<p style="text-align: justify;">

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$keys</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'nome'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'email'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'site'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$values</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Rafael'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'rafaelwendel@hotmail.com'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'www.rafaelwendel.com'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array_combine</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$keys</span><span style="color: #339933;">,</span> <span style="color: #000088;">$values</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">/* Retorna
(
    [nome] =&gt; Rafael
    [email] =&gt; rafaelwendel@hotmail.com
    [site] =&gt; www.rafaelwendel.com
)
*/</span></pre></td></tr></table></div>

</p>
<p style="text-align: justify;">
<p style="text-align: justify;">Por hoje é só. Espero que lhe seja útil. Vale lembrar que existem inúmeras outras funções para utilizar com os arrays. Se eu fosse mostrar e comentar todas o post ficaria gigante.</p>
<p style="text-align: justify;">Abs!</p>
<p style="text-align: justify;">Siga-me no twitter: <a href="http://www.twitter.com/rafaelwendel" target="blank">@rafaelwendel</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.rafaelwendel.com/2012/02/principais-funcoes-php-para-trabalhar-com-arrays/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Função php para gerar url encurtadas com a API do migre.me</title>
		<link>http://www.rafaelwendel.com/2012/02/funcao-php-para-gerar-url-encurtadas-com-a-api-do-migre-me/</link>
		<comments>http://www.rafaelwendel.com/2012/02/funcao-php-para-gerar-url-encurtadas-com-a-api-do-migre-me/#comments</comments>
		<pubDate>Tue, 14 Feb 2012 14:14:56 +0000</pubDate>
		<dc:creator>Rafael Wendel Pinheiro</dc:creator>
				<category><![CDATA[api]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[xml]]></category>
		<category><![CDATA[url encurtadas]]></category>

		<guid isPermaLink="false">http://www.rafaelwendel.com/?p=324</guid>
		<description><![CDATA[Com a febre de redes sociais, twitter, facebook, Google Plus, etc, as url encurtadas se tornaram uma ferramenta importante e até mesmo vital para os desenvolvedores, blogueiros e usuários finais de plantão. Com as técnicas de SEO sendo cada vez mais utilizadas, a tendência é que os sites utilizem cada vez mais url longas onde [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">Com a febre de redes sociais, twitter, facebook, Google Plus, etc, as <strong>url encurtadas</strong> se tornaram uma ferramenta importante e até mesmo vital para os desenvolvedores, blogueiros e usuários finais de plantão.</p>
<p style="text-align: justify;">Com as técnicas de SEO sendo cada vez mais utilizadas, a tendência é que os sites utilizem cada vez mais url longas onde a mesma traz em sua estrutura informações relevante sobre o seu conteúdo visando maior organização e otimização para mecanismos de busca.</p>
<p style="text-align: justify;"><span id="more-324"></span></p>
<p style="text-align: justify;">Isso gera um desconforto na divulgação dessas URL em redes sociais, principalmente o <strong>Twitter</strong>, porque vai exatamente na <strong>contra-mão. </strong>Como o microblog permite apenas 140 caracteres em cada atualização de status, talvez o espaço se torne insuficiente.</p>
<p style="text-align: justify;">Felizmente existem ferramentas e sites que geram as famosas <strong>url encurtadas, </strong>como o <a title="Migre.me" href="http://migre.me" target="_blank">http://migre.me</a>. Url encurtadas são <em><span style="text-decoration: underline;">strings</span>, </em>geralmente com menos de 20 caracteres, que gerenciam o redirecionamento para uma URL considerada normal. Por exemplo, o link <strong>http://www.rafaelwendel.com</strong> se torna <strong style="text-align: justify;">migre.me/7V9wO</strong> . Assim, mesmo você digitando o segundo link (nada sugestível) você chegará à página inicial do meu blog <img src='http://www.rafaelwendel.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p style="text-align: justify;">Pois bem. Pensando na necessidade em se ter e gerar as URL encurtadas, resolvi entrar no blog oficial do <strong>migre.me</strong> na parte da <strong>API (<a title="API do migre.me" href="http://migreme.com.br/blog/category/api/" target="_blank">http://migreme.com.br/blog/category/api/</a>)</strong> e acabei verificando que sua utilização é ridícula de fácil.</p>
<p style="text-align: justify;">Com o conhecimento adquirido, criei uma função <strong>php </strong>onde passo uma &#8220;URL normal&#8221; e ela, através da API do migre.me, retorna a URL encurtada da mesma.</p>
<p style="text-align: justify;">Aí vai a função:</p>
<p style="text-align: justify;">

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
&nbsp;
<span style="color: #009933; font-style: italic;">/**
    * Acessa a API do migre.me e gera uma URL encurtada
    * @author Rafael Wendel Pinheiro
    * @param  $url URL a ser encurtada
    * @return String URL encurtada ou informações do erro
*/</span>	
<span style="color: #000000; font-weight: bold;">function</span> gera_url_encurtada<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;">$url</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;">$xml</span> <span style="color: #339933;">=</span>  <span style="color: #990000;">simplexml_load_file</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;http://migre.me/api.xml?url=<span style="color: #006699; font-weight: bold;">$url</span>&quot;</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: #000088;">$xml</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">error</span> <span style="color: #339933;">!=</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$xml</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">errormessage</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">else</span><span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$xml</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">migre</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$url_encurtada_1</span> <span style="color: #339933;">=</span> gera_url_encurtada<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'http://www.rafaelwendel.com'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$url_encurtada_1</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// retorna http://migre.me/7VaFd</span>
&nbsp;
<span style="color: #000088;">$url_encurtada_2</span> <span style="color: #339933;">=</span> gera_url_encurtada<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'http://www.rafaelwendel.com/2012/02/otimize-o-banco-de-dados-do-seu-wordpress-com-o-wp-optimize/'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$url_encurtada_2</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// retorna http://migre.me/7VaFe</span></pre></div></div>

</p>
<p style="text-align: justify;">Como você pode ver, para utilizar a função basta passar como parâmetro a URL que deseja encurtar. Em caso de sucesso ela te retornará a URL encurtada. Caso contrário ele te retorna informações sobre o erro ocorrido. A função trabalha com informações em XML e uso o <strong>simplexml_load_file</strong> (que já foi abordado em outros posts) para executar a API.</p>
<p style="text-align: justify;">Antes de terminar vaí uma ressalta: existe um limite de 30 URLs por hora para usuários comuns (free) que é verificado através do IP. Então fique atento a esse limite.</p>
<p style="text-align: justify;">Espero que tenham gostado.</p>
<p style="text-align: justify;">Abs.</p>
<p>Siga-me no twitter: <a href="http://www.twitter.com/rafaelwendel" target="blank">@rafaelwendel</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.rafaelwendel.com/2012/02/funcao-php-para-gerar-url-encurtadas-com-a-api-do-migre-me/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

