Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/262.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将变量从iOS发送到php文件_Php_Ios_Nsurl - Fatal编程技术网

如何将变量从iOS发送到php文件

如何将变量从iOS发送到php文件,php,ios,nsurl,Php,Ios,Nsurl,我有一个简单的任务,通过GET向php页面发送一个变量。我似乎找不到任何有用的东西,所有的东西似乎都超出了我的需要 似乎我需要代码来设置NSURL字符串、NSURL请求,然后执行 有人能给我粘贴一些简单的代码来执行如下URL: http://localhost/trendypieces/site/ios/processLatest.php?caption=yosa 谢谢 这里是一些不起作用的东西的最新迭代,看起来更接近,因为它实际上抛出了错误警报。不知道那个错误是什么……但是 //constr

我有一个简单的任务,通过GET向php页面发送一个变量。我似乎找不到任何有用的东西,所有的东西似乎都超出了我的需要

似乎我需要代码来设置NSURL字符串、NSURL请求,然后执行

有人能给我粘贴一些简单的代码来执行如下URL:

http://localhost/trendypieces/site/ios/processLatest.php?caption=yosa
谢谢

这里是一些不起作用的东西的最新迭代,看起来更接近,因为它实际上抛出了错误警报。不知道那个错误是什么……但是

//construct an URL for your script, containing the encoded text for parameter value
    NSURL* url = [NSURL URLWithString:
                  [NSString stringWithFormat:
                   @"http://localhost/trendypieces/site/ios/processLatest.php?caption=yosa"]];

    NSData *dataURL =  [NSData dataWithContentsOfURL:url];
    NSString *serverOutput = [[NSString alloc] initWithData:dataURL encoding: NSASCIIStringEncoding];

    if([serverOutput isEqualToString:@"OK"]) {

        alertsuccess = [[UIAlertView alloc] initWithTitle:@"Posted" message:@"Done"
                                                 delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];

    } else {
        alertsuccess = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Done"
                                                 delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];

    }
    [alertsuccess show];
有两点:

  • 使用
    initWithRequest:delegate:
    创建
    NSURLConnection
    时,它会自动启动连接。不要自己调用
    start
    方法(在某些情况下,它可能会干扰初始连接)。这仅适用于将
    initWithRequest:delegate:startImmediately:
    NO
    一起用于该最终参数的情况

  • 然后你说:

    不产生活动结果的当前代码(来自iAction函数内)

    您的代码不会在
    iAction
    方法中产生任何“活动结果”。它将调用
    nsurconnectiondatadelegate
    nsurconnectiondelegate
    方法。你实施了吗?值得注意的是,请确保还实现了
    连接:didFailWithError:
    ,它将告诉您是否存在任何连接错误

    如果在
    iAction
    方法中需要结果,则应使用
    NSURLConnection
    方法
    sendsynchronousrequest

  • 转到这个问题的标题“如何发送变量”,您应该注意向URL添加用户输入。(这不是您当前无法获得任何回复的问题,但在将变量的内容发送到web服务器时,这一点很重要。)

    值得注意的是,
    caption=xxx
    部分,
    xxx
    不能包含空格或保留字符,如
    +
    &
    等。您需要做的是对其进行百分比编码。因此,你应该:

    NSString *caption = ... // right now this is @"yosa", but presumably this will eventually be some variable
    
    NSMutableData *data = [[NSMutableData alloc] init];
    self.receivedData = data;
    // [data release];  // if not ARC, insert this line
    
    //initialize url that is going to be fetched.
    NSString *encodedCaption = [self percentEscapeString:caption];
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://localhost/trendypieces/site/ios/processLatest.php?caption=%@", encodedCaption]];
    
    //initialize a request from url
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    
    //initialize a connection from request
    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    self.connection = connection;
    // [connection release]; // if not ARC, insert this line
    
    // DO NOT start the connection AGAIN
    //[connection start];
    
    其中,
    percentEscapeString
    定义为:

    - (NSString *)percentEscapeString:(NSString *)string
    {
        NSString *result = CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
                                                                                     (CFStringRef)string,
                                                                                     (CFStringRef)@" ",
                                                                                     (CFStringRef)@":/?@!$&'()*+,;=",
                                                                                     kCFStringEncodingUTF8));
        return [result stringByReplacingOccurrencesOfString:@" " withString:@"+"];
    }
    
    (注意,有一种很有前途的
    NSString
    方法,
    stringByAddingPercentEscapesUsingEncoding
    ,它做了一些非常类似的事情,但不想使用它。它处理一些字符(例如空格字符),但不处理其他一些字符(例如
    +
    &
    字符)

  • 最后,您说这是一个
    GET
    请求(这意味着您没有更改服务器上的任何内容)。如果它确实是一个
    GET
    请求,请参阅我前面的观点。但是如果这个请求确实是在更新数据,那么您应该执行一个
    POST
    请求(其中
    caption=yosa
    位于请求主体中,而不是URL)。这还有另一个优点,因为URL的长度存在限制(因此,当您在
    GET
    请求中在URL中提交参数时,参数可以有多长)

    无论如何,如果您想创建一个
    POST
    请求,它将如下所示:

    NSString *caption = ... // right now this is @"yosa", but presumably this will eventually be some variable
    
    NSMutableData *data = [[NSMutableData alloc] init];
    self.receivedData = data;
    // [data release];  // if not ARC, insert this line
    
    //create body of the request
    NSString *encodedCaption = [self percentEscapeString:caption];
    NSString *postString = [NSString stringWithFormat:@"caption=%@", encodedCaption];
    NSData *postBody = [postString dataUsingEncoding:NSUTF8StringEncoding];
    
    //initialize url that is going to be fetched.
    NSURL *url = [NSURL URLWithString:@"http://localhost/trendypieces/site/ios/processLatest.php"];
    
    //initialize a request from url
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [request setHTTPBody:postBody];
    [request setHTTPMethod:@"POST"];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    
    //initialize a connection from request
    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    self.connection = connection;
    // [connection release]; // if not ARC, insert this line
    
  • 当您的初始代码示例使用基于代理的
    NSURLConnection
    时,您已将答案修改为使用
    dataWithContentsOfURL
    。如果您确实不想使用基于委托的
    NSURLConnection
    ,那么可以使用它的
    sendAsynchronousRequest
    ,它提供了
    数据与URL
    内容的简单性,但允许您使用
    GET
    POST
    请求,以及异步执行它。因此,如上所示创建
    NSMutableURLRequest
    (根据您是
    GET
    还是
    POST
    ,使用适当的方法代码),删除实例化
    NSMutableData
    NSURLConnection
    的行,并将其替换为:

    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
    
        if (!data) {
            NSLog(@"Error = %@", connectionError);
    
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"Error" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [alert show];
    
            return;
        }
    
        NSString *serverOutput = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
    
        NSLog(@"Data = %@", serverOutput);
    
        UIAlertView *alert;
    
        if ([serverOutput isEqualToString:@"OK"]) {
            alert = [[UIAlertView alloc] initWithTitle:nil message:@"Posted" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        } else {
            alert = [[UIAlertView alloc] initWithTitle:nil message:@"Not OK" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        }
    
        [alert show];
    }];
    

  • 所以主要的问题是试图通过电话而不是模拟器来访问本地主机,duh。下面是现在成功运行的代码。谢谢大家的帮助

    NSURL *url = [NSURL URLWithString:@"http://localhost/trendypieces/site/ios/processLatest.php?caption=mycaption"];
    
        NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
        [request setURL:url];
        [request setHTTPMethod:@"GET"];
    
        returnData = [[NSMutableData alloc] init];
        NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];
    

    在这里,它适用于带有两个变量的POST,这要感谢@Rob用于另一篇POST的代码

    NSURL *url = [NSURL URLWithString:@"http://192.168.1.5/trendypieces/site/ios/processLatest.php"];
    
        NSString *var2 = @"variable2";
        NSString *postString    = [NSString stringWithFormat:@"caption=%@&var2=%@", _captionField.text, var2];
        NSData   *postBody      = [postString dataUsingEncoding:NSUTF8StringEncoding];
    
        //initialize a request from url
        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
        [request setHTTPBody:postBody];
        [request setHTTPMethod:@"POST"];
        [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    
        //initialize a connection from request, any way you want to, e.g.
        NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    

    如果你什么都不懂,就看一看并问一些具体的问题。仅仅要求代码是违反网站精神的。可能重复的确定,然后,添加代码。在过去的10个小时里,我尝试了很多次这段代码的迭代,我不确定它是否有用。对于有经验的人来说,这似乎是一个非常基本的问题。很抱歉冒犯了你。没有人会被冒犯,只是在SO的正常做法是标记类似的问题:)@Dominikhdl该帖子是一个非常不同的主题:这是因为有人试图使用URL中的参数执行
    post
    请求(这是错误的,应该在正文中)。但是这个问题是关于向
    GET
    请求添加参数的,如果他真的想做
    GET
    ,那么它必须在URL中,而不是正文中,与另一个问题所建议的情况正好相反。我怀疑这里的问题可能是他开始连接了两次,或者别的什么。但不是因为他没有把参数放在身体里。谢谢。我已经尝试了所有这些,很多错误。不管是POST还是GET,只想向页面发送一个变量。插入php页面无论我点击POST还是GET,都能正常工作。当我尝试那里的最后一个代码POST方法时,我得到以下错误:self.receivedData=data;…'在AppViewController“[数据发布];…”上找不到道具ARC禁止发布“NSString*encodedCaption=[self percentEscapeString:caption];…的显式消息发送。。获取“APPViewController”的“无可见@接口”声明选择器self.connection=connection;…”找不到道具“[连接释放];。。。several@harp我不理解您提供的错误,因为您的代码示例会生成相同的错误。例如,您的代码示例