简体   繁体   English

如何通过调用PHP网页获取值?

[英]How can I get a value back from a call to my PHP web page?

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. 我正在构建一个C#应用程序,它需要调用一个网页(在PHP )来请求对数据库中的数据执行验证。 Data is supplied to the PHP page via HTTP parameters in the URL . 数据通过URL HTTP参数提供给PHP页面。

I would like to retrieve a single response value back from a call to my PHP web page. 我想从调用我的PHP网页中检索一个响应值。 In this specific example, I only need a Boolean value. 在这个具体的例子中,我只需要一个Boolean值。 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页面的简化版本:

<?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. 这个PHP页面只是接收值,并在数据库表中插入或更新记录。 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 . 这将继续,但还有另一个表,我想从中提取信息,与提供的信息进行比较,并将验证指示符作为Boolean返回。

And this is my current request code in the C# application: 这是我在C#应用程序中的当前请求代码:

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. 标注的行导致变量包含要返回的页面的完整HTML。

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 从中可以可靠地提取值的东西

Your return is really just and echo or print away - just display the value that you want returned to your c# script. 您的返回真的只是回显或打印 - 只需显示您想要返回到c#脚本的值。

<?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#. 但是你可以创建一个数组或一个对象,并使用json_encode()将值传递给c#。

What is the code inside the function db_doquery ? 函数db_doquery的代码是什么?

Have you tried using an if statement to see if the query has run sucessfully? 您是否尝试使用if语句来查看查询是否已成功运行?

    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. 标注的行导致变量包含要返回的页面的完整HTML。

Well, that's because the PHP page is emitting a web page and not a specific value (as an API call). 嗯,那是因为PHP页面发出的是一个网页,而不是一个特定的值(作为API调用)。 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. 您可以将页面响应加载到DOM解析器(例如, HtmlAgilityPack ,尽管还有其他),并解析出您想要的值。
  2. You can have the PHP code emit only the value(s) you want in some structured form. 您可以让PHP代码仅以某种结构化形式发出所需的值。 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: 由于你有FULL html对象返回,你可以随时做我做的事,在某些标签中设置参数(我称之为'数据',你可以随意调用它们!)你可以用PHP做什么:

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

Then strip the tags in C#: 然后在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. 发送的数据将在theResponse字符串中提供。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从aspx网页在客户端计算机上调用Win应用程序? - How can I call a win app on client machine from my aspx web page? 返回上一页时如何保留搜索过滤器? - How can I keep my search filters when I go back to a previous web page? 如何从asp.net Web表单返回列表对象到ajax回调函数? - How can I return list object to ajax call back function from asp.net web form? 如何从AJAX调用恢复Web API的军事时间 - How to get military time back from AJAX call to web api 如何将值从页面传递到控制器? - How can I pass a value from my page to the controller? 如何将来自C#程序中Web浏览器小程序中加载的网页的参数传递回C#应用程序? - How can I pass an argument from a web page that is loaded inside a web browser applet in a C# program, back to the C# application? 如何从C#中的回调方法获取返回值 - How to get return value from call back method in c# 如何使用C#从Web服务中获取价值? - How to get value back from a web services in C#? 如何从 C# 调用组装过程并返回结果? - How can I call an Assembly procedure from C# and get a result back? (webform)如何从9个不同的文本框中获取值并回传值,然后升序 - (webform) how can i get the value from 9 different textbox and post back the value follow by ascending
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM