Avatar billede diz Nybegynder
08. juni 2006 - 16:26 Der er 4 kommentarer

Send XML via HTTP post

Hejsa,

Jeg har brug for at sende et XML dokument til en server, hvor jeg indkluderer nogle varible i headeren. Men jeg kan simpelthen ikke få det til at virke.

Her er min kode:
----------------
        $body = '<?xml version="1.0"?>
<RTMWeblet>
<!– xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" –>
<!– xsi:noNamespaceSchemaLocation="http://172.18.252.143/rtm/RTMWebletReq.xsd"> –>
<RTMEmailToEmailAddress>
<AcknowledgementsTo>
<EmailAddress>xxx</EmailAddress>
<Option>0</Option>
</AcknowledgementsTo>
<ClientName>xxx</ClientName>
<SiteName>xxx</SiteName>
<CampaignName>xxx</CampaignName>
<MailingName>xxx</MailingName>
<MaximumDeferrableMinutes>20</MaximumDeferrableMinutes>
<ActionAfterDeferredTime>0</ActionAfterDeferredTime>
<ToEmailAddress>
<EventEmailAddress>
<EmailAddress>xxx</EmailAddress>
<EventVariables>
<Variable>
<Name>EventVar:ev1</Name>
<Value>
<![CDATA[
<SetVar Name="var:aktiv” Value="1">
<SetVar Name="var:navn” Value="xxx">
]]>
</Value>
</Variable>
</EventVariables>
</EventEmailAddress>
</ToEmailAddress>
</RTMEmailToEmailAddress>
</RTMWeblet>';
       
$url = "http://rtm.ddc.dartmail.net/weblet/weblet.dll";
   
$res = (PostIt($body,$url));
echo implode('',$res);

  function PostIt($DataStream, $URL) {

//  Strip http:// from the URL if present
    $URL = ereg_replace("^http://", "", $URL);

//  Separate into Host and URI
    $Host = substr($URL, 0, strpos($URL, "/"));
    $URI = strstr($URL, "/");

//  Form up the request body
    $ReqBody = "";
    while (list($key, $val) = each($DataStream)) {
      if ($ReqBody) $ReqBody.= "&";
      $ReqBody.= $key."=".urlencode($val);
    }
    $ContentLength = strlen($body);

//  Generate the request header
    $ReqHeader =
        "Username: talefod_dmc\n".
          "Password: Talefod3Connect\n".
          "Servername: EDC2\n".
        $body."\n";

//  Open the connection to the host
    $socket = fsockopen($Host, 80, &$errno, &$errstr);
    if (!$socket) {
      $Result["errno"] = $errno;
      $Result["errstr"] = $errstr;
      return $Result;
    }
    $idx = 0;
    fputs($socket, $ReqHeader);
    while (!feof($socket)) {
      $Result[$idx++] = fgets($socket, 128);
    }
    return $Result;
  }
--------------

Når jeg sender dette afsted får jeg følgende svar fra serveren:

HTTP/1.1 400 Bad Request Server: Microsoft-IIS/5.0 Date: Thu, 08 Jun 2006 14:21:11 GMT Content-Type: text/html Content-Length: 87 The parameter is incorrect.

Hvor jeg skulle få:

<?xml version="1.0" encoding="US-ASCII" ?>
- <DMResponse xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://rtm.ddc.dartmail.net/weblet/RTMResponse.xsd">
  <Code>-100</Code>
  <Description>The Servername provided in the HTTP header is unknown.</Description>
  </DMResponse>

Her er et link til et pl-script som virker (bare fyld noget tilfældigt i):

http://demo.doubleclick.com/rtm/rtm_demo.pl

Håber nogen kan hjlæpe :)

/Sune
Avatar billede iss Novice
08. juni 2006 - 16:27 #1
Brug cURL...
Avatar billede diz Nybegynder
08. juni 2006 - 16:28 #2
Her er koden til pl-scriptet, hvis det skulle være til nogen hjælp:

rtm_demo.pl:
------------

#!perl
# Epsilon Interactive DREAMmail RTM Demo script
#
# This CGI perl script tries to take the post parameter of a form
# and reformat it as RTM Weblet request protocol
# and post it to real RTM Weblet for processing
use CGI;
use HTTP::Request::Common qw(POST);
use LWP::UserAgent;
#
# All the form parameters
#
$cgi = new CGI;
$farm = $cgi->param('farm');
$username = $cgi->param('username');
$password = $cgi->param('password');
$post_url = $cgi->param('url');
$xml_code = $cgi->param('rtm_xml');
#
# Check if any field is empty
#
if ($username eq "" or $password eq "" or $farm eq "" or $post_url eq ""
Real-Time Messaging Guide Triggering Real-Time Messages
Epsilon Interactive DREAMmail Last Modified June 1, 2006 48
or $xml_code eq "") {
print $cgi->header(-type => 'text/html', expires => '+1s');
#
# Display default page from the file rtm_demo.htm
#
my $save_eol = $/;
undef $/;
open (IN, "rtm_cgi_demo.htm");
$content = <IN>;
close IN;
$/ = $save_eol;
print $content;
exit;
}
#
# Setup the POST request
#
$ua= LWP::UserAgent -> new();
$req = HTTP::Request->new(POST => $post_url);
# RTM Request:
# HTTP Header contains servername, username and password
$req->header('ServerName' => $farm);
$req->header('UserName' => $username);
$req->header('Password' => $password);
# HTTP POST content with the xml code
$req->content($xml_code);
# Submit the request
$resp = $ua->request($req);
# Print out the result from the RTM Weblet
print $cgi->header(-type=> 'text/html', expires => '+1s');
print $resp->content;

----------------------------------------------




rtm_cgi_demo.htm
----------------

<html>
<head>
<title>Epsilon Interactive DREAMmail RTM Demo Form</title>
<style>
h3 {font-family: verdana, arial;}
td {font-family: verdana, arial; font-size: 12px;}
input {font-family: verdana, arial; font-size: 12px;}
textarea {font-family: verdana, arial; font-size: 12px;}
</style>
<script language=javascript>
function generateXML() {
rtm.rtm_xml.value = '<RTMWeblet xmlns:xsi="http://
www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://rtm.ddc.DREAMmail.net/
weblet/RTMWebletReq.xsd">\n' +
'<RTMEmailToEmailAddress>\n' +
'<ClientName>' + rtm.client.value + '</ClientName>\n' +
'<SiteName>' + rtm.site.value + '</SiteName>\n' +
'<CampaignName>' + rtm.campaign.value + '</CampaignName>\n' +
'<MailingName>' + rtm.mailing.value + '</MailingName>\n' +
'<ToEmailAddress>\n'+
'<EventEmailAddress>\n' +
'<EmailAddress>' + rtm.email.value + '</EmailAddress>\n' +
'</EventEmailAddress>\n' +
'</ToEmailAddress>\n ' +
'</RTMEmailToEmailAddress>\n' +
'</RTMWeblet>';
}
function validate() {
var ret = false;
if (rtm.username.value == '') {
alert("Please type in username");
} else if (rtm.password.value == '') {
alert("Please type in password");
} else if (rtm.farm.value == '') {
alert("Please type in farm");
} else if (rtm.url.value == '') {
alert("Please type in post url");
} else if (rtm.rtm_xml.value == '') {
alert("Please provide the XML for Weblet");
} else {
ret = true;
}
return ret;
}
</script>
</head>
<h3>Epsilon Interactive DREAMmail RTM Demo Form</h3>
Real-Time Messaging Guide Triggering Real-Time Messages
Epsilon Interactive DREAMmail Last Modified June 1, 2006 46
<form name=rtm method=post>
<table bgcolor=#bbbbbb cellspacing=3 width=650>
<tr><td colspan=4><b>RTM Weblet HTTP Header</b></td></tr>
<tr>
<td>Username: </td>
<td><input type=text name=username size=20 maxlength=50></td>
</tr>
<tr>
<td>Password: </td>
<td><input type=password name=password size=20 maxlength=50></
td>
</tr>
<tr>
<td>DM Farm: </td>
<td><input type=text name=farm size=20 maxlength=50></td>
</tr>
<tr>
<td>POST URL: </td>
<td colspan=3><input type=text name=url size=80 maxlength=120
value="http://rtm.ddc.DREAMmail.net/weblet/weblet.dll"></td>
</tr>
</table>
<table height=10><tr><td height=3></td></tr></table>
<table bgcolor=#cccccc cellspacing=3 width=650>
<tr><td colspan=4><b>RTM Weblet Post Content</b></td></tr>
<tr>
<td>Client Name: </td>
<td><input type=text name=client value="" size=25
maxlength=50></td>
<td>Site Name: </td>
<td><input type=text name=site value="" size=25 maxlength=50></
td>
</tr>
<tr>
<td>Campaign Name: </td>
<td><input type=text name=campaign value="" size=25
maxlength=50></td>
<td>Mailing Name: </td>
<td><input type=text name=mailing value="" size=25
maxlength=50></td>
</tr>
<tr>
<td>Email Address: </td>
<td><input type=text name=email value="" size=25 maxlength=80></
td>
</tr>
<tr>
<td colspan=4 align=right height=50 valign=top><input
Real-Time Messaging Guide Triggering Real-Time Messages
Epsilon Interactive DREAMmail Last Modified June 1, 2006 47
type=button value="Generate XML" onclick="generateXML()"></td>
</tr>
<tr>
<td width=120 valign=top>RTM XML Request:</td>
<td colspan=3><textarea name=rtm_xml cols=80 rows=15></textarea>
</tr>
<tr>
<td align=right colspan=4>
<input type=submit value="Submit RTM" onclick="return
validate();">
</td>
</tr>
</table>
</form>
</html>
</form>
Avatar billede diz Nybegynder
08. juni 2006 - 16:36 #3
iss->

Kan du give et eksempel?

/Sune
Avatar billede iss Novice
08. juni 2006 - 17:02 #4
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, ""); // der sætter du hvad du skal poste, gøres som var det en GET.
curl_setopt($ch, CURLOPT_URL, ""); // url der skal postes til
curl_exec($ch);
curl_close($ch);
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Vi tilbyder markedets bedste kurser inden for webudvikling

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester