2014-05-19 84 views
0

我試圖保持同一個類在各種功能中活着。這與PHP完美協作,但是當我在C#中使用相同的函數時,它無法按預期工作。PHP nusoap(webservice)保持類活着+ C#

我的index.php文件在此由:

<?php 
    require_once('nusoap/lib/nusoap.php'); 
    require_once('libs/webservice.php'); 

    $web = new webservice(); // this is a class 

    function login($user_email, $user_password){ 
     global $web; 
     return json_encode($web->login($user_email, $user_password)); 
    } 

    function getClients(){ 
     global $web; 
     return json_encode($web->getClients()); 
    } 

    $HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : ''; 
    $server = new soap_server(); 
    $server->register("login"); 
    $server->register("getClients"); 
    $server->service($HTTP_RAW_POST_DATA); 

?> 

如果我添加的$server->service($HTTP_RAW_POST_DATA);下面的PHP代碼只是爲了測試後:

echo login("[email protected]", "testing"); // returns TRUE > which is correct 
echo getClients(); // returns TRUE > which is correct 

它的偉大工程。該課程包含將用於其他功能的user_id。 現在,類web服務有以下幾點:

class webservice{ 

    private $user; 
    private $user_id; 

    public function __construct(){ 
     require_once("../libs/user.php"); 
     $this->user  = new users(); 
    } 

    public function login($email, $password){ 
     $results = $this->user->logIn($email, sha1($password)); 

     if($results['id'] == null){ 
      return json_encode(array("message" => false)); 
     } 

     $this->user_id = $results['id']; // holds the user id for further functions needs 

     return json_encode(array("message" => true)); 
    } 

    public function getClients(){ 
     if($this->user_id == null){ 
      return json_encode(array("message" => false)); 
     } 

     return json_encode(array("message" => true)); 
    } 
} 
?> 

正如我所說的,在PHP這個偉大工程。但是,當我在C#中使用此webservice時,由於user_id等於null,我無法從getclients()函數檢索結果TRUE。 不成立user_id一次登錄。

WebReference.myWebservice webS = new WebReference.myWebservice(); 
public Form1() 
{ 
    InitializeComponent(); 
} 

private void Form1_Load(object sender, EventArgs e) 
{ 
    JObject login = JObject.Parse(webS.login("[email protected]", "testing")); 
    MessageBox.Show(login["message"].ToString()); // returns TRUE 

    JObject clients = JObject.Parse(webS.getClients()); 
    MessageBox.Show(clients["message"].ToString()); // returns FALSE 
} 

我知道問題是出在的index.php頁面,但我怎麼能實現我在尋找什麼呢?

回答

0

每當你打電話給你的web服務你的腳本將在php中創建一個class webservice的新實例。這意味着您的$user_id將不會保留上次請求的用戶ID。

您應該通過登錄函數返回散列(會話)和用戶標識。將散列和用戶標識存儲在c#中。之後,你的hash和userid應該是每個請求的一部分。在你的webservice類中驗證它並返回用戶請求的結果。

我希望你能按照我的指示。

也許看一看到Twitter的REST服務API: https://dev.twitter.com/docs/auth/application-only-auth

我喜歡他們做的方式。您發送身份驗證請求並且Twitter會返回一個名爲bearer的哈希。之後,每個請求都會包含一個帶有承載者值的授權標頭。

+0

是的,我知道該怎麼做......但我正在跳槽,以我的方式實施。 但是,如果沒有人有任何線索,如果這是可能的,我會去那樣。 – Linesofcode

+0

問題是請求完成後,您的實例不見了。這就是http請求的工作方式。如果你想這樣做,你需要用php(一個運行在無限循環中的php腳本)構建一個套接字服務器。但我認爲這不是最簡單的解決方案。 – steven

+0

是的,它不是最簡單的解決方案。好吧,謝謝。我會去尋找你的解決方案。 – Linesofcode