我有一份把数据寄给与NSURLRequest和NSURLConnexion的PHP书。 我想以员额方法发送像“Hello World”这样的数据,我希望在我的“Hello world from myPHP. .php”上显示,我没有网络服务,特别是没有PHP的经验。
我的客观法典发出了以下要求:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL
URLWithString:@"http://localhost:8888/testNSURL/index.php"]
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
[request setHTTPMethod:@"POST"];
NSString *postString = @"myVariable=Hello world !";
[request setHTTPBody:[postString dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:request delegate:self];
if (theConnection) {
receiveData = [NSMutableData data];
}
这是我的PHP代码:
<?php
if(isset($_REQUEST["myVariable"])) {
echo json_encode("Hello from index.php !");
}
else echo json_encode( Request failed );
?>
我的问题是:
- Is my PHP code correct?
- How can I catch the
echo json_encode("Hello from index.php !");
in my obj-c app?