iPhone:将数据发布到php页面 [英] iPhone: Post data to php page

查看:133
本文介绍了iPhone:将数据发布到php页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尽管看到这个网站和谷歌的类似的帖子,我只是不能包装我的头如何发布到iPhone的php页面。这里是我想做的:

Despite looking at similar posts on this site and google, I just can't wrap my head around how to post to a php page from the iPhone. Here is what I want to do:

我有一个php脚本,在www.mypage.com/myscript.php说,我可以通过做www正常发布。 mypage.com/myscript.php?mynumber=99&myname=codezy

I have a php script, say at www.mypage.com/myscript.php that I could post to normally by doing www.mypage.com/myscript.php?mynumber=99&myname=codezy

这样会依次在数据库中添加日志消息。我不想要任何数据回来,它本质上是一种单向事务。 NSMutableURLRequest似乎是我正在寻找,但我只是不能得到如何使这个工作与一对夫妇的参数的句柄。

This in turn will add a log message in database for example. I do not want any data back, it is essentially a one way transaction. NSMutableURLRequest seems to be what I am looking for but I just cant get a handle on how to make this work with a couple parameters.

推荐答案

p>如果您要发送单个网址请求,而不需要发送多个变体,则 NSURLRequest 将执行。即使您使用多个参数,它们都是同一网址的一部分,因此您只需按照这种方式对待。以字符串形式构建URL,然后使用该字符串初始化 NSURL 对象。

If you're sending a single URL request without needing to send multiple variations, NSURLRequest will do. Even though you are using multiple parameters, they are all part of the same URL, so you just treat them that way. Build the URL as a string first and then use the string to initialize a NSURL object.

日志消息在服务器上,但你会想要有响应数据,以防万一出现问题。您可以忽略响应数据,除非有错误。使用 NSURLConnection 对象发送请求。

You are prompting a log message on the server, but you will want to have response data in case something goes wrong. You can just ignore the response data unless there's an error. The request is sent using a NSURLConnection object.

NSURL *urlToSend = [[NSURL alloc] initWithString: @"www.mypage.com/myscript.php?mynumber=99&myname=codezy"];

NSURLRequest *urlRequest = [NSURLRequest requestWithURL:urlToSend   
                                            cachePolicy:NSURLRequestReturnCacheDataElseLoad                                               
                                   cachetimeoutInterval:30];

NSData *urlData;
NSURLResponse *response;
NSError *error;
urlData = [NSURLConnection sendSynchronousRequest:urlRequest  
                                returningResponse:&response 
                                            error:&error];

这篇关于iPhone:将数据发布到php页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆