Welcome to WebmasterWorld Guest from 34.201.121.213

Forum Moderators: ocean10000

Message Too Old, No Replies

Help with .net to XML SOAP Php

     
2:22 am on Jul 27, 2008 (gmt 0)

New User

10+ Year Member

joined:Nov 20, 2007
posts:12
votes: 0


I have an sample application in .net C# and it is just a form that interacts with a web service. I need this form to work properly in PHP. I was told that it has to be done using SOAP. I can't seem to get going in the right direction with this. Any help that you would be willing to give would be awesome. Here is the .cs file below and below that is the .config / XML file. Please help me.

-----////App.cs/////-----

using System;
using System.Collections.Generic;
using System.Text;
using apitest.api;
using System.IO;
using System.Threading;
using System.Net;
using System.Configuration;

namespace apitest
{
class TestMain
{
static void Main(string[] args)
{
string api_url = ConfigurationManager.AppSettings["api_url"];
string login_name = ConfigurationManager.AppSettings["login_name"];
string pwd = ConfigurationManager.AppSettings["password"];
string document_name = ConfigurationManager.AppSettings["document_name"];
string data_template_name = ConfigurationManager.AppSettings["data_template_name"];
string list_file_name = ConfigurationManager.AppSettings["list_file_name"];
string image_file_name = ConfigurationManager.AppSettings["image_file_name"];
string pdf_file_name = ConfigurationManager.AppSettings["pdf_file_name"];
string mailing_type_code = ConfigurationManager.AppSettings["mailing_type_code"];
string preferred_schedule_date = ConfigurationManager.AppSettings["preferred_schedule_date"];
string bill_name = ConfigurationManager.AppSettings["bill_name"];
string bill_company = ConfigurationManager.AppSettings["bill_company"];
string bill_address1 = ConfigurationManager.AppSettings["bill_address1"];
string bill_address2 = ConfigurationManager.AppSettings["bill_address2"];
string bill_city = ConfigurationManager.AppSettings["bill_city"];
string bill_state = ConfigurationManager.AppSettings["bill_state"];
string bill_zip = ConfigurationManager.AppSettings["bill_zip"];
string bill_country = ConfigurationManager.AppSettings["bill_country"];
string bill_type = ConfigurationManager.AppSettings["bill_type"];
string bill_number = ConfigurationManager.AppSettings["bill_number"];
string bill_exp_month = ConfigurationManager.AppSettings["bill_exp_month"];
string bill_exp_year = ConfigurationManager.AppSettings["bill_exp_year"];
string api_version = ConfigurationManager.AppSettings["api_version"];

int api_version_number;
try
{
api_version_number = Int32.Parse(api_version);
}
catch
{
throw new Exception("API Version number must be a number.");
}

if (api_version_number < 4 api_version_number > 5)
{
throw new Exception("Invalid API version number specified");
}

Console.Out.WriteLine("Starting test...");

MolAPI api = new MolAPI();

Console.Out.WriteLine("Initializing API url...");
api.Url = api_url;

UriBuilder upload_url_builder = new UriBuilder(api_url);
upload_url_builder.Path = "/upload.aspx";
upload_url_builder.Query = "clientid=" + login_name + "&clientpwd=" + pwd;
string upload_url = upload_url_builder.Uri.ToString();

Console.Out.WriteLine("Finding document...");
name_id[] documents = api.GetDocuments(login_name, pwd);
int document_id = -1;
foreach (name_id document in documents)
{
if (document.name.ToLower() == document_name.ToLower())
{
document_id = document.id;
break;
}
}
if (document_id == -1)
{
throw new Exception(string.Format("Document {0} cannot be found", document_name));
}

Console.Out.WriteLine("Document found ... {0}", document_id);

Console.Out.WriteLine("Finding data template...");
name_id[] data_templates = api.GetDataTemplates(login_name,pwd);
int data_template_id = -1;
foreach(name_id data_template in data_templates)
{
if (data_template.name.ToLower() == data_template_name) {
data_template_id = data_template.id;
break;
}
}
if (data_template_id == -1)
{
throw new Exception(String.Format("Data Template {0} is not found",data_template_name));
}

Console.Out.WriteLine("Data template found ... {0}",data_template_id);
string[] columns;
List<string[]> data_values = new List<string[]>();

Console.Out.WriteLine("Reading List data...");
using (StreamReader reader = new StreamReader(list_file_name))
{
string header = reader.ReadLine();
columns = header.Split(',');
while (!reader.EndOfStream)
{
string line_data = reader.ReadLine();
string[] values = line_data.Split(',');
data_values.Add(values);
}
reader.Close();
}

Console.Out.WriteLine("Building List data...");
name_value[][] name_values = new name_value[data_values.Count][];
for (int a = 0; a < data_values.Count; a++)
{
name_values[a] = new name_value[columns.Length];
for (int b = 0; b < columns.Length; b++)
{
name_values[a]= new name_value();
name_values[a][b].name = columns[b];
if (data_values[a].Length >= b)
{
name_values[a][b].value = data_values[a][b];
}
else
{
name_values[a][b].value = "";
}
}
}
Console.Out.WriteLine("List data built...");

Console.Out.WriteLine("Submitting List...");
int list_id = api.CreateDataList(login_name, pwd, data_template_id, 1, name_values);
Console.Out.WriteLine("List submitted... {0}", list_id);
bool bCompleted = false;
Console.Out.WriteLine("Waiting for list completion");
for (int a = 0 ; a < data_values.Count / 10 + 10; a++)
{
status_type list_status = api.CheckListStatus(login_name, pwd, list_id);
if (list_status.status_id == 5)
{
// completed
bCompleted = true;
break;
}
if (list_status.status_id == 9)
{
// error
throw new Exception(String.Format("Error processing list - {0}",list_status.description));
}
Console.Out.WriteLine("List Status - {0}",list_status.description);
Thread.Sleep(5 * 1000); // sleep for 5 seconds
}
if (!bCompleted)
{
throw new Exception("List processing has timed out");
}

Console.Out.WriteLine("List completed");

Console.Out.WriteLine("Submitting List data 2...");
StringBuilder sb = new StringBuilder();
sb.Append("<data>");
foreach (string[] record in data_values)
{
sb.Append("<record>");
for (int b = 0; b < columns.Length; b++)
{
sb.Append("<variable>");
sb.Append("<name>");
sb.Append(columns[b]);
sb.Append("</name>");
sb.Append("<value>");
if (record.Length >= b)
{
sb.Append(record[b]);
}
sb.Append("</value>");
sb.Append("</variable>");
}

sb.Append("</record>");
}
sb.Append("</data>");

Console.Out.WriteLine("List data 2 built...");
Console.Out.WriteLine("Submitting List 2...");
list_id = api.CreateDataListSimple(login_name, pwd, data_template_id, 1, sb.ToString());
Console.Out.WriteLine("List submitted... {0}",list_id);
bCompleted = false;
Console.Out.WriteLine("Waiting for list completion");
for (int a = 0; a < data_values.Count / 10 + 10; a++)
{
status_type list_status = api.CheckListStatus(login_name, pwd, list_id);
if (list_status.status_id == 5)
{
// completed
bCompleted = true;
break;
}
if (list_status.status_id == 9)
{
// error
throw new Exception(String.Format("Error processing list - {0}", list_status.description));
}
Console.Out.WriteLine("List Status - {0}", list_status.description);
Thread.Sleep(5 * 1000); // sleep for 5 seconds
}
if (!bCompleted)
{
throw new Exception("List processing has timed out");
}

Console.Out.WriteLine("List completed");

if (api_version_number >= 5)
{
Console.Out.WriteLine("Submitting List data 3...");

Console.Out.WriteLine("Uploading List 3...");

string list_file_id = null;
using (StreamReader reader = new StreamReader(list_file_name))
{
char[] buffer = new char[100];

while (!reader.EndOfStream)
{
int nRead = reader.ReadBlock(buffer, 0, 100);
if (list_file_id == null)
{
list_file_id = api.UploadFile(login_name, pwd, new string(buffer, 0, nRead));
}
else
{
api.UploadFileAppend(login_name, pwd, list_file_id, new string(buffer, 0, nRead));
}
}
}

// 1 = CSV File
list_id = api.CreateDataList2(login_name, pwd, data_template_id, "test list", 1, list_file_id);
Console.Out.WriteLine("List submitted... {0}", list_id);
bCompleted = false;
Console.Out.WriteLine("Waiting for list completion");
for (int a = 0; a < data_values.Count / 10 + 10; a++)
{
status_type list_status = api.CheckListStatus(login_name, pwd, list_id);
if (list_status.status_id == 5)
{
// completed
bCompleted = true;
break;
}
if (list_status.status_id == 9)
{
// error
throw new Exception(String.Format("Error processing list - {0}", list_status.description));
}
Console.Out.WriteLine("List Status - {0}", list_status.description);
Thread.Sleep(5 * 1000); // sleep for 5 seconds
}
if (!bCompleted)
{
throw new Exception("List processing has timed out");
}

Console.Out.WriteLine("List completed");

}

Console.Out.WriteLine("Submitting preview...");
int preview_id = api.SubmitPreview(login_name, pwd, list_id, document_id);
bool bPreviewCompleted = false;
for (int a = 0; a < 10; a++)
{
status_type preview_status = api.CheckPreviewStatus(login_name, pwd, preview_id);
if (preview_status.status_id == 2) {
bPreviewCompleted = true;
break;
}
if (preview_status.status_id == 3)
{
throw new Exception(string.Format("Preview error - {0}", preview_status.description));
}
Console.Out.WriteLine("Preview Status: {0}", preview_status.description);
Thread.Sleep(5 * 1000); // sleep for 5 seconds
}

if (!bPreviewCompleted)
{
throw new Exception("Preview timeout");
}
Console.Out.WriteLine("Preview completed");
Console.Out.WriteLine("Getting preview pdf url...");
string pdf_url = api.GetPreviewPDFURL(login_name, pwd, preview_id);
Console.Out.WriteLine("Downloading preview pdf ...");
using (WebClient wc = new WebClient())
{
wc.DownloadFile(pdf_url, pdf_file_name);
}
Console.Out.WriteLine("Downloaded preview pdf");

Console.Out.WriteLine("Submitting job...");
int job_id = api.SubmitJob(login_name, pwd, list_id, document_id, mailing_type_code);
Console.Out.WriteLine("Job id {0} created",job_id);
bool bJobSubmitCompleted = false;
for (int a = 0; a < data_values.Count / 10 + 10; a++)
{
status_type job_status = api.CheckJobStatus(login_name, pwd, job_id);
if (job_status.status_id == 3 job_status.status_id == 4 job_status.status_id == 11)
{
// submitted
bJobSubmitCompleted = true;
break;
}
if (job_status.status_id == 9)
{
// error
throw new Exception(string.Format("Error submitting job - {0}", job_status.description));
}
Thread.Sleep(5 * 1000); // sleep for 5 seconds
}
if (!bJobSubmitCompleted)
{
throw new Exception("Job Submit time out");
}
Console.Out.WriteLine("Submit job completed");

Console.Out.WriteLine("Submitting job 2...");
billing_details billing_info = new billing_details();
billing_info.bill_name = bill_name;
billing_info.bill_company = bill_company;
billing_info.bill_address1 = bill_address1;
billing_info.bill_address2 = bill_address2;
billing_info.bill_city = bill_city;
billing_info.bill_state = bill_state;
billing_info.bill_zip = bill_zip;
billing_info.bill_country = bill_country;
billing_info.bill_type = bill_type;
billing_info.bill_number = bill_number;
billing_info.bill_exp_month = bill_exp_month;
billing_info.bill_exp_year = bill_exp_year;

job_id = api.SubmitJob2(login_name, pwd, list_id, document_id, mailing_type_code,preferred_schedule_date,billing_info);
Console.Out.WriteLine("Job id {0} created", job_id);
bJobSubmitCompleted = false;
for (int a = 0; a < data_values.Count / 10 + 10; a++)
{
status_type job_status = api.CheckJobStatus(login_name, pwd, job_id);
if (job_status.status_id == 3 job_status.status_id == 4 job_status.status_id == 11)
{
// submitted
bJobSubmitCompleted = true;
break;
}
if (job_status.status_id == 9)
{
// error
throw new Exception(string.Format("Error submitting job - {0}", job_status.description));
}
Thread.Sleep(5 * 1000); // sleep for 5 seconds
}
if (!bJobSubmitCompleted)
{
throw new Exception("Job Submit time out");
}
Console.Out.WriteLine("Submit job 2 completed");

if (api_version_number >= 5)
{
// Submit job 3
Console.Out.WriteLine("Submitting job 3...");
billing_info = new billing_details();
billing_info.bill_name = bill_name;
billing_info.bill_company = bill_company;
billing_info.bill_address1 = bill_address1;
billing_info.bill_address2 = bill_address2;
billing_info.bill_city = bill_city;
billing_info.bill_state = bill_state;
billing_info.bill_zip = bill_zip;
billing_info.bill_country = bill_country;
billing_info.bill_type = bill_type;
billing_info.bill_number = bill_number;
billing_info.bill_exp_month = bill_exp_month;
billing_info.bill_exp_year = bill_exp_year;

return_address_details return_address = new return_address_details();
return_address.name = bill_name;
return_address.address = bill_address1 + " " + bill_address2;
return_address.business = bill_company;
return_address.city = bill_city;
return_address.state = bill_state;
return_address.zip = bill_zip;
return_address.ancillary_endorsement = "";
job_id = api.SubmitJob3(login_name, pwd, list_id, document_id, mailing_type_code, preferred_schedule_date, billing_info,return_address);
Console.Out.WriteLine("Job id {0} created", job_id);
bJobSubmitCompleted = false;
for (int a = 0; a < data_values.Count / 10 + 10; a++)
{
status_type job_status = api.CheckJobStatus(login_name, pwd, job_id);
if (job_status.status_id == 3 job_status.status_id == 4 job_status.status_id == 11)
{
// submitted
bJobSubmitCompleted = true;
break;
}
if (job_status.status_id == 9)
{
// error
throw new Exception(string.Format("Error submitting job - {0}", job_status.description));
}
Thread.Sleep(5 * 1000); // sleep for 5 seconds
}
if (!bJobSubmitCompleted)
{
throw new Exception("Job Submit time out");
}
Console.Out.WriteLine("Submit job 3 completed");

}

if (api_version_number >= 5)
{
Console.Out.WriteLine("Getting list of projects ...");
name_id[] projects = api.GetProjects(login_name, pwd);
foreach (name_id project in projects)
{
Console.Out.WriteLine(string.Format("Project found - {0}", project.name));
}
string image_file_id;

Console.Out.WriteLine("Uploading test image file ...");
using (WebClient wc = new WebClient())
{
try
{
byte[] result = wc.UploadFile(upload_url, image_file_name);
image_file_id = ASCIIEncoding.ASCII.GetString(result);
}
catch
{
throw new Exception(string.Format("Error uploading image file - {0}", image_file_name));
}
}

Console.Out.WriteLine("Creating test image file ...");
// 4 = jpeg
int[] image_ids = api.CreateBitmapImage(login_name, pwd, "test image", -1, 4, image_file_id);

Console.Out.WriteLine("Deleting image");
foreach (int image_id in image_ids)
{
api.DeleteImage(login_name, pwd, image_id);
}

Console.Out.WriteLine("Deleting previously uploaded list");

api.DeleteList(login_name, pwd, list_id);

}

Console.Out.WriteLine("Getting job costs...");
job_cost costs = api.GetJobCost(login_name, pwd, job_id);
Console.Out.WriteLine("Job Costs: production=${0} , postage=${1}",costs.total_production_cost,costs.total_postage_cost);

Console.Out.WriteLine("Test completed");
}
}
}

-------// Config File ///-------------

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="apitest.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</sectionGroup>
</configSections>
<applicationSettings>
<apitest.Properties.Settings>
<setting name="apitest_api_MolAPI" serializeAs="String">
<value>http://localhost:1324/MolAPI.asmx</value>
</setting>
</apitest.Properties.Settings>
</applicationSettings>
<appSettings>
<add key="api_url" value="http://example.com/molapi.asmx"/>
<add key="login_name" value="your login name"/>
<add key="password" value="Your password"/>
<add key="document_name" value="Name of your document"/>
<add key="data_template_name" value="Name of your data template"/>
<add key="list_file_name" value="Name of your list file"/>
<add key="pdf_file_name" value="test proof.pdf"/>
<add key="mailing_type_code" value="First Class"/>
<add key="preferred_schedule_date" value="02/16/2007"/>
<add key="bill_name" value="Your Name"/>
<add key="bill_company" value="Your Company"/>
<add key="bill_address1" value="Your address"/>
<add key="bill_address2" value=""/>
<add key="bill_city" value="Your City"/>
<add key="bill_state" value="Your State Code"/>
<add key="bill_zip" value="Your Zip Code"/>
<add key="bill_country" value="USA"/>
<add key="bill_type" value="Credit Card"/>
<add key="bill_number" value="Your Credit Card number"/>
<add key="bill_exp_month" value="12"/>
<add key="bill_exp_year" value="2010"/>
<add key="api_version" value="5"/>
<add key="image_file_name" value="Name of jpg file"/>
</appSettings>
</configuration>

[[b]edited by: eelixduppy at 2:39 am (utc) on July 27, 2008]
[edit reason] exemplified [/edit]

10:07 pm on July 27, 2008 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:June 13, 2002
posts:2175
votes: 0


You need to look at nusoap for php, you will be up and running in 5 minutes. Once you have the basic proxy class you can get help on the programming detail on the php forum.