<?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>Blog do Beraldo &#187; web service</title>
	<atom:link href="http://www.rberaldo.com.br/blog/tag/web-service/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.rberaldo.com.br/blog</link>
	<description>Programação, Música, Entretenimento e o que mais me vier à mente</description>
	<lastBuildDate>Mon, 06 Feb 2012 14:30:10 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
		<item>
		<title>Trabalhando com a biblioteca cURL</title>
		<link>http://www.rberaldo.com.br/blog/trabalhando-com-a-biblioteca-curl/</link>
		<comments>http://www.rberaldo.com.br/blog/trabalhando-com-a-biblioteca-curl/#comments</comments>
		<pubDate>Fri, 02 Oct 2009 16:29:46 +0000</pubDate>
		<dc:creator>Beraldo</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programação]]></category>
		<category><![CDATA[bot]]></category>
		<category><![CDATA[bot de busca]]></category>
		<category><![CDATA[cURL]]></category>
		<category><![CDATA[robot]]></category>
		<category><![CDATA[robot de busca]]></category>
		<category><![CDATA[web service]]></category>

		<guid isPermaLink="false">http://www.rberaldo.com.br/blog/?p=75</guid>
		<description><![CDATA[A biblioteca cURL do PHP nos auxilia a buscar informações em outro sites, possibilitando a obtenção de dados remotos, como criação de WEB Services, bots de busca etc. Neste tutorial, são mostradas algumas técnicas e aplicações da cURL.]]></description>
			<content:encoded><![CDATA[<p>A biblioteca <a href="http://www.php.net/manual/pt_BR/book.curl.php" target="_blank" onclick="urchinTracker('/outgoing/www.php.net/manual/pt_BR/book.curl.php?referer=');">cURL</a> do PHP nos auxilia a buscar informações em outro sites, possibilitando a obtenção de dados remotos, como criação de <em>WEB Services</em>, <em>bots</em> de busca etc.</p>
<p><span id="more-75"></span></p>
<p>Vamos começar com um simples exemplo, acessando o site do Google e exibindo o conteúdo obtido.</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
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// página que desejamos acessar</span>
<span style="color: #000088;">$pagina</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'http://www.google.com.br'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// iniciamos uma instância do cURL</span>
<span style="color: #000088;">$ch</span> <span style="color: #339933;">=</span> <span style="color: #990000;">curl_init</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// definimos a URL a ser acessada</span>
<span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_URL<span style="color: #339933;">,</span> <span style="color: #000088;">$pagina</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// executamos a ação</span>
<span style="color: #990000;">curl_exec</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$ch</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// encerramos a instância do cURL</span>
<span style="color: #990000;">curl_close</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$ch</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Caso queira, em vez de exibir o conteúdo, retorná-lo a uma variável, para um tratamento de string, por exemplo, basta usar a opção RETURNTRANSFER. O código ficará assim:</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
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$pagina</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'http://www.google.com.br'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$ch</span> <span style="color: #339933;">=</span> <span style="color: #990000;">curl_init</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_URL<span style="color: #339933;">,</span> <span style="color: #000088;">$pagina</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// define que o conteúdo obtido deve ser retornado em vez de exibido</span>
<span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_RETURNTRANSFER<span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</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: #990000;">curl_exec</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$ch</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #990000;">curl_close</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$ch</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// trocamos a palavra &quot;Google&quot; por &quot;Beraldo&quot;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #990000;">preg_replace</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;/google/i&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;Beraldo&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$retorno</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Será  exibido o conteúdo, porém, com a palavra &#8220;Beraldo&#8221; substituindo todas as palavras &#8220;Google&#8221;.</p>
<p>Há diversas opções para usar com a função curl_setopt. Veja a lista completa no <a href="http://www.php.net/manual/pt_BR/function.curl-setopt.php" target="_blank" onclick="urchinTracker('/outgoing/www.php.net/manual/pt_BR/function.curl-setopt.php?referer=');">link da documentação dessa função</a>.</p>
<p>Por padrão, o cURL não segue redirecionamentos. Por exemplo, se houver um <strong>header(&#8220;Location: url&#8221;);</strong>, o redirecionamento não será feito. Para contornar isso, usamos a opção CURLOPT_FOLLOWLOCATION. Usarei o site www.google.com para testar. Experimente retirar esta última opção inserida e veja que a página em português não e exibida.</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
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$pagina</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'http://www.google.com'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$ch</span> <span style="color: #339933;">=</span> <span style="color: #990000;">curl_init</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_URL<span style="color: #339933;">,</span> <span style="color: #000088;">$pagina</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_RETURNTRANSFER<span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// define que deve-se seguir redirecionamentos</span>
<span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_FOLLOWLOCATION<span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$retorno</span> <span style="color: #339933;">=</span> <span style="color: #990000;">curl_exec</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$ch</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #990000;">curl_close</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$ch</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #990000;">preg_replace</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;/google/i&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;Beraldo&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$retorno</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Os exemplos acima poderiam perfeitamente funcionar com funções de manipulação de arquivos, como file_get_contents. Porém, essas funções não podem executar requisições com método POST. É aí que entra o cURL! :)</p>
<p>A opção CURLOPT_POST define que a requisição será pelo método POST. Para definir os campos da requisição, usaremos CURLOPT_POSTFIELDS. Vamos a um exemplo simples:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// campos da requisição POST</span>
<span style="color: #000088;">$postfields</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;">=&gt;</span> <span style="color: #0000ff;">'Beraldo'</span><span style="color: #339933;">,</span>
						<span style="color: #0000ff;">'site'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'www.rberaldo.com.br'</span>
					<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// página que receberá a requisição post</span>
<span style="color: #000088;">$pagina</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'http://localhost/tmp/curl/post.php'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$ch</span> <span style="color: #339933;">=</span> <span style="color: #990000;">curl_init</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_URL<span style="color: #339933;">,</span> <span style="color: #000088;">$pagina</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_POST<span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_POSTFIELDS<span style="color: #339933;">,</span> <span style="color: #000088;">$postfields</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #990000;">curl_exec</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$ch</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #990000;">curl_close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Código do arquivo post.php:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$_POST</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Isso mostrará os campos que definimos no nosso script de cURL.</p>
<p>Podemos usar a opção CURLOPT_HEADER para incluir na saída o cabeçalho da página. Por exemplo:</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
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// campos da requisição POST</span>
<span style="color: #000088;">$postfields</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;">=&gt;</span> <span style="color: #0000ff;">'Beraldo'</span><span style="color: #339933;">,</span>
						<span style="color: #0000ff;">'site'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'www.rberaldo.com.br'</span>
					<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// página que receberá a requisição post</span>
<span style="color: #000088;">$pagina</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'http://localhost/tmp/curl/post.php'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$ch</span> <span style="color: #339933;">=</span> <span style="color: #990000;">curl_init</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_URL<span style="color: #339933;">,</span> <span style="color: #000088;">$pagina</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_POST<span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_POSTFIELDS<span style="color: #339933;">,</span> <span style="color: #000088;">$postfields</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// define que o cabeçalho deve ser incluso na saída da requisição</span>
<span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_HEADER<span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #990000;">curl_exec</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$ch</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #990000;">curl_close</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>É possível, inclusive, enviar arquivos por cURL, via POST. Basta incluir mais um campo e inserir o arroba (@) antes do caminho completo do arquivo. Por exemplo:</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
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// campos da requisição POST</span>
<span style="color: #000088;">$postfields</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;">=&gt;</span> <span style="color: #0000ff;">'Beraldo'</span><span style="color: #339933;">,</span>
						<span style="color: #0000ff;">'site'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'www.rberaldo.com.br'</span><span style="color: #339933;">,</span>
						<span style="color: #0000ff;">'arquivo'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'@/tmp/imagem.jpg'</span>
					<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// página que receberá a requisição post</span>
<span style="color: #000088;">$pagina</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'http://localhost/tmp/curl/post.php'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$ch</span> <span style="color: #339933;">=</span> <span style="color: #990000;">curl_init</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_URL<span style="color: #339933;">,</span> <span style="color: #000088;">$pagina</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_POST<span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_POSTFIELDS<span style="color: #339933;">,</span> <span style="color: #000088;">$postfields</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_HEADER<span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #990000;">curl_exec</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$ch</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #990000;">curl_close</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$ch</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Se você inserir um <strong>print_r( $_FILES );</strong> no post.php, verá os dados do arquivo enviado. ;)</p>
<p>Unindo as duas últimas opções (POST e HEADER), podemos falar sobre login usando cURL. Sim, é possível, inclusive, fazer login em sites usnado cURL. Afinal, essa biblioteca pode perfeitamente simular um navegador ou um bot de busca.</p>
<p>Primeiro vamos incluir a criação de um cookie no nosso arquivo post.php, deixando-o da seguinte forma:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">setcookie</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;test&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;valor do cookie de teste&quot;</span><span style="color: #339933;">,</span> <span style="color: #990000;">time</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #cc66cc;">3600</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;">$_POST</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>E o nosso script de cURL passará a receber o valor do cookie que deve ser salvo no navegador.</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
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// campos da requisição POST</span>
<span style="color: #000088;">$postfields</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;">=&gt;</span> <span style="color: #0000ff;">'Beraldo'</span><span style="color: #339933;">,</span>
						<span style="color: #0000ff;">'site'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'www.rberaldo.com.br'</span>
					<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// página que receberá a requisição post</span>
<span style="color: #000088;">$pagina</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'http://localhost/tmp/curl/post.php'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$ch</span> <span style="color: #339933;">=</span> <span style="color: #990000;">curl_init</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_URL<span style="color: #339933;">,</span> <span style="color: #000088;">$pagina</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_POST<span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_POSTFIELDS<span style="color: #339933;">,</span> <span style="color: #000088;">$postfields</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_RETURNTRANSFER<span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_HEADER<span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$retorno</span> <span style="color: #339933;">=</span> <span style="color: #990000;">curl_exec</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$ch</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #990000;">curl_close</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$ch</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;/Set-Cookie:([^<span style="color: #000099; font-weight: bold;">\n</span>]+)/i&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$retorno</span><span style="color: #339933;">,</span> <span style="color: #000088;">$matches</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$cookie</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$matches</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: #b1b100;">echo</span> <span style="color: #000088;">$cookie</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>A variável $cookie conterá o valor do cookie que deve ser salvo no navegador. Para salvar esse cookie, usamos a opção CURLOPT_COOKIE. Tendo esse valor salvo numa variável, basta fazer isto no script que se conecta a uma página que exija esse cookie:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_COOKIE<span style="color: #339933;">,</span> <span style="color: #000088;">$cookie</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Na documentação das funções cURL (especialmente na da função curl_setopt) é possível encontrar muitas outras funcionalidades para a cURL. É possível definir cabeçalhos de navegador, idioma, usar SSL, proxy e muito mais.</p>
<p>Lista de funções da cURL:<br />
<a href="http://www.php.net/manual/pt_BR/ref.curl.php" target="_blank" onclick="urchinTracker('/outgoing/www.php.net/manual/pt_BR/ref.curl.php?referer=');">http://www.php.net/manual/pt_BR/ref.curl.php</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.rberaldo.com.br/blog/trabalhando-com-a-biblioteca-curl/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

