dtgj8529 2013-12-20 20:59
浏览 98
已采纳

PHP页面在objective-C中返回垃圾而不是返回字符串

I'm building an iOS app using Xcode. In that, I need to get a value from an online php page: index.php.

I don't know how to do that. I've tried the following:

    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];

Is it is the right way to do this? Then how to get the return string? I need the return string only as: "Hello, PHP!" (ie., the php output). Please, please help me...

  • 写回答

2条回答 默认 最新

  • dselp3944 2013-12-20 21:49
    关注

    yes it's the right way, you have the same result on your NSLog as rigth click on your page and show source code. Your url index.php is in plain text and not interpreted by the server, is why you haven't "Hello, PHP!" as a result.

    see php hello world tutorial : http://www.utexas.edu/learn/php/example1.shtml

    the result: http://www.utexas.edu/learn/php/ex1.php

    change your code :

    NSString *urlstr = [[NSString alloc] 
          initWithFormat:@"http://www.utexas.edu/learn/php/ex1.php"];
    NSURL *url = [[NSURL alloc] initWithString:urlstr];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大