Jump to content

Translating ASP instructions into PHP?


galvin

Recommended Posts

Big favor needed.  I am working on a website for a customer who uses a web-based software that tracks leads.  On their website, there will be a relatively simple HTML form that will submit Contact Info and they would like that info to go into their software's "Leads" section.

 

The issue is that the site is in PHP and the instructions from the software company are only for ASP (the sites server does not support ASP).

 

So I'm fairly certain I can do the same thing listed below using PHP, but I'm having trouble figuring out how.

 

This might be super easy, or might be super complicated, I'm really not sure.  I am a bit familiar with AJAX which I believe is how this is going to be done, but I can't figure out how the Ajax function should interact with PHP code.

 

But if anyone can read it over and tell me how I would do the same thing using PHP, I would be forever in your debt :) 

 

If it's complicated, then rather than tell me exactly how to do it, maybe you could direct me to a good tutorial on submitting XML from forms via XMLHTTP using PHP???  I'll take any feedback, I'm desperate :)

 

 

INSTRUCTIONS FROM THE WEB-BASED SOFTWARE COMPANY:

Technical Specifications

• Leads must be submitted via XMLHTTP to http://<server address>/xml/importlead.asp.

• The submitted XML must conform to the schema (XSD) provided below.

• All fields must be HTML-encoded. This will ensure that they will not cause errors from invalid XML.

 

Schema (XSD)

<?xml version="1.0" encoding="utf-8" ?>
<xs:schema id="LeadImport" elementFormDefault="qualified" attributeFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="LeadImportDatagram">
	<xs:complexType>
		<xs:sequence>
			<xs:element name="CompanyKey" type="xs:string" />
			<xs:element name="LeadInfo">
				<xs:complexType>
					<xs:sequence>
						<xs:element name="Category" type="xs:string" minOccurs="0" /> <!--up to 50 characters-->
						<xs:element name="Subcategory" type="xs:string" minOccurs="0" /> <!--up to 50 characters-->
						<xs:element name="OfferName" type="xs:string" minOccurs="0" /> <!--up to 50 characters-->
						<xs:element name="Contact" type="xs:string" minOccurs="0" /> <!--up to 35 characters-->
						<xs:element name="Company" type="xs:string" minOccurs="0" /> <!--up to 35 characters-->
						<xs:element name="Address" type="xs:string" minOccurs="0" /> <!--up to 35 characters-->
						<xs:element name="Address2" type="xs:string" minOccurs="0" /> <!--up to 35 characters-->
						<xs:element name="City" type="xs:string" minOccurs="0" /> <!--up to 20 characters-->
						<xs:element name="State" type="xs:string" minOccurs="0" /> <!--up to 2 characters-->
						<xs:element name="Zip" type="xs:string" minOccurs="0" /> <!--up to 10 characters-->
						</xs:element>
					</xs:sequence>
				</xs:complexType>
			</xs:element>
		</xs:sequence>
	</xs:complexType>
</xs:element>
</xs:schema>

 

Sample XML

<?xml version="1.0" encoding="utf-8" ?>
<LeadImportDatagram>
<CompanyKey>212121</CompanyKey>
<LeadInfo>
	<Category>Gutters & Drains</Category>
	<Subcategory>Broken Gutter Repair</Subcategory>
	<OfferName>First month free with one year contract</OfferName>
	<Contact>Graham Carpenter</Contact>
	<Company>Union Boxing Company</Company>
	<Address>34 Railroad Rd.</Address>
	<Address2></Address2>
	<City>Newark</City>
	<State>DE</State>
	<Zip>34852</Zip>
	</LeadInfo>
</LeadImportDatagram>

Sample ASP Code for Creating XML from an HTML Form and Submitting via XMLHTTP

<%
'get CompanyKey; set this to the Company Key used to login
cCompanyKey = "212121"

'get server address
cServer = "http://192.168.1.111/"

'create lead xml
cXML = "<?xml version=""1.0"" encoding=""utf-8"" ?>" & _
       "<LeadImportDatagram>" & _
       "<CompanyKey>" & cCompanyKey & "</CompanyKey>" & _
"<NotificationEmailTo>joeschmoe@aol.com</NotificationEmailTo>" & _
       "<LeadInfo>"

'add nodes to LeadInfo for each item from form
For Each oItem In Request.Form
'This code assumes that the form fields match up exactly with the node names, including uppercase/lowercase.
'You may need to write custom code here if that's not true for your form.
cXML = cXML & "<" & oItem & ">" & Request.Form(oItem) & "</" & oItem & ">"
Next

'add node for lead cost
cXML = cXML & "</LeadInfo>" & _
              "</LeadImportDatagram>"

'send lead to provider
Set oXML = Server.CreateObject("Microsoft.XMLDOM")
oXML.async = False
oXML.loadXML(cXML)

'verify that the server address is in the expected format
If Left(cServer, 4) <> "http" Then cServer = "http://" & cServer
If Right(cServer, 1) = "/" Then cServer = Left(cServer, Len(cServer) - 1)

'submit the XML to the server
Set oXMLHTTP = Server.CreateObject("Microsoft.XMLHTTP")
oXMLHTTP.open "POST", cServer & "/xml/importlead.asp", False
oXMLHTTP.send oXML

'check for an error
If oXMLHTTP.statusText <> "OK" Then
'print the error to the screen; this will help with debugging.
Response.Write "Error sending XML: " & oXMLHTTP.responseText
Response.End
End If

'At this point, we know that the submission was successful.
Set oXMLHTTP = Nothing
Set oXML     = Nothing

'redirect to the next screen
Response.Redirect "thanks.asp"
%>

Link to comment
Share on other sites

Here's the translation. Please note that I didn't test it because I couldn't (192.168 is type C -> local) Making it work will be your end of the bargain.

 

$companyKey = '212121';
$server = 'http://192.168.1.111/';


$xml = '<?xml version="1.0" encoding="utf-8"?>' .
       '<LeadImportDatagram>'.
           '<CompanyKey>' . $companyKey . '</CompanyKey>' .
           '<NotificationEmailTo>joeschmoe@aol.com</NotificationEmailTo>' .
           '<LeadInfo>';

foreach($_POST as $key => $value) {
    //This code assumes that the form fields match up exactly with the node names, including uppercase/lowercase.
    //You may need to write custom code here if that's not true for your form.
    $xml .=     '<' . $key . '>' . htmlentities($value) . '</' . $key . '>';
}

$xml .=     '</LeadInfo>' .
        '</LeadImportDatagram>';

if(substr($server, 0, 4) != 'http') $server = 'http://' . $server;
if(substr($server, -1) == '/') $server = rtrim($server, '/');

$curlConfig = array(
    CURLOPT_POST = > true,
    CURLOPT_RETURNTRANSFER => true
);

$curl = curl_init($server . '/xml/importlead.php');
curl_setopt_array($curlConfig);
$responseText = curl_exec();
$httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);

if($httpCode != 200) {
    echo 'Error sending XML: ' . $responseText;
    exit(0);
}

header('Location: thanks.php');
exit(0);

Link to comment
Share on other sites

Thanks Ignace!  I tried this and one thing I noticed is that for this line you wrote:

$curl = curl_init($server . '/xml/importlead.php');

 

That should actually be "asp" because the server we are submitting the lead too uses asp.  So I changed it to be:

$curl = curl_init($server . '/xml/importlead.asp');

 

and did a test and I get back this below.  Not sure if the Warning is important, but as for the runtime error, I have no idea what that means.  Any clue?

 

Warning: curl_setopt_array() expects exactly 2 parameters, 1 given in /home/gsaldutt/public_html/submit.php on line 30

Microsoft VBScript runtime error '800a01a8'

Object required: 'oXML.documentElement'

/xml/importlead.asp, line 33 Error sending XML: 1

 

 

Could the problem be that we are trying to submit using PHP to a page that's written in ASP (i.e. $curl = curl_init($server . '/xml/importlead.asp'); or should that not matter?

Link to comment
Share on other sites

I made the change you said and tried it again and this time it only said:

 

Error sending XML:

Bad Request (Invalid Number)

 

Not sure if that changes things or not? :)

 

If anyone out there can confirm if this SHOULD work (i.e. sending to an ASP page via PHP code) or if it will simply never work no matter what I do, I would appreciate it. Thanks!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.