2012-06-19 150 views
0

我目前正在開發一個Windows Phone上的移動應用程序,它要求我向位於服務器上的PHP文件發送請求。PHP文件在本地工作,但不在服務器上

我遇到了一個問題,即請求在服務器上拋出一個錯誤,但不是當我更改代碼來頁面本地PHP文件時。任何想法爲什麼?

這裏是我的請求代碼:

 string sendString = ""; 
     sendString += "&lat=" + (App.Current as App).mapCenter.Latitude; 
     sendString += "&lng=" + (App.Current as App).mapCenter.Longitude; 
     sendString += "&address=" + (App.Current as App).locationAddress; 
     if (checkNumber(CountryCode.Text, AreaCode.Text, Number.Text)) 
     { 
      sendString += "&phone=" + CountryCode.Text + "" + AreaCode.Text + "" + Number.Text; 
     } 
     else 
     { 
      MessageBox.Show("Phone number invalid"); 
     } 
     if (checkEmail(email.Text)) 
     { 
      sendString += "&email=" + email.Text; 
     } 
     else 
     { 
      MessageBox.Show("Invalid Email"); 
     } 
     sendString += "device=WindowsPhone"; 
     sendString += (bool)prefsms.IsChecked ? "&contactMethod=sms" : "&contactMethod=email"; 
     ListPickerItem selectedItem = (ListPickerItem)ServiceType.SelectedItem; //Cast listpickeritem into String 
     string content = (string)selectedItem.Content; 
     sendString += "&serviceType=" + content; 
     sendString += "&version=2.0"; 
     sendString += "&language=" + CultureInfo.CurrentUICulture.TwoLetterISOLanguageName; 
     (App.Current as App).watcher.Stop(); 
     sendData = sendString; 


     if ((checkNumber(CountryCode.Text, AreaCode.Text, Number.Text)) && (checkEmail(email.Text))) 
     { 
      //Send data to server 
      HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("http://localhost/test.php"); 
      myRequest.Method = "POST"; 
      myRequest.ContentType = "application/x-www-form-urlencoded"; 
      myRequest.BeginGetRequestStream(new AsyncCallback(RequestCallBack), myRequest); 
     } 
     else 
     { 
     } 

private void RequestCallBack(IAsyncResult asyncresult) 
{ 
     HttpWebRequest myRequest = asyncresult.AsyncState as HttpWebRequest; 
     Stream dataStream = myRequest.EndGetRequestStream(asyncresult); 
     //Avoid trashing procedure 
     this.Dispatcher.BeginInvoke(delegate() 
     { 
      StreamWriter writer = new StreamWriter(dataStream); 
      writer.Write(sendData); 
      writer.Flush(); 
      writer.Close(); 
      //Expect a response 
      myRequest.BeginGetResponse(new AsyncCallback(ResponseCallBack), myRequest); 
     }); 

    } 
    private void ResponseCallBack(IAsyncResult asyncresult) 
    { 
     HttpWebRequest myRequest = asyncresult.AsyncState as HttpWebRequest; 
     HttpWebResponse myResponse = (HttpWebResponse)myRequest.EndGetResponse(asyncresult); 
     // Avoid trashing the procedure 
     this.Dispatcher.BeginInvoke(delegate() 
     { 
      // Retrieve response stream 
      Stream responseStream = myResponse.GetResponseStream(); 
      StreamReader reader = new StreamReader(responseStream); 
      // Show response 
      responseTxt = reader.ReadToEnd(); 
      MessageBox.Show("" + responseTxt); 
     }); 

    } 

我送一個字符串到服務器,服務器的PHP代碼如下所示(測試PHP是完全一樣的):

require_once("includes/globals.php"); 

$vars = ($PRODUCTION)?$_POST:$_REQUEST; 

$MINIMUM_VERSION = 2.0; 
function validServiceType($type) 
{ 
return $type == "taxi" || $type == "ladies" || $type == "limo" || $type == "handicap"; 
} 

if(!isset($vars['version']) || ((float)$vars['version']) < $MINIMUM_VERSION) 
{ 
echo "oldVersion"; 
} 
elseif(isset($vars['lat'], $vars['lng'], $vars['phone'], $vars['language'])) 
{ 
    /*do stuff */ 
if($data['contactMethod'] == "email" && $data['email'] == "") 
{ 
    echo "invalid"; 
    die(); 
} 
if(DB_AddHail($data)) 
    echo "processing"; 
else if (($company = DB_GetReferralTaxi($data['lat'], $data['lng'], $data['serviceType']))) 
{ 
    /*do some more stuff*/ 
} 
else 
    echo "sorry"; 
} 
else 
{ 
echo "invalid"; 
} 

?> 

我不斷收到一個OldVersion錯誤,但我已經將版本號設置爲2.0了......關於發生了什麼的任何想法?

更新:我現在已經能夠找出問題的根源; HttpWebRequest根本沒有發送任何數據到服務器;我的$ _POST只是一個空數組。任何想法爲什麼?

更新2:我得到的錯誤是錯誤302(「我使用的URI只是http://而不是https://)。但是在將協議更改爲https://之後,它仍然沒有':T的工作是我的代碼不正確的那部分

+0

這段代碼是否與你的代碼相同?' return $ type ==「taxi」|| $ type ==「女士們」|| $ type ==「limo」|| $ type == handicap「;'如果是,請嘗試從結尾刪除'''! – undone

+0

^耶逃跑障礙。 – Killrawr

+0

對不起,這是我的一個錯字;障礙已經在實際的現場版 – docaholic

回答

0

你可以試試,立即行之前,如果(isset開始($瓦爾?![' 版本']),並補充說:

echo "version: " . $vars['version']; 
exit; 

見如果它向瀏覽器輸出任何內容,可能是因爲你使用的php版本不像$ vars =($ PRODUCTION)?$ _ POST:$ _ REQUEST;語法,你可能需要手動完成所以像

$vars['version']=$_POST['version']; 
$vars['somethingElse']=$_POST['somethingElse'] 

等等

+0

是的,我試過了,$ vars ['version']給了我「2.0」,所以我真的不明白爲什麼它不起作用 – docaholic

+0

然後我很抱歉地說我也不確定。我將您的確切代碼複製並粘貼到現場製作服務器上,並且效果非常好...祝您好運!希望有人能得到更多的幫助。 – JoeTomato

+0

哈哈感謝您的幫助;我想我會嘗試使用不同的方法(使用WebClient而不是HttpWebRequest)對其進行重新編碼,並查看是否有所作爲 – docaholic

0

確保您的5.4.3,並與的phpinfo運行腳本();去檢查。嘗試改變你是PHP/C#來接收/發送GET請求,而不是POST請求。

這樣,您可以至少測試一下,看看您何時發送PHP腳本的請求,例如http://yourdomain.com/script.php?name=John+Smith&Age=23,如果您獲得預期的結果。下載https://addons.mozilla.org/en-US/firefox/addon/live-http-headers/以檢查結果時從瀏覽器的角度看標題是如何工作的。

+0

是的,我也試過了,它給出了相同的結果 – docaholic

+0

@docaholic因爲你的跑步關閉本地主機你有沒有試過改變你的URL呢?從「http:// localhost /」到「http://WORKSTATION_GROUP.USERNAME.local/test.php」(打開命令提示符並運行ping localhost。)。有些東西告訴我你的C#代碼找不到localhost,是C IDE中聲明爲環境變量的本地主機?也有GET請求設置你的內容類型爲「text/html」,看看是否有幫助。 – Killrawr

+0

嗯......即使使用GET,我仍然會得到相同的錯誤。另外,服務器的設置只能處理POST請求;我知道服務器也可以正常工作,因爲我有一個具有相同服務的網站,並且功能完美。 – docaholic

相關問題