is it possible to get values from a asp.net to a php script?

Dani AI

Generated

As suggested, serializing data to a file is one valid path, but there are several practical ways to get values from ASP.NET into PHP. Common choices are: have ASP.NET POST data to a PHP URL (JSON is easiest), write a shared file (XML or JSON) that PHP reads, or use a shared database or REST service. Which is best depends on hosting (same server vs. different machines), concurrency needs, and security.

Example: PHP reading a simple XML file produced by ASP.NET (adjust path and permissions):

<?php
$path = 'C:\\MyFolder\\Person.xml';
if (file_exists($path)) {
    $xml = simplexml_load_file($path);
    echo "FirstName: " . (string)$xml->FirstName . "\n";
    echo "LastName:  " . (string)$xml->LastName . "\n";
}
?>

Example: PHP endpoint that accepts JSON posted from ASP.NET:

<?php
$raw = file_get_contents('php://input');
$data = json_decode($raw, true);
if ($data) {
    $first = $data['FirstName'] ?? '';
    $last  = $data['LastName']  ?? '';
    // validate and process...
}
?>

Notes and cautions: prefer JSON over XML for simplicity and smaller payloads; when using files, write to a temp file and atomically rename to avoid partial reads; ensure the IIS/PHP user has appropriate read/write permissions; set correct content-type (e.g., application/json) when posting; always validate and sanitize incoming data and use HTTPS for transport. For modern ASP.NET code use HttpClient to POST JSON; for legacy .NET Framework other WebRequest patterns can be used. This complements ’s file approach and gives a simpler, production-ready route when direct HTTP POST/REST is available.

Recommended Answers

All 4 Replies

Convert your data to XML and send it to PHP.

Convert your data to XML and send it to PHP.

how? can you give a sample source code please?...

I don't know the side of PHP but there should be enough classes to work with XML.
In this example I created a simple class called Person. This class will be serialized to an XML file called Person.xml. Most .NET objects are serializable so you can serialize almost any object, think of DataTable, DataSet etc.

Though serialization is a huge topic, here is a simple sample that can easily be modified to serialize many types of objects.

If you need your own custom class to be serialized, first create the class and mark it as Serializable:

[Serializable()]
public class Person
{
    public string FirstName;
    public string LastName;

    public Person() { }

}

Because you are writing to disk and you are going to use Xml serialization, include the following namespaces.

using System.IO;
using System.Xml.Serialization;

The following code creates the person class, then uses the filestream to create a file. Next it serializes the object and passes it to the filestream that writes the data to disk.

Person p = new Person();
p.FirstName = "Jim";
p.LastName = "Jones";
        
FileStream fs = new FileStream(@"c:\MyFolder\Person.xml", FileMode.Create);
XmlSerializer xs = new XmlSerializer(typeof(Person));
xs.Serialize(fs, p);
fs.Close();

Then you need to find the PHP code to read from it. Again (Xml)serialization is a big topic and you can customize the xml output completely. For more I information about (Xml)serializing I suggest you do some Online searching on this topic.

I hope this was helpfull

Thank you very much for your kind reply...
Thank very much for the sample you gave.. it helps a lot

Now I have a clue where to start studying

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.