如何在php webservice中传递参数?

Posted

技术标签:

【中文标题】如何在php webservice中传递参数?【英文标题】:How to pass parameter in php webservice? 【发布时间】:2013-07-10 09:39:02 【问题描述】:

我正在一个应用程序中工作,我需要在请求字符串的该参数中传递json 对象,现在我被困在这里,不知道该怎么做。

 SBJSON *json = [SBJSON new];
 json.humanReadable = YES;
 responseData = [NSMutableData data] ;

NSString *service = @"http://localhost.abc.com/index.php?p=api/user/register";

NSString *requestString = [NSString stringWithFormat:@"\"Name\":\"%@\",\"Email\":\"%@\",\"Password\":\"%@\",\"PasswordMatch\":\"%@\",\"TermsOfUSe\":\"1\"",txtusername.text,txtemail.text,txtpassword.text,txtretypepassword.text];


NSData *requestData = [NSData dataWithBytes: [requestString UTF8String] length: [requestString length]];

NSString *urlLoc=@"";
urlLoc = [urlLoc stringByAppendingString:service];

NSLog(@"URL:- %@",urlLoc);


NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:
                                [NSURL URLWithString: urlLoc]];
NSString *postLength = [NSString stringWithFormat:@"%d", [requestData length]];
[request setHTTPMethod: @"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:requestData];

NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self];
NSLog(@"%@",request);

【问题讨论】:

你能展示一些你已经尝试过的代码吗,你是把它作为 POST 或 GET 的一部分发送吗?您是否已经创建了 JSON 或什么。我们需要更多细节来提供帮助 ***.com/questions/4456966/… 我在上述方法中做错了什么 确保您可以通过实现连接委托方法来连接您的网络服务器。 请给我一个例子 【参考方案1】:
SBJSON *json = [SBJSON new];
 json.humanReadable = YES;
 responseData = [NSMutableData data] ;

NSString *service = @"http://localhost.abc.com/index.php?p=api/user/register";

NSString *requestString = [NSString stringWithFormat:@"\"Name\":\"%@\",\"Email\":\"%@\",\"Password\":\"%@\",\"PasswordMatch\":\"%@\",\"TermsOfUSe\":\"1\"",txtusername.text,txtemail.text,txtpassword.text,txtretypepassword.text];


NSData *requestData = [NSData dataWithBytes: [requestString UTF8String] length: [requestString length]];

NSString *urlLoc=@"";
urlLoc = [urlLoc stringByAppendingString:service];

NSLog(@"URL:- %@",urlLoc);


NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:
                                [NSURL URLWithString: urlLoc]];
NSString *postLength = [NSString stringWithFormat:@"%d", [requestData length]];
[request setHTTPMethod: @"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:requestData];

NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self];
NSLog(@"%@",request);

连接的委托方法

 - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 
    [responseData setLength:0];


- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
    [responseData appendData:data];
  //**check here for responseData & also data**


- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error 
  NSLog([NSString stringWithFormat:@"Connection failed: %@", [error description]]);


- (void)connectionDidFinishLoading:(NSURLConnection *)connection 
    [connection release];
  //do something with the json that comes back ... (the fun part)

/ /// / ///////// 已编辑答案 //////////////////

GET 方法:在此方法中,您可以将请求数据附加到 Web 服务后面。正如您现在通过行 [request setHTTPMethod: @"POST"]; 所做的那样。

POST 方法:在此方法中,您不能附加请求的数据。但是将字典作为参数传递。如下:

NSArray *objects = [NSArray arrayWithObjects:[[NSUserDefaults standardUserDefaults]valueForKey:@"StoreNickName"],
  [[UIDevice currentDevice] uniqueIdentifier], [dict objectForKey:@"user_question"],     nil];
NSArray *keys = [NSArray arrayWithObjects:@"nick_name", @"UDID", @"user_question", nil];
NSDictionary *questionDict = [NSDictionary dictionaryWithObjects:objects forKeys:keys];

NSDictionary *jsonDict = [NSDictionary dictionaryWithObject:questionDict forKey:@"question"];

NSString *jsonRequest = [jsonDict JSONRepresentation];

NSLog(@"jsonRequest is %@", jsonRequest);

NSURL *url = [NSURL URLWithString:@"https://xxxxxxx.com/questions"];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
             cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];


NSData *requestData = [NSData dataWithBytes:[jsonRequest UTF8String] length:[jsonRequest length]];

[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:[NSString stringWithFormat:@"%d", [requestData length]] forHTTPHeaderField:@"Content-Length"];
[request setHTTPBody: requestData];

NSURLConnection *connection = [[NSURLConnection alloc]initWithRequest:request delegate:self];

【讨论】:

当我发出 GET 请求时,它给了我正确的响应,但是当我尝试发布时没有问题,但它给了我一个空数组而不是正确的值 是的,当我传递登录数据时它没有给我需要输出,在发出请求传递 json 字符串时我需要做些什么 查看此链接:[raywenderlich.com/2965/… 也试试这个 [***.com/questions/13359314/…. 这不会我已经使用 ASI HTTP 请求在 PHP Web 服务中传递 json 对象

以上是关于如何在php webservice中传递参数?的主要内容,如果未能解决你的问题,请参考以下文章

如何向Webservice里传递类参数

如何在调用WebService方法时,传递对象数组参数

Perl WebService::Soundcloud - 如何在上传到 Soundcloud 时传递轨道参数

ajax调用c# webservice 如何传递实体参数

Asp.net WebService 接口中在传递参数过程中,参数是不是有长度限制?

java Web工程,如何在不传参数的情况下,判断WebService接口是不是开放