2017-09-03 52 views
1

因此,我有一個遠程(託管)MySQL數據庫,我使用PHP服務連接到該數據庫。我需要我的ASP.Net c#web應用程序和我的android與它通信。但是,我正在用我從服務中檢索的所有信息填充我的Web應用程序模板。例如,我想填充用戶的配置文件頁面。從連接到遠程MySQL數據庫的PHP服務中檢索/獲取數據以顯示到我的ASP.Net Web應用程序

下面是我的PHP連接和通信到數據庫:

`// Create connection 
     $conn = new mysqli($servername, $username, $password, $dbname); 
// Check connection 
if ($conn->connect_error) { 
    die("Connection failed: " . $conn->connect_error); 
} 

$sql = "SELECT * FROM Vendor Where VendorId = 2"; //this is just a test 
$result = $conn->query($sql); 

if ($result->num_rows > 0) { 
    // output data of each row 
    while($row = $result->fetch_assoc()) { 
     echo . $row["id"]. . $row["BusinessName"]. . $row["Location"]. . $row["Email"]. .$row["Website"]. .$row["ProductType"]. .$row["Contact"]. .$row["PaymentOption"]. .$row["ProfileImg"]."<br>"; 
    } 
} else { 
    echo "0 results"; 
} 
$conn->close(); 
` 

,然後(不分享我所有的設置),這將是asp.net C#代碼示例和我的PHP文件進行通信/服務。

public void getUserInfo(int id) 
     { 


      string BusinessName = lblBusiness.Text.Trim(); 
      string email = lblEmail.Text.Trim(); 
      string Contact = lblPhone.Text.Trim(); 

      string location = lblLocation.Text.Trim(); 
      string Website = lblWebsite.Text.Trim(); 

      string payment = lblPayment.Text.Trim(); 

      //Variables to get information from the service 
      Stream dataStream = null; 
      WebResponse response = null; 
      StreamReader reader = null; 
      //Stores the result from the server 
      string responseFromServer = null; 
      try 
      { 
       string requestMethod = "GET"; 
       //Sending this data across the stream 
       string postData = "&Email=" + email + "&BusinessName=" + BusinessName + "&Website=" + Website + "&PaymentOption=" + payment + "&Location=" + location + "&ProductType=" + ProductType + "&Contact=" + Contact + ""; 
       byte[] byteArray = Encoding.UTF8.GetBytes(postData); 
       string URL = "";// url of php service location 
       string contenttype = "application/x-www-form-urlencoded"; 
       //Create link to web service 
       WebRequest request = WebRequest.Create(URL); 
       //Pass the request method 
       request.Method = requestMethod; 
       request.ContentType = contenttype; 
       request.ContentLength = byteArray.Length; 
       dataStream = request.GetRequestStream(); 
       dataStream.Write(byteArray, 0, byteArray.Length); 
       //Get response from the server 
       response = request.GetResponse(); 
       dataStream = response.GetResponseStream(); 
       reader = new StreamReader(dataStream); 
       responseFromServer = reader.ReadToEnd(); 
      } 
      catch (WebException ex) 
      { 
       Console.WriteLine(ex.ToString()); 
      } 
      finally 
      { 
       if (dataStream != null && reader != null && response != null) 
       { 
        dataStream.Close(); 
        reader.Close(); 
        response.Close(); 
       } 
       //Getting the response from the service 
       //string result = responseFromServer.ToString(); 

      } 

     } 

此外,我不知道該從哪個函數返回。 請幫忙。

+0

這個概念是以你可以使用的格式返回你需要的數據。這種格式,如果你正在創建對話者,可以隨心所欲。但是,有些方法大部分是標準化的。查找「JSON」。 – deg

+0

無論您使用何種語言或您擁有多少應用程序都無關緊要。創建一個RESTful api並將其用作每個多應用程序中的數據訪問層。那麼所有這些應用程序都可以使用此api相互集成並傳遞數據。 – Valkyrie

回答

1

我相信,你的PHP Web應用程序託管,您可以在ASP.NET中使用以下步驟使用PHP服務:

WebClient client = new WebClient(); //Create WebClient object 

string url = "http://test.com/test.php"; //Get the URL of the PHP service 

byte[] html = client.DownloadData(url); //Byte array to hold returned data from the service 

最後使用UTF8Encoding對象將字節數組轉換爲sring:

UTF8Encoding utf = new UTF8Encoding(); //Create an object of the UTF8Encoding class 

string str = utf.GetString(html); //Convert data into string 
+0

這是另一個使用Curl請求的選項 - http://stackoverflow.com/questions/16619065/curl-request-with-asp-net –

+0

謝謝!這工作。完美與大多數,如果不是全部我的功能! – CodeGirlAnon

2

你的php文件是我按下的「API」。
你基本上需要從你的PHP文件中返回這個。

$vendorArray = [ 
    "Id" => $row["id"], 
    "BusinessName" => $row["BusinessName"], 
    // ... this is just pseudo code, convert the array or take the array or something like that 
]; 

header('Content-type: application/json'); 
echo json_encode($vendorArray); 

然後在asp.net你這樣做:

var deserializedVendor = JsonConvert.DeserializeObject<Vendor>(responseFromServer); 

你的供應商類別必須與您的JSONObject是Deserializable

public class Vendor { 

    public string Id {get;set;} 

    public string BusinessName {get;set;} 

    ... 
} 

這取決於你的jsonResponse ...

你也可以直接反序列化成一個複雜的項目,像這樣的供應商列表:

var allTheVendorsDeserialized = JsonConvert.DeserializeObject<AllTheVendors>(responseFromServer); 


public class AllTheVendors { 

    public bool Success {get;set} 

    public List<Vendor> {get;set} 

} 

PHP在哪裏:

$arr = ["Success" => true, $myArrayOfVendors]; 

header('Content-type: application/json'); 
echo json_encode($arr); 
相關問題