apostila php-avancado1444

26

Upload: elifranio-alves-cruz

Post on 04-Jul-2015

519 views

Category:

Documents


0 download

DESCRIPTION

Apostila de PHP

TRANSCRIPT

Page 1: Apostila php-avancado1444
Page 2: Apostila php-avancado1444

����������������������������������������������������������������������������������������������������������������������������������������������������������������

���������������

�� ����� �� �������������� ���� ����� ���������� ������ �� ������������ ����� ������������� ��� ��� ��� �!

"# ������ ���������������������� ������ �� ������������ ����� ����������$������� ���������� ����� ���� �%��� �&���&����� ���'���������� �(���%�����&���� ��� ���� ������������)�� �������%���&����� ��������������'��*�������������)���+��������������#

�# , ���&�����������������'�� ���� �������������� � ���� �� ���� ��%������- ������+ �����%������ �� ���� ������.��������� ��� ����/���� ������ �� ������������ �������/�0�� �/��00#

1# 2������ ����.����� �����3������������ �� ������������&���&������'�� ����� ������ ����� �� ����� ������� ��������� ���� ��� ������ ������� ������������� ���� ����#�4 ����)�%���������� � �����$����� � �����3��������� ��������� ������ ���.���5������� �������#�4 ���� ������ �� ������������ �������� � ���� �� ������� �'����/ ������ �� �6 �����#������ �#��0��� ���� �����3������� ��������'����� �� ���������������� ��3���������� �����#�

�������� �� �%����� ������������������� ��� ������ ���.��!

7���������� �'���������������� �'������� � ��

7�4 �������-����������� �'����.�������������� �'���������� �'����.���� ���

7�8���3���� �� ���'� ���� ��������� ��

7��������$�������� �� ��� ��������������� ��

7�9���.���� �'����������� �� ��� ����������� ������ ��������� ����&���� ��$��������� �����)��'��������������� #

��������������������� ���������� ��� � ��������'���������� ����3�� �� ���� �����&���&��� ��%��+����������� ����� �%�&��� ���������&�������&���&����'�������#�4 ���� ������ �� ������������ ���������� ��&���&��������������������������������'�� ��.���� ������� ��� � �����#

:��� ������2����'��*�����2����; ����<���� ������ �� ����<������� ���������������������������������������������������� ����

Page 3: Apostila php-avancado1444

����������������������������������������������������������������������������������������������������������������������������������������������������������������

������

���� ���"�<��� ��� ����4 ���� �����4 �)� �############################################### =

�����������)� �################################################################################### �

��������> > > ####################################################################################### ?

@ � ���� �� ������ �� �################################################################ A

���������� �� #################################################################################### "�

B� ��'����############################################################################################### "1

����������� �� ��################################################################################### "=

�������C�3����D'����D############################################################################### "?

@ � ���������������� - ����########################################################## "E

@ � ��� - ���#################################################################################### �"

���� �����<�@ ��������������&����� �+ ##################################################### 1"

���� ���1�<�F�� ����G@ H################################################################################### 1=

���� ���I�<�2�� ��� ����� �4 4 �###################################################################### I?

Última atualização em 03/07/2006

:��� ������2����'��*�����2����; ����<���� ������ �� ����<������� ���������������������������������������������������� �1��

Page 4: Apostila php-avancado1444

����������������������������������������������������������������������������������������������������������������������������������������������������������������

���� ������

� ������������ ����������������� ���������� ����

�� ������ ������������ ��� �������'-�������)� ������� ��������������&���� �+ �� ����������#

:��� ������2����'��*�����2����; ����<���� ������ �� ����<������� ���������������������������������������������������� �I��

Page 5: Apostila php-avancado1444

����������������������������������������������������������������������������������������������������������������������������������������������������������������

��������������������������

�����'������� ������ ���� �'��������&����&�������������� �$�������� ������-��������������� ������� �'���.�������� ���%� ���������'��3�� �� #8�������� ����J� � ������ �����&�������� ������� ���#

$arquivo = "teste.txt";$abertura = fopen($arquivo, "w");

4 ��� �������� ������!

� �!�F� �� ����� ���#

� �� !�H�� ������ ������#�F�����&����)-��+�� ��%���-� ����������������&���#

� ��!�F� �� �� ������#�F�����&����)-��+�� ��%���-���� ��� ���� �K�����#

� �� �!�L ���������H�� ���#�F�����&����)-��+�� ��%���-���� ��� ���� �K�����#

� ��!�F� �� �� ������#��������&�����+�� ����-� ��������'��������&���#

� �� �!�L ������������ ���#��������&�����+�� ����-� ��������'��������&���#

� ��������������������������

�����'�������������� ��� �� �� ������ �'��������&����/� ���������������� ���0����� ��� ��� �/�� �K����&���0#�8� ������K� ��������� ����� �����#

$conteudo = "Isto é um teste";$gravacao = fwrite($abertura, $conteudo);

��������������������������

�����'�������������� ��� �� �� ������ �'��������&����/� ���������������� ���0���� �� ��3����&������ ��M ����� ��� ���� �� �� #�8� ������� �K����&���%�&�������������� �$������ ��� �����-���#

:��� ������2����'��*�����2����; ����<���� ������ �� ����<������� ���������������������������������������������������� �=��

Page 6: Apostila php-avancado1444

����������������������������������������������������������������������������������������������������������������������������������������������������������������

������ ���� �� ��3����&���%���C���� ���$�����'�����'�����$�/0����� ������� ��&����� ������'������� ������%��� ����$�&�����'�����'N �� �/0%�� �)-���� �%��� ������K� ���������� ����� �����#�

$abertura = fopen($arquivo, "r");$leitura = fread($abertura,filesize($arquivo));

�� ����������������������������!� ���������������� ��"

�����'�������������� ��� �� �� ������ �'��������&����/� ���������������� ���0��������������&����&���������������/���+�� ������+%������������� ��'������������������ ���������������&���0#

fseek($abertura, 0);

#��$���������������������� �

�����'�����$����������� ���&���%����C���'��3-C�%������ � �������� ����.�������������#�O����������-���� ���$������3�� ��� �� �� ����� ����;������ �'��������&���#�

fclose ($abertura);

:��� ������2����'��*�����2����; ����<���� ������ �� ����<������� ���������������������������������������������������� ����

Page 7: Apostila php-avancado1444

����������������������������������������������������������������������������������������������������������������������������������������������������������������

�!����� %������ � ���������&� ���'����������

(�����

<?php

# teste_arquivo.php

$arquivo = "teste.txt";

$conteudo="Isto é um teste";

$abertura=fopen("$arquivo","w+");

$gravacao = fwrite($abertura, $conteudo);

echo "Número de caracteres gravados: $gravacao";

# Reposiciona o ponteiro no início do arquivo

fseek($abertura, 0);

$leitura = fread($abertura, filesize($arquivo));

fclose ($abertura);

echo "<br> Conteúdo do arquivo: $leitura";

?>

(����)��

9�� ���� �����3�����+�� ������� �%��� ����������� ������ ��� ������� ������)� �#�������� ������ �&���'������ ������������ ������ ��� ���&��� �+ %�� ���$������������P��&���Q������ ������ ����/����� ������- ����="0#

:��� ������2����'��*�����2����; ����<���� ������ �� ����<������� ���������������������������������������������������� �?��

Page 8: Apostila php-avancado1444

����������������������������������������������������������������������������������������������������������������������������������������������������������������

<?php# arquivo.class.php

class arquivo{

# Atributosprotected $abertura;protected $gravacao;protected $leitura;

# Métodos# Abertura do arquivo, recebe como parametros o nome do arquivo# e o tipo de abertura function abreArq ($nome_arq, $tipo_abertura) {

$this->abertura = fopen("$nome_arq", "$tipo_abertura");}

# Gravação do arquivo, recebe como parâmetros o identificador do arquivo # e o conteúdo a ser gravado

function gravaArq ($conteudo) {

$this->gravacao = fwrite($this->abertura, $conteudo);}

# Leitura do arquivo, a função fread recebe como parâmetros# o identificador do arquivo e o tamanho do mesmofunction leArq () { $this->leitura = fread($this->abertura, $this->gravacao);}

# Fechamento do arquivo, a função fclose utiliza como parâmetro# o identificador do arquivofunction fechaArq () { fclose ($this->abertura);}

# Exibe o conteúdo do arquivo, utiliza o resultado obtido # pelo método de leiturafunction exibeArq () { echo "<br> Conteúdo do arquivo: ". $this->leitura;}

}

?>

:��� ������2����'��*�����2����; ����<���� ������ �� ����<������� ���������������������������������������������������� �A��

Page 9: Apostila php-avancado1444

����������������������������������������������������������������������������������������������������������������������������������������������������������������

Outras funções úteis para a manipulação de arquivos texto:

��������

2�� ������'�� �����&����/�'0��� ��� ��� ��������&���#

������!��

H*��� �����3������ ��� ��������&�������� ������ ��� ��� #������ ���� ��� ���&�������� ���'�� ������3����'�� �����&���#

���� ���� �� ���� ��+�� ���&���� ���$������������'���.��!

<?php

$abertura = fopen("teste.txt","r");

// Testa pelo fim de arquivo (eof)

while (!feof($abertura)) {

// fgets lê uma linha de um ponteiro de arq

$buffer = fgets($abertura);

echo $buffer . "<Br>";

}

fclose($abertura);

?>

:��� ������2����'��*�����2����; ����<���� ������ �� ����<������� ���������������������������������������������������� �E��

Page 10: Apostila php-avancado1444

����������������������������������������������������������������������������������������������������������������������������������������������������������������

���� ����1�

(���� &� �*+ *,����(���� &� �*+ *,������������������������������������� - � �������� - � �

���������� �����������&�����G@ H��� ������� ����������� ���� �������%�F�� ���G@ H#

:��� ������2����'��*�����2����; ����<���� ������ �� ����<������� ���������������������������������������������������� �"�� �

Page 11: Apostila php-avancado1444

����������������������������������������������������������������������������������������������������������������������������������������������������������������

���� ���'���.���������'������������� ��������������&�����G@ H%�����= �����3���� �1��+ ���.���G@ H%�����&������ ����������� ���� �����������'����!�

: 4 @ �/:��� �� �4 �)�� �@ ��0!������+ �����&��� �� �� �����������%����� ��+� �� �� � �����3%�� ��� �������G@ H�� ������� ����#�2�� ��� �� ���$��� ����� �� ;���#

F�G�/F�� ������B�'��G@ H0!��+ ������� �����G@ H�����#�� ���$��� ����� �� ;���&�����:4 @ %����� %�'��&��� �� �� ��������� �����;� ������� ���+#

F�� ���G@ H!�'����� ����� ��������������&�����G@ H%������ ��C���� ��)� �%��)-���� �3����� �������'��� ������=#��� ����%� �����3�C����� ���� �� ��G@ H�� ����'���� ��)� ���� ��������� �� �� ����%���� ������ ���$���������: 4 @ %���� �� ������ ���+#�O����� ������ �� �� ���$���� ��F�� ���G@ H%��������� ������+ ���.���� ����'-���������� �����3��#

. ���/- � �

G@ H�/�G ��������@ ��R���H�� �� �0����� ����� �� �� ���� �������&������� � ��� ��������'�� ��.�����'�� ���� �� ������ �-������B� ���� #�S��� �� ���� � ��/����� ��� ���2@ H0%��;�&��%���&��� ���2@ H� �����3���� � � �����C�'�����%��G@ H����� � ��&�����������������������������;������ � ������������ ���������� �� �� ���� ������#��� ���&����+� ������� ������&����� ���&��� �+ ��� �� �����.��%���������� �����.������� � �� ��� �� ������ �������������'�� ��.����� ��������&���#�

: �����'�� �%����� � ���� ����������� ����� ��� �������� ���� ��'���� ����� �� ���#

-� ������ 00

����� ����� � �� ���� ��� ����� �����%���� ���� �� ������ �����������&����G@ H���'�� � �8FF����� �� ���$������+ �����F�� ���G@ H����������������&���#8FF����� �'�� � ����� ������������'�� ��.��������B� ���� �&������� � ������-��� ������������ ���������-�����'� ������ �� ����� �� �%���� � ���&������ �������������� ������ ��#���������&������������� ���� �G@ H��� �� ��������3������� �P'���Q#

���� ������� ��� ������ ������8FF%�����-������� ������������'�����)������������������������ �������� ������� �� ����� � �� ������%����

:��� ������2����'��*�����2����; ����<���� ������ �� ����<������� ���������������������������������������������������� �""� �

Page 12: Apostila php-avancado1444

����������������������������������������������������������������������������������������������������������������������������������������������������������������

'�� � ���� ��3�� ������ ��������������C� ���#�

4 ����&�����8FF� ��$�� ��� ���)�� ��� � ��������'����%�����������������!����%������������ �#��� � ��������� ����� �� �G@ H%��� �� � �������� ���#�: �� ���� � ���� �� ����������� �� � ��������#

O�� � ��������� �� ���-������ ���� � ��&��� ��$�� ���'�� ��.����������� �� ����!�, ��� �� ���%��&����� �� %�� ���� �����%�� �#�: �� ���� � �������� ���� �� �� ��� ��-����� � ���� #����� � ��� ��������� ���� ��� ����%���� ��-��� �� ��� � �������������� ����%� ����� �� ��%���������������#

���� ���%� �� ������ �� �����-��������� ���&����8FF�������� � ������������!

������������

��� ������������������������������������������������

���� ���� �

�����������������������������������������������

���� ���������

������

(����)���

9�� ���������������;����'��%��� ���&����G@ H�/�����8FF0%�� ���$��������@ MF, H#��B������� �� �%���������� �������� �� ��������� �����%�� ����������'�� ��-��#��� ��� ���%��� ��� ��� ��������������������� ������� ���&����G@ H��'�� � �8FF#������'�����$��%�� ���� ��� �����������'������+��������� �����#�O������+������%�� ���$���� ��������� ������ ��� ����4 ���� �����4 �)� �%��� ���� ����� �� �#

*� 1�2 ���������������3������45�� ������ �3����6���47

��� ����� �����!

��������������������������� �����������������������������������������

:��� ������2����'��*�����2����; ����<���� ������ �� ����<������� ���������������������������������������������������� �"�� �

Page 13: Apostila php-avancado1444

����������������������������������������������������������������������������������������������������������������������������������������������������������������

����������������������������

*� 8�2 ��������� ��� � �����������+�������������� �������������%�&���'��������� �$������������ ��������!���������!�� ���$������������������������#������� �!�� ���$������� ������������������&����8FF#

���� ���%� �� �����'�������������������!

��9��� 9�$��

:��� ������2����'��*�����2����; ����<���� ������ �� ����<������� ���������������������������������������������������� �"1� �

Page 14: Apostila php-avancado1444

����������������������������������������������������������������������������������������������������������������������������������������������������������������

<?php# db.class.php

class db{

public $conexao;

function __construct($dominio, $usuario, $senha, $db){

$this->conexao = mysql_connect($dominio, $usuario, $senha);mysql_select_db($db, $this->conexao);

}

function DBError(){

echo mysql_error($this->conexao);}

function insert($tabela, $campos) {

$declar = "INSERT into $tabela values $campos";return mysql_query ($declar);

}

function select($tabela, $campos, $condicao) {

$declar = "SELECT $campos from $tabela $condicao";return mysql_query ($declar);

}

function delete($tabela, $condicao) {

$declar = "DELETE from $tabela $condicao";return mysql_query ($declar);

}

function update($tabela, $campos, $condicao) {

$declar = "UPDATE $tabela SET $campos WHERE $condicao";return mysql_query ($declar);

}}

?>

�������������������� �� � ���� �� ��&���'��-������+���� ���������������� �&���������������'����� ������#�2�� ��� ����������$��� � ��&��'�$�� ����������%�������%��+���������� �������� � ������#

����9��� 9�$��

:��� ������2����'��*�����2����; ����<���� ������ �� ����<������� ���������������������������������������������������� �"I� �

Page 15: Apostila php-avancado1444

����������������������������������������������������������������������������������������������������������������������������������������������������������������

<?php

class feed{

public $obj_feed;

function __construct($nome_feed){

$this->obj_feed = simplexml_load_file($nome_feed);}

function exibeFeed(){

foreach ( $this->obj_feed->channel->item as $noticia ){

echo '<br>';echo $noticia->title . '<br>';echo "<a href=\"$noticia->link\">$noticia->link</a>".'<br>';echo $noticia->description . '<br>';

}}

}

?>

O�� � ���� �� �����������'���� ���$�� ����'�����simplexml_load_file%�&����*��� �K����� ���&����G@ H����� ������ ��)� #O�� � ����!��� ���$�� ����� ���foreach%�&���'�������� ��� ������'-������ �����3����� �� � ��$�����'��������� �� ���� �����M�#�4 �foreach�������� ������ � ��$����� �����D��D%������������ �� ������ ����� ���������� �����-����/$noticia0������ ������ ������� � ��$������������ ��� �������#

*� :�#����;������������������� 5���6������9$���7

:��� ������2����'��*�����2����; ����<���� ������ �� ����<������� ���������������������������������������������������� �"=� �

Page 16: Apostila php-avancado1444

����������������������������������������������������������������������������������������������������������������������������������������������������������������

<html><head><title>Formulário</title><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"></head><body><p><font size="2" face="Verdana, Arial, Helvetica, sans-serif"><strong>InserirNotícias </strong></font></p><form name="form1" method="post" action="inclui_noticia.php"> <table width="50%" border="0" cellspacing="10" cellpadding="0"> <tr> <td width="20%"><strong><font size="2" face="Verdana, Arial, Helvetica, sans-serif">Título:</font></strong></td> <td width="80%"><input name="titulo" type="text" id="titulo" size="50"maxlength="100"></td> </tr> <tr> <td width="20%"><strong><font size="2" face="Verdana, Arial, Helvetica, sans-serif">Link:</font></strong></td> <td width="80%"><input name="link" type="text" id="link" size="50"maxlength="100"></td> </tr> <tr> <td width="20%"><strong><font size="2" face="Verdana, Arial, Helvetica, sans-serif">Descrição:</font></strong></td> <td width="80%"><textarea name="descricao" cols="50" rows="4"id="descricao"></textarea></td> </tr> </table> <p> <input type="submit" name="Submit" value="Enviar"> </p> </form></body></html>

:��� ������2����'��*�����2����; ����<���� ������ �� ����<������� ���������������������������������������������������� �"�� �

Page 17: Apostila php-avancado1444

����������������������������������������������������������������������������������������������������������������������������������������������������������������

*� <�. �����������6������9�$����;������ �� ��� ������� ��������;����������������=�����>������� �2 ?9

<?php# inclui_noticia.phpinclude_once ('classes/db.class.php');

// Recebe dados do formulário$titulo = $_POST["titulo"];$link = $_POST["link"];$descricao = $_POST["descricao"];

// obtem data de publicação$data_publ = date("Y").'-'.date("m").'-'.date("d");

// Instancia um objeto da classe db$inc_not = new db('localhost', 'root', 'unicamp', 'topico_xml');$inc_not->DBError();$campos = "(' ', '$titulo', '$link', '$descricao', '$data_publ')";$result = $inc_not->insert('noticia', $campos);

If ($result) {echo "Inclusao OK <br><br> <a href=\"form_noticia.html\">Voltar</a>";

}else {

echo "Erro na Inclusao";}

?>

*� ,�0��������!���������- � �5#���7�������� ��� ��������������5!���6����9�$�79@ � � ����������>���� � ��� � 32 ?4�3������49

:��� ������2����'��*�����2����; ����<���� ������ �� ����<������� ���������������������������������������������������� �"?� �

Page 18: Apostila php-avancado1444

����������������������������������������������������������������������������������������������������������������������������������������������������������������

<?php

# gera_feed.php

function __autoload($classe){

include_once ("classes/$classe.class.php");}

// Obtem data atual$data_atual = date("Y").'-'.date("m").'-'.date("d");

// Instancia um objeto da classe db$sel_not = new db('localhost', 'root', 'unicamp', 'topico_xml');$sel_not->DBError();$condicao = "where data_publ = '$data_atual'";$result = $sel_not->select('noticia', '*', $condicao);

// Verifica se encontrou algum registro$row = mysql_num_rows($result);if ($row > 0) {

// Determina o nome do arquivo XML que será criado$arquivo = "feed.xml";

// Instancia um objeto da classe arquivo e abre o arquivo$arquivo_xml = new arquivo();$arquivo_xml->abreArq($arquivo,'w');

$conteudo = "<?xml version='1.0' encoding='ISO-8859-1'?>";$conteudo .= "<rss version='2.0'>";$conteudo .= '<channel>';$conteudo .= '<title>Seu Site</title>';$conteudo .= '<link>http://www.seusite.com.br</link>';$conteudo .= '<description>Descrição de seu site</description>';$conteudo .= '<language>pt-br</language>';$conteudo .= '<copyright>Copyright de seu site</copyright>';$conteudo .= '<webmaster>[email protected]</webmaster>';

while ($result2 = mysql_fetch_assoc($result)) {

// Monta as tags referentes as noticias$conteudo .= "<item>";$conteudo .= "<title>$result2[titulo]</title>";$conteudo .= "<link>$result2[link]</link>";$conteudo .= "<description>$result2[descricao]</description>";$conteudo .= "</item>";

}

//Fecha as tags channel e rss$conteudo .= '</channel>';"��������#$�%������%&

"��'������ ��(����)�'�"��������&"��'������ �� ���)�'��&

���*���( ����+,���'��������+#"��'����#+����� ���(������� ��������-+&

.

/�

:��� ������2����'��*�����2����; ����<���� ������ �� ����<������� ���������������������������������������������������� �"A� �

Page 19: Apostila php-avancado1444

����������������������������������������������������������������������������������������������������������������������������������������������������������������

. �����!���6����9�$���;!����������3����9���4������� 009

. ����'��� ������������;����������A��!� �!����

<?xml version="1.0" encoding="ISO-8859-1"?><rss version="2.0"><channel><title>Seu Site</title><link>http://www.seusite.com.br</link><description>Descrição de seu site</description><language>pt-br</language><copyright>Copyright de seu site</copyright><webmaster>[email protected]</webmaster><item><title>Noticia 1</title><link>URL da Notícia 1</link><description>Descrição da Notícia 1 </description></item><item><title>Noticia 2</title><link> URL da Notícia 2</link><description> Descrição da Notícia 2 </description></item>

<item><title>Noticia n</title><link> URL da Notícia n </link><description> Descrição da Notícia n </description></item></channel></rss>

*� BC�����)���D����� �����5�����6������9�$�7����������������������� ������'�9�����>����� �����98���� ������- ����=�#

. � !���;�� �� ����� �������������� ������6������9�$� %���������� ���J� � �����&����������� ���'������ ���� �� �%��������$���������� ���J� � ���'�����T�3��4 ������B��� ���#������'��!�$����EE���� 9��$�9��9��9��E��$�E��� �����E� FG19���

4 � ��������������+ �����F�� ���G@ H4 �F�� ���G@ H� �� ��� ����������$��� �������������� �� ��K ���%��� ���'������� ����� �����������%�� ���$�������������� ��� ��� �G@ H%�����'���������%�&������ � ������������ ����'�� ����������'������ ������ ��)� #

���� ���%��� ��+�� ���&���� ���$������������'���.��#����� ����� �� �% ����'�� �� ����� �K��������&����������� ���� ��� ��� ��� %��� ���+������'������ �����#��� ��� ������� �������� ��� ��� ���'������ ����� ��������������� �� ��� � ��������� ������ ��������������!

:��� ������2����'��*�����2����; ����<���� ������ �� ����<������� ���������������������������������������������������� �"E� �

Page 20: Apostila php-avancado1444

����������������������������������������������������������������������������������������������������������������������������������������������������������������

<?php

$string = file("feed.xml");$string = implode(" ", $string);$xml = simplexml_load_string($string);

/* Procurando pelo título da primeira notícia */$result = $xml->xpath('/rss/channel/item/title');

echo $result[0];

?>

O��;� ����� �������������� ������� �����M������������-����U����� %��� �� �������� �� �����/ � ���0����&���#������+��� ��������-���%��� �� �����3����������������M�&���&����� ���+����#

4 ��!�2�� ��� ���� �������������&����8FF��� ��� ���� �� ����� �����8FF����������%����;����� �������������'������)��!

T�������! �3 �!VVN N N #'�������#�� V� /W ��N �0 � H�� � � � � � ����� � 23�������!�3 �!VVN N N #� $����#�� V 3�������V�/W ��N �VH���+0#

:��� ������2����'��*�����2����; ����<���� ������ �� ����<������� ���������������������������������������������������� ���� �

Page 21: Apostila php-avancado1444

����������������������������������������������������������������������������������������������������������������������������������������������������������������

���� �� 4

H������� ��. . *H������� ��. . *

�� ��'�� ����� ���$���������������.���W ��� �� �����)� ��� ����������;� ���� ������+� �����'-������������ ��� �������%����+�������� ��'�� �%� � � ��� ���� � �� �� � �������.��# � F��- � ����� � � � '�� � � � ��� � �-���� � ��� ���� �� ������ ������� %�&���������-��������������� ���������� ����� ���+�#

:��� ������2����'��*�����2����; ����<���� ������ �� ����<������� ���������������������������������������������������� ��"� �

Page 22: Apostila php-avancado1444

����������������������������������������������������������������������������������������������������������������������������������������������������������������

19I�������

�� �� �� ������% �� � ����� � �� ��;� �� ��� � ��������� ����� �� �W �� ����� ���$��%��� �����������'������� �������%��� �� �� ���;� ���2@ H����� ���.�������'�� �����&���&����)�� #�4 ��� ��� ��������-��� �����3�&����� �� � � � ��������� ����� � �� ��� � � � � ��� � ��������% � ����� ��� �3�� ���� ���� ���.���F, H���������������� �����������&������-� �������� �%�� ���� ������������� ��#�� ���$��������� ����� ��� ����4 ���� ���54 �)� � � � �� � �� � �����)�� �� � � � � ���$��� � � ��� � �� ��� �% � � � �������� ���� �$������ ���� �� ���� �� �����3������������ ��� ��������;� #�O�� ����� ���� ���$���� ���� ��+�� �����- ���&�� ����- �� ���$���� �������)� T����#��� ���+�� �������� ���� �� � �������� ��� ���������� ���� ���� �����-����%�� ������������;� �����������/�2@ H%��FF���X�������� 0%���;� ������������������#�

"#"<�4 � �� ��� ��� ���$�� �

O������� �3 �!VV����3� V�3�V �� ��� �� %����� ��C������- ����&������-� ���$����� ���� ���)� �T����% �&�� � ��- ��� � �� � ��� ��� � �� �� �����# ��� ���$������������ �� �������� ��C������T� ����"#"#�: ����� ��� �� ��� ����A���� ��%����&�����=� *�� ������� �K���� �$������ ����������%�&�����-��@ MF, H#�

:��� ������2����'��*�����2����; ����<���� ������ �� ����<������� ���������������������������������������������������� ���� �

Page 23: Apostila php-avancado1444

����������������������������������������������������������������������������������������������������������������������������������������������������������������

#�!���191C2 ��� �����������*�%��#����9

"# D������$!�: �'����.������- �������� � ���#�# � ��� �!�O����������&��������� ��C��C����� ���������� ���#1# @ �)��� �� (���� ! � O� � ����� � ���� � � ���� ��� C�� � ��� � �����

��'���� ���5����� ���� �� �� ��#I# @ �)��� *��������� ! � O�� � ����� ����� ��C�� �� �� ���� ����������% ��

� ���3� ����� �#=# @ �)����2 �� !� �O�� ����������� ��� C������� ����������&��

������� ���3� ��#�# @ �)�����J �����+ ���!��O�� ����������� ��� C������� �������

K� �� ��3��#�?# ���/!���'�� ��.����������������-����������� ��������� �����

�������������- ���#

:��� ������2����'��*�����2����; ����<���� ������ �� ����<������� ���������������������������������������������������� ��1� �

Page 24: Apostila php-avancado1444

����������������������������������������������������������������������������������������������������������������������������������������������������������������

19198CDA��!

, �� � � ����� � � �� � ������-��� � ���� � ������ � � �- ��� � ���������� ��'������� �� # ���� ��� ���������� ���3� ��� ��������'��� � ��� ���� ������ ���- ���#���T� ����"#������ ����'������� �� ��� ������� �!

� @ �����9�$� �� �� ������� ��� �� ����������O ����%�&������-�� ���$������ ������������� �������� ���������������'�� ��.������- �������������#

� ��������9��� 9�$� �� �� �� ������������������-����������� ���������� �� ��� �����- ���#�2��&����� -������������ ���������� ������- �������� ��C������ ������� #

� ��������9�$� �� �� �� � � � ������ � ������-���� ���� � � ��������� � ����'�� ��.���&����� ����� �$������������������@ MF, H#�, ���&��������� � �� � � ���� � � � ��� ����� � ��� �)� �� ��� ������� �� �������#

� �����9�$� �� �� � ������ ��������� ���$� �� � ��� ����� ��)� ��&�� ��� ��'���� � �� � ���� �� � �� ������% � �'� ���� � � � ������� ��� � � � �- ������������#

:��� ������2����'��*�����2����; ����<���� ������ �� ����<������� ���������������������������������������������������� ��I� �

���+#�3�

������ #�����#�3�

�� ��� �#�����#�3�

O ����#�����#�3�

Page 25: Apostila php-avancado1444

����������������������������������������������������������������������������������������������������������������������������������������������������������������

� � ��������)����A���8L������������ �������� ���&���� �+ �� ���$������������P��&���Q#

<?php# teste_arquivo.php# objetivo: gravar conteúdo num arquivo texto e em seguida exibir esseconteúdo

// Inclui o script onde foi definida a classe a ser utilizadainclude_once ('classes/arquivo.class.php');

# Inicializa variáveis que serão passadas como parâmetros$arquivo = "teste.txt";$conteudo = "Isto é um teste";

# Instancia a classe $arq = new arquivo();

# Chama o método de abertura de arquivo (escrita)$arq->abreArq($arquivo,'w');

# Chama o método de gravação de arquivo$arq->gravaArq($conteudo);

# Chama o método de abertura de arquivo (leitura)$arq->abreArq($arquivo,'r');

# Chama o método de leitura de arquivo$arq->leArq();

# Chama o método de fechamento de arquivo$arq->fechaArq();

# Chama o método que exibe o conteúdo do arquivo$arq->exibeArq();

?>

:��� ������2����'��*�����2����; ����<���� ������ �� ����<������� ���������������������������������������������������� ��=� �

Page 26: Apostila php-avancado1444

����������������������������������������������������������������������������������������������������������������������������������������������������������������

� � ��������)����A���:

�������C������ ��+���Y� ����#�3�

<?php

# exibe_noticia.php

include_once ('classes/feed.class.php');

$noticia = new feed('feed.xml');$noticia->ExibeFeed();

?>

:��� ������2����'��*�����2����; ����<���� ������ �� ����<������� ���������������������������������������������������� ���� �