[英]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): 我想知道的是我如何能够将这些响应作为一个或多个回复(无论可能的是什么):
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:
您可以通过以下几种方式解决此问题:
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.