我正在使用iOS
构建Xcode
应用程序。这样,我需要从在线php page
中获取一个值:index.php。
我不知道该怎么做。我尝试了以下方法:
NSString *urlstr = [[NSString alloc]
initWithFormat:@"http://www.mediafire.com/edit/l8lba665t24h3dg/index.php"];
NSURL *url = [[NSURL alloc] initWithString:urlstr];
NSString *ans = [NSString stringWithContentsOfURL:url
encoding:NSASCIIStringEncoding
error:nil];
NSLog(@"ans--------->%@",ans); // This returns the whole page :(
[urlstr release];
[url release];
这是正确的方法吗?那么如何获得返回字符串呢?我只需要返回字符串为:
"Hello, PHP!"
(即php输出)。拜托,请帮帮我...
最佳答案
是的,这是正确的方法,您在NSLog上获得的结果与严格单击页面并显示源代码的结果相同。
您的url index.php为纯文本格式,未由服务器解释,这就是为什么您没有“Hello,PHP!”的原因。结果是。
参见php hello world教程:http://www.utexas.edu/learn/php/example1.shtml
结果:http://www.utexas.edu/learn/php/ex1.php
更改您的代码:
NSString *urlstr = [[NSString alloc]
initWithFormat:@"http://www.utexas.edu/learn/php/ex1.php"];
NSURL *url = [[NSURL alloc] initWithString:urlstr];
关于php - PHP页面返回垃圾,而不是Objective-C中的返回字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20711850/