How we can pass data from PHP to ASP,ASP.net?
Top Questions
PHP to ASP
Let's first look at how you can pass data from PHP to ASP using WDDX.
You create a WDDX packet by first serializing data into a WDDX packet and then presenting it.
WDDX can convert most variable types that applications use, such as strings, integers, and arrays. After a variable has been converted to a WDDX variable type, it is ready for another application to pick up.
PHP has WDDX support built in, so you don't need to modify your PHP.ini file to start using WDDX.
A PHP Script to Serialize WDDX
<?php
//define PHP data to send
$ValueToSend = "Andrew";
//convert PHP data to WDDX data
$wddxvar = wddx_serialize_value("$ValueToSend");
//output WDDX data
print("$wddxvar");
?>
You first set the data you want to serialize into a WDDX packet:
$ValueToSend = "Andrew";
You then serialize that data:
$wddxvar = wddx_serialize_value("$ValueToSend");
Finally, you present that data as
print("$wddxvar");
********************************
O/P=Andrew
********************************
Internet Explorer just shows the data within the WDDX packet. It doesn't show the surrounding XML structures. However, if you choose the view source option, you can see the WDDX packet's XML structures
ASP Script to Deserialize WDDX Data
To receive the WDDX packet from the PHP script, you must load the packet
into a variable within the receiving ASP script. None of the WDDX implementations (the WDDX COM component or PHP's WDDX functions) provides native ways of doing this. You must add this functionality using separate code. After you receive the WDDX packet, you can convert it into a data type native to the receiving application. This is called deserializing.
Using ASP, you can use a third-party COM component. A free COM component that allows such functionality is ASP Tear from http://www.alphasier-rapapa.com/IisDev/Components/ (also included with the WDDX SDK). However, you can use any COM component that has similar functionality.
<%
set aspget = Server.CreateObject("SOFTWING.AspTear")
set wddxob = Server.CreateObject("WDDX.Deserializer.1")
'get WDDX data
wddxdata = aspget.Retrieve("http://localhost/phpbook/Chapter5_Sessions/WDDX/PHP/
two_wddxserver.php", Request_POST, "", "", "")
'convert WDDX data to ASP data
wddxvar = wddxob.deserialize(wddxdata)
'output ASP data
response.write "Hello " & wddxvar
set wddxob = nothing
set aspget = nothing
%>
First, you must load the WDDX and ASP Tear COM objects into memory for use by ASP. Don't worry too much if you are not familiar with using COM objects
set aspget = Server.CreateObject("SOFTWING.AspTear")
set wddxob = Server.CreateObject("WDDX.Deserializer.1")
Next you use ASP Tear to obtain the WDDX packet produced by the PHP
server and load it into a variable:
'get WDDX data
wddxdata = aspget.Retrieve("http://localhost/phpbook/Chapter5_Sessions/WDDX/PHP/
two_wddxserver.php", Request_POST, "", "", "")
You deserialize the WDDX packet into a native data type for ASP:
'convert WDDX data to ASP data
wddxvar = wddxob.deserialize(wddxdata)
You then output the value:
'output ASP data
response.write "Hello " & wddxvar
Finally, you unload the COM objects from memory:
set wddxob = nothing
set aspget = nothing
ASP to PHP
PHP can also receive WDDX packets. Here you will see a PHP script obtaining a WDDX packet from an ASP script.
An ASP Script to Serialize WDDX
<%
'define ASP data to send
ValueToSend = "Andrew"
set wddxob = Server.CreateObject("WDDX.Serializer.1")
'convert ASP data to WDDX data
wddxvar = wddxob.serialize(ValueToSend)
'output WDDX data
response.write wddxvar
set wddxob = nothing
%>
This is very much the same as the serializing PHP script. First, you define the value you want to serialize in WDDX:
'define ASP data to send
ValueToSend = "Andrew"
You then load the WDDX COM object into memory, ready for use by ASP:
set wddxob = Server.CreateObject("WDDX.Serializer.1")
You then serialize the value into a WDDX packet:
'convert ASP data to WDDX data
wddxvar = wddxob.serialize(ValueToSend)
You then display the WDDX packet:
'output WDDX data
response.write wddxvar
Finally, you unload the WDDX COM object from memory:
set wddxob = nothing
A PHP Script to Deserialize WDDX Data
The deserialize PHP script needs to do the same as the deserialize ASP script: It must convert the WDDX packet back into a native variable type for our script (in this case, a native variable type for PHP):
<?php
//get WDDX data
$wddxdata = join ('', file
('http://localhost/phpbook/Chapter5_Sessions/WDDX/ASP/one_wddxserver.asp'));
//convert WDDX data to PHP data
$wddxvar = wddx_deserialize("$wddxdata");
//output PHP data
print("Hello " . $wddxvar);
?>
You can obtain the WDDX packet from the ASP script using the PHP join function. To use the function in this manner, you must make sure that HTTP transparency is enabled in the PHP.ini settings file (the allow_url_fopen setting is enabled). (Note that HTTP transparency is enabled by default.)
$wddxdata = join ('', file
('http://localhost/phpbook/Chapter5_Sessions/WDDX/ASP/one_wddxserver.asp'));
As before, you deserialize the WDDX packet into a native variable type:
$wddxvar = wddx_deserialize("$wddxdata");
You then display the WDDX packet:
print("Hello " . $wddxvar);
Post new comment