[英]How can I get a value back from a call to my PHP web page?
我正在構建一個C#
應用程序,它需要調用一個網頁(在PHP
)來請求對數據庫中的數據執行驗證。 數據通過URL
HTTP參數提供給PHP
頁面。
我想從調用我的PHP網頁中檢索一個響應值。 在這個具體的例子中,我只需要一個Boolean
值。 但是,我學習如何請求任何東西,甚至是一個請求中的多個值(如果可能的話),這似乎是謹慎的。
這是我正在調用的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);
}
}
?>
這個PHP
頁面只是接收值,並在數據庫表中插入或更新記錄。 這將繼續,但還有另一個表,我想從中提取信息,與提供的信息進行比較,並將驗證指示符作為Boolean
返回。
這是我在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();
標注的行導致變量包含要返回的頁面的完整HTML。
我想知道的是我如何能夠將這些響應作為一個或多個回復(無論可能的是什么):
您的返回真的只是回顯或打印 - 只需顯示您想要返回到c#腳本的值。
<?php
if( // true ) {
echo 'TRUE';
} else {
echo 'FALSE';
}
當然你要返回一個字符串,而不是一個布爾值。 但是你可以創建一個數組或一個對象,並使用json_encode()將值傳遞給c#。
函數db_doquery
的代碼是什么?
您是否嘗試使用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';
}
}
標注的行導致變量包含要返回的頁面的完整HTML。
嗯,那是因為PHP頁面發出的是一個網頁,而不是一個特定的值(作為API調用)。 您可以通過以下幾種方式解決此問題:
由於你有FULL html對象返回,你可以隨時做我做的事,在某些標簽中設置參數(我稱之為'數據',你可以隨意調用它們!)你可以用PHP做什么:
send('this string should be returned with data tags');
function send(dat) {
echo '<data>'. $dat .'</data>'
}
然后在C#中刪除標簽:
public string removeDataTags(string fullhtml) {
string rmt = fullhtml.Substring(fullhtml.IndexOf("<data>") + ("<data>").Length);
rmt = rmt.Substring(0, rmt.IndexOf("</data>"));
return rmt;
}
你現在要做的就是:
theResponse = removeDataTags(theResponse);
發送的數據將在theResponse字符串中提供。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.