2012-10-23 27 views
3

我嘗試從我的應用程序發送一些文本到PHP,但不知何故,我從來沒有收到文本。這裏有什麼不對?php參數傳輸

NSString *name = [[NSString alloc]initWithString: @"Hello World"]; 
NSString *email = [[NSString alloc]initWithString: @"Ohai2u"]; 
NSString *urlString = [NSString stringWithFormat:@"http://www.myhosturlxyz.com/tracktxt.php?name=%@ email=%@", 
         [name stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding], 
         [email stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; 

NSURL *url = [[NSURL alloc] initWithString:urlString]; 

NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url]; 

NSData *urlData; 
NSURLResponse *response; 
urlData = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:&response error:nil]; 
[url release]; 

我所做的PHP是

<?php 
    $name = $_REQUEST['name']; 
    $email = $_REQUEST['email']; 
    $myFile = "tracktxtf.txt"; 
    $fh = fopen($myFile, 'a') or die("can't open file"); 
    fwrite($fh, "my posted var is " .$name $email "\n"); 
    fclose($fh); 
?> 
+0

'tracktxtf.txt'是否存在? – Leri

回答

2

只是胡亂猜測,但你可能需要name=%@email=%a之間&,使之name=%@&email=%@

或者,您可以嘗試使用$_GET['name']$_GET['email']而不是REQUEST嗎?也許嘗試寫入print_r($_REQUEST, true); print_r($_GET, true);到您的調試文件,並看看它包含什麼。

+0

我添加了「&」,改變了POST/REQUEST的結果。 –

+0

$ _REQUEST和$ _GET變量中包含什麼?它不應該是一個POST,而是一個GET。 –

+0

奇怪,當我在日誌中查看我看到.... tracktxt.php name = .......它就像「?」不在字符串中。名稱和電子郵件變量的內容是通過ok發送的,但是php和變量之間的連接沒有生成....因此變量永遠不會在php腳本中進一步處理。 –