Hi All
I have spent days trying to do a simple task, yet am having no luck at all!
I am looping through a directory, containing xml files. For each file, I am trying to open the file, read the contents, and post it to a URL.
My problem is, i'm not sure if I am reading the file correctly and posting the contents. Also how do I encode the xml contents?
The code is as follows:
sub sendXMLDocs
{
my $userAgent = LWP::UserAgent->new(agent => 'Mozilla/5.0 (WinNT; I)');
my $pattern = $outDir."*\.xml";
foreach my $fileName (glob($pattern))
{
# Open the file for readonly
open(XML_FILE_CONTENTS, $fileName);
#post the file contents to infoMedia
my $response = $userAgent->request(POST '
http://1.httpfeeds.XXXXXXXX.com/SKY/SSSSS/POST/',
Content_Type => 'application/x-www-form-ur
lencoded',
Content => XML_FILE_CONTENTS);
#Close the file.
close(XML_FILE_CONTENTS);
#print $response->error_as_HTML unless $response->is_success;
#print $response->as_string;
if ($response->is_success) {
#Read the XMl response from Infomedia
#[
#<?xml version="1.0" standalone="yes"?>
#<result>
# <event>01</event>
# <status>EMPTY</status>
# <text>The input was empty</text>
#</result>
#]
#Get the response from InfoMedia
my $xmlResponse = $response->content;
#Strip all carriage returns & line feeds
$xmlResponse =~ s/\r\n//g;
$logFile->log ("xmlResponse : $xmlResponse");
#Load response XML
my $objParser = XML::DOM::Parser->new ();
my $requestDoc = $objParser->parse ($xmlResponse);
#Grab code, description, details from xml response.
my $xmlResponseCode = $requestDoc->find ('/result/event')->string_
value ();
my $xmlResponseDesc = $requestDoc->find ('/result/status')->string
_value ();
my $xmlResponseDetails = $requestDoc->find ('/result/text')->string_v
alue ();
print "xmlResponseCode : ".$xmlResponseCode."\n";
print "xmlResponseDesc : ".$xmlResponseDesc."\n";
print "xmlResponseDetails : ".$xmlResponseDetails."\n\
n\n";
#Check the response
if ($xmlResponseCode == "01")
{
$logFile->log ("FILE NAME : $fileName");
$logFile->log ("ERROR CODE : 01");
$logFile->log ("ERROR DESC : $xmlResponseDesc");
$logFile->log ("ERROR DETAILS: $xmlResponseDetails");
$logFile->log ("EXPLANATION : A valid XML payload could not be found.\n\n\n");
}
elsif ($xmlResponseCode == "02")
{
$logFile->log ("FILE NAME : $fileName");
$logFile->log ("ERROR CODE : 02");
$logFile->log ("ERROR DESC : $xmlResponseDesc");
$logFile->log ("ERROR DETAILS: $xmlResponseDetails");
$logFile->log ("EXPLANATION : Includes a message giving more details on the nature of the error. Invalid XML markup was submitted.\n\n\n");
}
elsif ($xmlResponseCode == "03")
{
$logFile->log ("FILE NAME : $fileName");
$logFile->log ("ERROR CODE : 03");
$logFile->log ("ERROR DESC : $xmlResponseDesc");
$logFile->log ("ERROR DETAILS: $xmlResponseDetails");
$logFile->log ("EXPLANATION : If this error is encountered please notify the administrator, informing them of the Result_Details.\n\n\n");
}
elsif ($xmlResponseCode == "00")
{
#Successful post. Delete the file.
$logFile->log ("FILE NAME : $fileName\n");
$logFile->log ("EXPLANATION : The feed was submitted successfully.\n");
#Try and delete the file.
$deleted = unlink($fileName);
if ($deleted)
{
$logFile->log ("FILE DELETED\n\n\n");
}
else
{
$logFile->log ("***FILE NOT DELETED***\n\n\n");
}
}
} else {
#Error occured posting to InfoMedia
print $response->error_as_HTML;
$logFile->log ("FILE NAME: $fileName\n");
$logFile->log ("ERROR : Could not post file. $response->error_as_HTML\n
\n\n");
}
}
}
Thanks for any help!
Start Free Trial