Tratamento de Webserver de Notícias

host-own-webserver

Como formatar um serviço de notícias que vem do webservice,  este código foi feito para tratar as notícias que eram fornecidas pela http://www.enfoque.com.br/, você pode utilizar isso em diversos webservices que dispõe desta mesma estrutura de XML, pode ser tanto em SOAP, como ASMX, isso varia de acordo com o servidor que disponibiliza o serviço. Você mesmo pode habilitar este tipe de serviço em algum servidor, basta saber como montar um webservice, confira no site: http://devedge-temp.mozilla.org/viewsource/2002/soap-overview/index_pt_br.html como montar um webservice.

formato XML:


<?xml version="1.0" encoding="utf-8"?>
<DataSet xmlns="http://tempuri.org/">
 <xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
 <xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:Locale="pt-BR">
 <xs:complexType>
 <xs:choice maxOccurs="unbounded">
 <xs:element name="Table">
 <xs:complexType>
 <xs:sequence>
 <xs:element name="idpagina" type="xs:int" minOccurs="0" />

 <xs:element name="data" type="xs:string" minOccurs="0" />
 <xs:element name="titulo" type="xs:string" minOccurs="0" />
 <xs:element name="URL" type="xs:string" minOccurs="0" />
 </xs:sequence>
 </xs:complexType>
 </xs:element>
 </xs:choice>
 </xs:complexType>
 </xs:element>

 </xs:schema>
 <diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">
 <NewDataSet xmlns="">
 <Table diffgr:id="Table1" msdata:rowOrder="0">
 <idpagina>2733487</idpagina>
 <data>2011-04-18 07:48:13.000</data>
 <titulo>Bolsas da Ásia e da Europa fecham em reação à medidas da economia chinesa</titulo>

 <URL>http://www.enfoque.com.br/headlines/ENEWS/042011/25458.asp</URL>
 </Table>
 <Table diffgr:id="Table2" msdata:rowOrder="1">
 <idpagina>2733489</idpagina>
 <data>2011-04-18 07:57:49.000</data>
 <titulo>Dilma: pincipais objetivos de viagem à China foram alcançados</titulo>
 <URL>http://www.enfoque.com.br/headlines/ENEWS/042011/25459.asp</URL>

 </Table>
 <Table diffgr:id="Table3" msdata:rowOrder="2">
 <idpagina>2733511</idpagina>
 <data>2011-04-18 08:30:03.000</data>
 <titulo>Agenda Econômica Semanal</titulo>
 <URL>http://www.enfoque.com.br/headlines/enews/agendaSemanal.asp</URL>
 </Table>

 <Table diffgr:id="Table4" msdata:rowOrder="3">
 <idpagina>2733512</idpagina>
 <data>2011-04-18 08:30:03.000</data>
 <titulo>Agenda Econômica Semanal</titulo>
 <URL>http://www.enfoque.com.br/headlines/enews/agendaSemanal.asp</URL>
 </Table>
 </diffgr:diffgram>
</DataSet>

página de tratamento do XML do webserver:


<?php
function DataBr($data = "0000-00-00 00:00:00") {
 if ($data ==  "0000-00-00 00:00:00" or $data == NULL) {
 return NULL;
 exit();
 }
 $temp = explode(" ", $data);
 $temp2 = explode("-", $temp[0]);
 $temp3 = explode(".",$temp[1]);
 $temp4 = explode(":",$temp3[0]);
 //$this->debug($data);
 if (count($temp) > 1) {
 return trim($temp2[2].'/'.$temp2[1].' '.$temp4[0].":".$temp4[1]);
 } else {
 return trim($temp2[2].'/'.$temp2[1].$temp2[0]);
 }
 }
function my_xml2array($__url)
{
 $xml_values = array();
 $contents = $__url; //file_get_contents($__url);
 $parser = xml_parser_create('');
 if(!$parser)
 return false;

 xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, 'UTF-8');
 xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
 xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
 xml_parse_into_struct($parser, trim($contents), $xml_values);
 xml_parser_free($parser);
 if (!$xml_values)
 return array();

 $xml_array = array();
 $last_tag_ar =& $xml_array;
 $parents = array();
 $last_counter_in_tag = array(1=>0);
 foreach ($xml_values as $data)
 {
 switch($data['type'])
 {
 case 'open':
 $last_counter_in_tag[$data['level']+1] = 0;
 $new_tag = array('name' => $data['tag']);
 if(isset($data['attributes']))
 $new_tag['attributes'] = $data['attributes'];
 if(isset($data['value']) && trim($data['value']))
 $new_tag['value'] = trim($data['value']);
 $last_tag_ar[$last_counter_in_tag[$data['level']]] = $new_tag;
 $parents[$data['level']] =& $last_tag_ar;
 $last_tag_ar =& $last_tag_ar[$last_counter_in_tag[$data['level']]++];
 break;
 case 'complete':
 $new_tag = array('name' => $data['tag']);
 if(isset($data['attributes']))
 $new_tag['attributes'] = $data['attributes'];
 if(isset($data['value']) && trim($data['value']))
 $new_tag['value'] = trim($data['value']);

 $last_count = count($last_tag_ar)-1;
 $last_tag_ar[$last_counter_in_tag[$data['level']]++] = $new_tag;
 break;
 case 'close':
 $last_tag_ar =& $parents[$data['level']];
 break;
 default:
 break;
 };
 }
 return $xml_array;
}

//
// use this to get node of tree by path with '/' terminator
//
function get_value_by_path($__xml_tree, $__tag_path)
{
 $tmp_arr =& $__xml_tree;
 $tag_path = explode('/', $__tag_path);
 foreach($tag_path as $tag_name)
 {
 $res = false;
 foreach($tmp_arr as $key => $node)
 {
 //echo is_int($key)?$node['name']."<br>":'';
 if(is_int($key) && $node['name'] == $tag_name)
 {
 $tmp_arr = $node;
 $res = true;
 break;
 }
 }
 if(!$res)
 return false;
 }
 return $tmp_arr;
}
header('Content-Type: text/html; charset=utf-8');
$conteudo= file_get_contents("http://endereco_webserver/pagina_webserver.asmx/variavel?login=usuario&senha=1234");
$arr = my_xml2array($conteudo);
$c=0;

foreach(array_reverse($arr[0][1][0]) as $item) {
 if (isset($item['name']) && $item['name']=="Table") {
?>
<div id="<? echo ($c%2==0)?'branco':'cinza';?>"><a href="carrega.php?p=<?=$item[3]['value'];?>" title="<?=$item[2]['value'];?>" target='_parent' class=><strong><?=DataBr($item[1]['value']);?></strong> &nbsp;&nbsp;<?=$item[2]['value'];?></a></div>
<?
 $c++;}
}
?>

Deixe um Comentário

O seu endereço de email não será publicado Campos obrigatórios são marcados *

*

Você pode usar estas tags e atributos de HTML: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>