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条)

报告相同问题?

悬赏问题

  • ¥15 matlab有关常微分方程的问题求解决
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable