2012-01-26 128 views
1

這是我的第一個問題,我希望能得到一些幫助:iPhone - PHP POST請求

我用幾種方法(並與不同的HTTP傳輸框架試過),使在後的行動PHP腳本localhost沒有好的結果。我不知道還有什麼要尋找。我一直堅持這個至少6或7小時,終於到這一點:

目標C代碼:

if (name != nil && message != nil) { 
     NSMutableString *postString = [NSMutableString stringWithString:TFPostURL]; 

     [postString appendString:[NSString stringWithFormat:@"%@=%@",TFName,name]]; 

     [postString appendString:[NSString stringWithFormat:@"%@=%@",TFMessage,message]]; 

     [postString setString:[postString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; 

     NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:postString]]; 

     [request setHTTPMethod:@"POST"]; 

     postConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES]; 


    } 

TFPostURL,TFName,TFMessage,姓名和消息常量由我定義。

我使用的數據庫只是爲了檢查傳輸已經成功完成:

PHP腳本

<?php 

$username = "root"; 
$database = "db"; 

mysql_connect(localhost, $username); 

@mysql_select_db($database) or die("Unable to find database"); 

$name = $_POST["name"]; 

$message = $_POST["message"]; 

$query = "INSERT INTO test VALUES ('', '$name', '$message')"; 

mysql_query($query) or die(mysql_error("error")); 

mysql_close(); 

?> 

任何想法,什麼是錯誤的?

我一直在localhost試過,仍然需要在另一臺mac上試試這個。 我正在上傳我的php文件到公共領域,並檢查它是否在我自己的計算機中。


編輯

我已經改變了PHP POST操作就搞定了,獲得驚人的效果;現在它工作!

<?php 

$username = "root"; 
$database = "db"; 

mysql_connect(localhost, $username); 

@mysql_select_db($database) or die("Unable to find database"); 

$name = $_GET["name"]; 

$message = $_GET["message"]; 

$query = "INSERT INTO test VALUES ('', '$name', '$message')"; 

mysql_query($query) or die(mysql_error("error")); 

mysql_close(); 

?> 
+0

只要你知道,模擬器上的localhost應該可以工作 - 模擬器上的Safari能夠訪問我的mac的http服務器。嘗試在iOS模擬器的Safari瀏覽器中打開您的php腳本,看看數據庫條目是否彈出。如果是這樣,那麼你可以肯定它是導致問題的應用程序代碼,而不是php。 – Tim

+0

歡迎來到SO,Fernando! – rdlowrey

+0

謝謝你們!我已經解決了我的問題......真的,我從來沒有看到過這種方式。檢查編輯! –

回答

-1

我建議你使用這種方法從iPhone發送到瓦爾任何PHP的主機:

-(void) httpRequestSender{ 
NSString *par1 = @"test1"; 
NSString *par2 = @"test2"; 
NSString *[email protected]"http://something/postToDb.php"; 
NSString *urlString =[NSString stringWithFormat:@"%@?par1=%@&par2=%@",requestedURL,par1,par2]; 
NSURL *url = [NSURL URLWithString:urlString]; 
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url]; 
[request setHTTPMethod:@"GET"]; 

NSURLResponse *response = nil; 
NSError *error = nil; 
NSData *result = [NSURLConnection sendSynchronousRequest:request 
    returningResponse:&response error:&error]; 
NSString *resultString = [[NSString alloc] initWithData:result encoding:NSUTF8StringEncoding]; 
//resultString is the returned buffer from calling the page 
[request release]; 

} 
+0

感謝您的反饋!現在我會嘗試一下,我知道我的PHP文件有什麼問題。檢查編輯! –

+0

您可以隨時使用$ _REQUEST而不是$ _GET和$ _POST,因爲它涵蓋了兩個方法變量 – Ashraf

+0

我會記住在接下來的幾天內測試,我會隨時通知您。 –

3

我建議使用ASIHTTP libraries。他們很棒。以下是一些示例代碼:

ASIHTTPRequest* req = [[[ASIHTTPRequest alloc] initWithURL:[NSURL URLWithString:@"localhost"]] autorelease]; 
[req setCompletionBlock:^{ 
    NSLog(@"It works! Response was %@",[req responseString]); 
}]; 
[req startAsynchronous]; 

它使異步響應變得簡單,並且可以使用像老闆這樣的閉包。

我已經嘗試過一段時間處理內置的NSURLRequest方法,並且強烈希望能夠在不創建單獨的委託和處理方法的情況下進行異步請求的簡單性。它還支持發佈數據和文件,以及其他一些我甚至還沒有使用的技巧。

+0

您好,我已經嘗試過ASIHTTP庫,但沒有好的結果,儘管每個人都沒有問題地使用它們。這表明有些奇怪的錯誤。 –

+0

你確定你的php代碼正在工作嗎?你可以嘗試上面的代碼,但讓你的網頁只需簡單的字符串,並驗證模擬器是否收到響應? **什麼不工作?**請求完全?值正在發送? – Tim

+0

使用過的網絡調試器和事情都很順利,開始在我的php文件中進行更改,並且如您所想,這就是問題所在。簡單地將HTTP POSTs更改爲GET並通過它。愚蠢的權利? –