mirror of
https://github.com/php/doc-pt_br.git
synced 2026-03-23 22:52:12 +01:00
258 lines
6.6 KiB
XML
258 lines
6.6 KiB
XML
<?xml version="1.0" encoding="utf-8"?>
|
|
<!-- EN-Revision: f0e6ac0428979cc3474c7087513904c58bc22c87 Maintainer: leonardolara Status: ready -->
|
|
<chapter xml:id="stream.examples">
|
|
&reftitle.examples;
|
|
<para>
|
|
<example>
|
|
<title>Usando <function>file_get_contents</function>
|
|
para obter dados de fontes múltiplas</title>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
/* Lê o arquivo local do diretório /home/bar */
|
|
$arquivolocal = file_get_contents("/home/bar/foo.txt");
|
|
|
|
/* Idêntico ao exemplo acima, porém nomeando explicitamente o esquema FILE */
|
|
$arquivolocal = file_get_contents("file:///home/bar/foo.txt");
|
|
|
|
/* Lê o arquivo remoto de www.example.com usando HTTP */
|
|
$arquivohttp = file_get_contents("http://www.example.com/foo.txt");
|
|
|
|
/* Lê o arquivo remoto de www.example.com usando HTTPS */
|
|
$arquivohttps = file_get_contents("https://www.example.com/foo.txt");
|
|
|
|
/* Lê o arquivo remoto de ftp.example.com usando FTP */
|
|
$arquivoftp = file_get_contents("ftp://usuario:senha@ftp.example.com/foo.txt");
|
|
|
|
/* Lê o arquivo remoto de ftp.example.com usando FTPS */
|
|
$arquivoftps = file_get_contents("ftps://usuario:senha@ftp.example.com/foo.txt");
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
</example>
|
|
</para>
|
|
<para>
|
|
<example>
|
|
<title>Realizando uma requisição POST para um servidor https</title>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
/* Envia requisição POST para https://secure.example.com/form_action.php
|
|
* Inclui elementos de formulário com nomes "foo" e "bar" com valores arbitrários
|
|
*/
|
|
|
|
$sock = fsockopen("ssl://secure.example.com", 443, $errno, $errstr, 30);
|
|
if (!$sock) die("$errstr ($errno)\n");
|
|
|
|
$data = "foo=" . urlencode("Valor para Foo") . "&bar=" . urlencode("Valor para Bar");
|
|
|
|
fwrite($sock, "POST /form_action.php HTTP/1.0\r\n");
|
|
fwrite($sock, "Host: secure.example.com\r\n");
|
|
fwrite($sock, "Content-type: application/x-www-form-urlencoded\r\n");
|
|
fwrite($sock, "Content-length: " . strlen($data) . "\r\n");
|
|
fwrite($sock, "Accept: */*\r\n");
|
|
fwrite($sock, "\r\n");
|
|
fwrite($sock, $data);
|
|
|
|
$cabecalhos = "";
|
|
while ($str = trim(fgets($sock, 4096)))
|
|
$cabecalhos .= "$str\n";
|
|
|
|
echo "\n";
|
|
|
|
$corpo = "";
|
|
while (!feof($sock))
|
|
$corpo .= fgets($sock, 4096);
|
|
|
|
fclose($sock);
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
</example>
|
|
</para>
|
|
<para>
|
|
<example>
|
|
<title>Escrevendo dados em um arquivo compactado</title>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
/* Cria um arquivo compactado contendo uma string qualquer
|
|
* O arquivo pode ser lido usando o fluxo compress.zlib ou simplesmente
|
|
* descompactado na linha de comando usando 'gzip -d foo-bar.txt.gz'
|
|
*/
|
|
$fp = fopen("compress.zlib://foo-bar.txt.gz", "wb");
|
|
if (!$fp) die("Não foi possível criar o arquivo.");
|
|
|
|
fwrite($fp, "Isto é um teste.\n");
|
|
|
|
fclose($fp);
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
</example>
|
|
</para>
|
|
|
|
<section xml:id="stream.streamwrapper.example-1">
|
|
<title>Exemplo de classe regsitradao como empacotador de fluxo</title>
|
|
<para>
|
|
O exemplo abaixo implementa o manipulador de protocolo var:// que permite
|
|
acesso de leitura/escrita a uma variável global usando funções padrão de fluxo
|
|
de sistemas de arquivos como <function>fread</function>. O protocolo var://
|
|
implementado abaixo, fornecido pela URL "var://foo" irá ler/escrever dados
|
|
de/para $GLOBALS["foo"].
|
|
</para>
|
|
<para>
|
|
<example>
|
|
<title>Um Fluxo para ler/escrever variáveis globais</title>
|
|
<programlisting role="php">
|
|
<![CDATA[
|
|
<?php
|
|
|
|
class FluxoDeVariavel {
|
|
var $posicao;
|
|
var $nomevar;
|
|
|
|
function abre_fluxo($caminho, $modo, $opcoes, &$caminho_aberto)
|
|
{
|
|
$url = parse_url($caminho);
|
|
$this->nomevar = $url["host"];
|
|
$this->posicao = 0;
|
|
|
|
return true;
|
|
}
|
|
|
|
function le_fluxo($contagem)
|
|
{
|
|
$ret = substr($GLOBALS[$this->nomevar], $this->posicao, $contagem);
|
|
$this->posicao += strlen($ret);
|
|
return $ret;
|
|
}
|
|
|
|
function escreve_fluxo($dados)
|
|
{
|
|
$esquerda = substr($GLOBALS[$this->nomevar], 0, $this->posicao);
|
|
$direita = substr($GLOBALS[$this->nomevar], $this->posicao + strlen($dados));
|
|
$GLOBALS[$this->nomevar] = $esquerda . $dados . $direita;
|
|
$this->posicao += strlen($dados);
|
|
return strlen($dados);
|
|
}
|
|
|
|
function posicao_fluxo()
|
|
{
|
|
return $this->posicao;
|
|
}
|
|
|
|
function final_fluxo()
|
|
{
|
|
return $this->posicao >= strlen($GLOBALS[$this->nomevar]);
|
|
}
|
|
|
|
function pesquisa_fluxo($deslocamento, $onde)
|
|
{
|
|
switch ($onde) {
|
|
case SEEK_SET:
|
|
if ($deslocamento < strlen($GLOBALS[$this->nomevar]) && $deslocamento >= 0) {
|
|
$this->posicao = $deslocamento;
|
|
return true;
|
|
} else {
|
|
return false;
|
|
}
|
|
break;
|
|
|
|
case SEEK_CUR:
|
|
if ($deslocamento >= 0) {
|
|
$this->posicao += $deslocamento;
|
|
return true;
|
|
} else {
|
|
return false;
|
|
}
|
|
break;
|
|
|
|
case SEEK_END:
|
|
if (strlen($GLOBALS[$this->nomevar]) + $deslocamento >= 0) {
|
|
$this->posicao = strlen($GLOBALS[$this->nomevar]) + $deslocamento;
|
|
return true;
|
|
} else {
|
|
return false;
|
|
}
|
|
break;
|
|
|
|
default:
|
|
return false;
|
|
}
|
|
}
|
|
|
|
function metadados_fluxo($caminho, $opcao, $var)
|
|
{
|
|
if($opcao == STREAM_META_TOUCH) {
|
|
$url = parse_url($caminho);
|
|
$nomevar = $url["host"];
|
|
if(!isset($GLOBALS[$nomevar])) {
|
|
$GLOBALS[$nomevar] = '';
|
|
}
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
}
|
|
|
|
stream_wrapper_register("var", "FluxoDeVariavel")
|
|
or die("Falha ao registrar protocolo");
|
|
|
|
$minhavar = "";
|
|
|
|
$fp = fopen("var://minhavar", "r+");
|
|
|
|
fwrite($fp, "linha1\n");
|
|
fwrite($fp, "linha2\n");
|
|
fwrite($fp, "linha3\n");
|
|
|
|
rewind($fp);
|
|
while (!feof($fp)) {
|
|
echo fgets($fp);
|
|
}
|
|
fclose($fp);
|
|
var_dump($minhavar);
|
|
|
|
?>
|
|
]]>
|
|
</programlisting>
|
|
&example.outputs;
|
|
<screen>
|
|
<![CDATA[
|
|
linha1
|
|
linha2
|
|
linha3
|
|
string(18) "linha1
|
|
linha2
|
|
linha3
|
|
"
|
|
]]>
|
|
</screen>
|
|
</example>
|
|
</para>
|
|
</section>
|
|
</chapter>
|
|
|
|
<!-- Keep this comment at the end of the file
|
|
Local variables:
|
|
mode: sgml
|
|
sgml-omittag:t
|
|
sgml-shorttag:t
|
|
sgml-minimize-attributes:nil
|
|
sgml-always-quote-attributes:t
|
|
sgml-indent-step:1
|
|
sgml-indent-data:t
|
|
indent-tabs-mode:nil
|
|
sgml-parent-document:nil
|
|
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
|
|
sgml-exposed-tags:nil
|
|
sgml-local-catalogs:nil
|
|
sgml-local-ecat-files:nil
|
|
End:
|
|
vim600: syn=xml fen fdm=syntax fdl=2 si
|
|
vim: et tw=78 syn=sgml
|
|
vi: ts=1 sw=1
|
|
-->
|
|
|