Question

I am building a C# application that needs to call out to a web page (in PHP) to request that validation be performed against data in a database. Data is supplied to the PHP page via HTTP parameters in the URL.

I would like to retrieve a single response value back from a call to my PHP web page. In this specific example, I only need a Boolean value. However, it seems prudent that I learn how to request anything, perhaps even multiple values in one request (if that's even possible).

This is a simplified version of the PHP page I am making the call to:

<?php
    $type = $_GET['type'];
    $accessid = $_GET['accessid'];
    $license = $_GET['license'];
    $machine = $_GET['machine'];
    $osver = $_GET['osver'];
    $ip = getenv("REMOTE_ADDR");

    $query = "select * from validatetable where licnum = '" . $license . "'";
    if ($result = db_doquery($query))
    {
        if (db_dofetcharray($result))
        {
            $query = "update validatetable set lastdate = CURRENT_TIMESTAMP, lastmachine = '" . $machine . "', accessip = '" .  $ip . "', osver = '" . $osver . "' where type = '" . $type . "' and licnum = '" . $license . "'";
            db_doquery($query);
        }
        else
        {
            $query = "insert into validatetable set type = '" . $type . "', name = '<unknown>', licnum = '" . $license . "', accessid = '" . $accessid . "', lastdate = CURRENT_TIMESTAMP, machine = '" . $machine . "', accessip = '" .  $ip . "', osver = '" . $osver . "'";
            db_doquery($query);
        }
    }
?>

This PHP page is simply receiving values and either inserting or updating a record in a database table. This will continue, but there's another table from which I would like to extract information, compare to what was supplied, and return a validation indicator as Boolean.

And this is my current request code in the C# application:

String webUrl = String.Format("http://www.mywebsite.com/validate.php?type=type&accessid={0}&license={1}&machine={2}&osver={3}.{4}", accessID, licNum, clientMachineName, Environment.OSVersion.Version.Major, Environment.OSVersion.Version.Minor);
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(webUrl);
request.Credentials = CredentialCache.DefaultCredentials;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode == HttpStatusCode.OK)
{
    using (Stream dataStream = response.GetResponseStream())
    {
        using (StreamReader reader = new StreamReader(dataStream, Encoding.UTF8))
        {
            String theResponse = reader.ReadToEnd();  //<--- THIS LINE
        }
    }
}
response.Close();

The noted line results in the variable containing the full HTML of the page being returned.

What I would like to know is how I can get the response back as one or more of these (whatever is possible):

  1. A single value
  2. A collection of values
  3. Something from which I can reliably extract a value
Was it helpful?

Solution

Your return is really just and echo or print away - just display the value that you want returned to your c# script.

<?php
if( // true ) {
echo 'TRUE';
} else { 
echo 'FALSE';
}

of course you are returning a string, not a boolean. But you can create an array or an object and json_encode() it to pass the values to c#.

OTHER TIPS

What is the code inside the function db_doquery?

Have you tried using an if statement to see if the query has run sucessfully?

    if (db_dofetcharray($result))
    {
        $query = "update validatetable set lastdate = CURRENT_TIMESTAMP, lastmachine = '" . $machine . "', accessip = '" .  $ip . "', osver = '" . $osver . "' where type = '" . $type . "' and licnum = '" . $license . "'";

        if(db_doquery($query))
        {
            echo '1';
        }
        else
        {
            echo '0';
        }
    }
    else
    {
        $query = "insert into validatetable set type = '" . $type . "', name = '<unknown>', licnum = '" . $license . "', accessid = '" . $accessid . "', lastdate = CURRENT_TIMESTAMP, machine = '" . $machine . "', accessip = '" .  $ip . "', osver = '" . $osver . "'";

        if(db_doquery($query))
        {
            echo '1';
        }
        else
        {
            echo '0';
        }
    }

The noted line results in the variable containing the full HTML of the page being returned.

Well, that's because the PHP page is emitting a web page and not a specific value (as an API call). You can approach this in a couple of ways:

  1. You can load the page response into a DOM parser (HtmlAgilityPack for example, though there are others) and parse out the value you want.
  2. You can have the PHP code emit only the value(s) you want in some structured form. While you'll still only get a string back from that, you can more easily deserialize it into an object if it follows that object's structure. Or, if the value is really simple (a number, a boolean, etc.) then you can just parse the result into that type.

Since you have the FULL html object being return, you can always do what I do, set the parameters in certain tags (I call mine 'data', you can call them whatever you want!) What you can do in PHP:

send('this string should be returned with data tags');
function send(dat) {
  echo '<data>'. $dat .'</data>'
}

Then strip the tags in C#:

public string removeDataTags(string fullhtml) {
  string rmt = fullhtml.Substring(fullhtml.IndexOf("<data>") + ("<data>").Length);
  rmt = rmt.Substring(0, rmt.IndexOf("</data>"));
  return rmt;
}

All you have to do now is have:

theResponse = removeDataTags(theResponse);

And the data sent will be available in the theResponse string.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top