简体   繁体   English

将iOS字符串发送到PHP

[英]Sending iOS string to PHP

I am trying to make an iPhone app send a string to my web server. 我正在尝试使iPhone应用程序向我的Web服务器发送字符串。 At the moment when I print out $_POST in my php, the array is empty. 当我在PHP中打印出$ _POST时,该数组为空。

This is what I am currently trying; 这是我目前正在尝试的;

    //NSString *query = @"SELECT name FROM users";
    NSString *query = @"select=name&from=users";

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://localhost/www/service.php"]];
    [request setHTTPMethod:@"POST"];
    [request setHTTPBody:[query dataUsingEncoding:NSUTF8StringEncoding]];

    [NSURLConnection connectionWithRequest:request delegate:self];

In the php script I simply do print_r($_POST); 在php脚本中,我只是执行print_r($ _ POST); I get no error messages, only an empty array. 我没有收到错误消息,只有一个空数组。 What am I doing wrong? 我究竟做错了什么? I have tried accessing the two fields separately and it does't work either ($POST['select'];) 我尝试过分别访问这两个字段,但也不起作用($ POST ['select'];)

Try using $_REQUEST . 尝试使用$_REQUEST May be $_POST not works. 可能是$_POST不起作用。

When you say that print_r prints out an empty array, do you mean that it prints it out an empty array in the browser? 当您说print_r打印出一个空数组时,是否表示它在浏览器中打印出一个空数组? If that is the case, don't be concerned - it is not meant to print anything. 如果是这种情况,请不要担心-这并不意味着要打印任何内容。 I was working on something similar and realized that I was debugging it the wrong way. 我正在做类似的事情,并且意识到我在错误地调试它。

When you run your simulator in xcode, your php script will get called and $_POST will have a value. 当您在xcode中运行模拟器时,您的php脚本将被调用,$ _ POST将具有一个值。 If you print_r at that moment it will temporarily hold the value of your two fields (but there is no way to see this on your browser). 如果此时使用print_r,它将暂时保存您两个字段的值(但是无法在浏览器中看到它)。 If you add some NSLogs and print out the response in xcode, you will see that the array is NOT empty. 如果添加一些NSLogs并用xcode打印出响应,您将看到该数组不是空的。

What I am trying to say is - the only way you should be debugging this should be in xcode, do not look at your browser. 我想说的是-调试的唯一方法应该是xcode,而不是浏览器。

Try this to see your response in the log (this was previously posted - see the link in your first comment); 尝试此操作以在日志中查看您的回复(此信息先前已发布-请参阅第一条评论中的链接);

    NSError *err;
    NSURLResponse *response;

    NSData *responseData = [NSURLConnection sendSynchronousRequest:request      returningResponse:&response error:&err];

    NSString *resSrt = [[NSString alloc]initWithData:responseData encoding:NSASCIIStringEncoding];

    //This is for Response
    NSLog(@"got response==%@", resSrt);

    if(resSrt)
    {
        NSLog(@"got response");
    }
    else
    {
        NSLog(@"faield to connect");
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM