php - PHP页面返回垃圾,而不是Objective-C中的返回字符串

标签 php ios objective-c

我正在使用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/

相关文章:

objective-c - 如何获取从 Nib 加载的 UIView 的多个副本?

php - RegEx 用于匹配 CSS 文件中的特定 javaDoc 属性,并提取有关 css 定义的数据

ios - 从中心点折叠 UIImageView

objective-c - NSDictionary 到 UITableView

objective-c - 从 subview 中更改 View

objective-c - UIButton 在 CABasicAnimaton 之后在错误的位置接收到 touchEvents

php - 无法获取mysql数据(CodeIgniter选择)

php - 从 MySql 表中选择信息,然后将其存储到变量中

c# - 用于搜索日期的 SQL 查询

iphone - 运行模拟器时出错 : duplicate symbol for architecture i386