Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Querying brazilian CEP (postal code) webservices

by fglock (Vicar)
on Mar 08, 2005 at 22:20 UTC ( #437742=snippet: print w/ replies, xml ) Need Help??
Description: This code gets a brazilian postal code and query the "Correios" webservice site for the actual street address. The result is given in XML.
#!/usr/bin/perl

use HTTP::Request;
use LWP::UserAgent;

my $CEP = shift || "91751-000";

my $content =<<"HERE";
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" x
+mlns:xsd="http://www.w3.org/2001/XMLSchema" 
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <Query xmlns="urn:Microsoft.Search">
      <queryXml>
<![CDATA[

<QueryPacket xmlns='urn:Microsoft.Search.Query' revision='1' build='(1
+1.0.5329)'>
  <Query domain='{1698075D-E2F5-4254-87B2-7FC9E9AB0780}'>
    <QueryId>{AE4CDA59-C3EC-496A-B0B3-B21A55EEA0CC}</QueryId>
    <OriginatorId>{F6FF7BE0-F39C-4ddc-A7D0-09A4C6C647A5}</OriginatorId
+>
    <SupportedFormats>
      <Format revision='1'>
urn:Microsoft.Search.Response.Document:Document</Format>
      <Format revision='1'>
urn:Microsoft.Search.Response.Content:Content</Format>
      <Format revision='1'>
urn:Microsoft.Search.Response.Form:Form</Format>
     </SupportedFormats><Context>
    <QueryText type='STRING' language='en-us' >$CEP</QueryText>
    <LanguagePreference>en-us</LanguagePreference>
    <Requery></Requery></Context>
    <Range id='result'></Range>
    <OfficeContext 
xmlns='urn:Microsoft.Search.Query.Office.Context' revision='1'>
      <UserPreferences>
       <ParentalControl>false</ParentalControl>
      </UserPreferences>
      <ServiceData></ServiceData>
      <ApplicationContext>
        <Name>Microsoft Office Word</Name>
        <Version>(11.0.5329)</Version>
        <SystemInformation>
          <SkuLanguage>en-us</SkuLanguage>
          <LanguagePack>en-us</LanguagePack>
          <InterfaceLanguage>en-us</InterfaceLanguage>
          <Location>US</Location>
        </SystemInformation>
      </ApplicationContext>
      <QueryLanguage>en-us</QueryLanguage>
      <KeyboardLanguage>en-us</KeyboardLanguage>
   </OfficeContext>
   <Keywords 
xmlns='urn:Microsoft.Search.Query.Office.Keywords' revision='1'>
    <QueryText>food</QueryText>
    <Keyword><Word>food</Word></Keyword>
   </Keywords>
  </Query>
</QueryPacket>

]]>
      </queryXml>
    </Query>
  </soap:Body>
</soap:Envelope>
HERE

my $ua = LWP::UserAgent->new();

my $request = HTTP::Request->new( 
    POST => 'http://consultacep.correios.com.br/office2003/Query.asmx'
+ 
);
$request->header( 'SOAPAction' => "urn:Microsoft.Search/Query" );
$request->header( 'Content-Type' => "text/xml; charset=utf-8" );
$request->content( $content );

print $request->as_string;

my $response = $ua->request( $request );

print $response->as_string;

Replies are listed 'Best First'.
Re: Querying brazilian CEP (postal code) webservices
by Anonymous Monk on Jan 03, 2006 at 18:41 UTC
    It works smoothly, it pretends to be MS Office, whhen querying
Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: snippet [id://437742]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2016-08-27 09:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    The best thing I ever won in a lottery was:















    Results (379 votes). Check out past polls.