Php iphone post请求头

Php iphone post请求头,php,iphone,json,zend-framework,Php,Iphone,Json,Zend Framework,我将开发基于RESTAPI的web服务。 这是我的第一个web服务项目 我有一个关于网络服务iPhone应用程序的问题 假设当用户使用iPhone应用程序登录时,其将获得身份验证令牌 使用此令牌,他/她将被授权使用应用程序的进一步操作。 我正在计划,iPhone应用程序需要发送听者,以及带有herders认证密钥的post/get请求方法 请求看起来像: www.mydomaoin.com 但要在服务器上授权,我需要将特殊参数传递到请求的标头: Headers: X-Testing-Auth-S

我将开发基于RESTAPI的web服务。 这是我的第一个web服务项目

我有一个关于网络服务iPhone应用程序的问题

假设当用户使用iPhone应用程序登录时,其将获得身份验证令牌

使用此令牌,他/她将被授权使用应用程序的进一步操作。 我正在计划,iPhone应用程序需要发送听者,以及带有herders认证密钥的post/get请求方法

请求看起来像: www.mydomaoin.com

但要在服务器上授权,我需要将特殊参数传递到请求的标头:

Headers: X-Testing-Auth-Secret: kI7wGju76kjhJHGklk76
Headers: Content-Type : application/json
将在控制器(zend框架)中使用

我不知道iPhone是否能够发送标题。 就像android一样

我是一名php开发人员


请帮助我

这是您如何在“Post”请求中附加令牌的方法。示例代码:

NSURL *url = [NSURL URLWithString:@"http://www.deveinvoice.sirus/process/user.asmx"];
req = [NSMutableURLRequest requestWithURL:url];

//---set the headers---

NSString *msgLength = [NSString stringWithFormat:@"%d",[strSoapMsg length]];
[req addValue:@"application/soap+xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[req addValue:@"http://tempuri.org/CheckTransactionIDExist" forHTTPHeaderField:@"SOAPAction"];
[req addValue:msgLength forHTTPHeaderField:@"Content-Length"];

//---set the HTTP method and body---

[req setHTTPMethod:@"POST"];
[req setHTTPBody: [strSoapMsg dataUsingEncoding:NSUTF8StringEncoding]];

这就是如何在“Post”请求中附加令牌的方法。示例代码:

NSURL *url = [NSURL URLWithString:@"http://www.deveinvoice.sirus/process/user.asmx"];
req = [NSMutableURLRequest requestWithURL:url];

//---set the headers---

NSString *msgLength = [NSString stringWithFormat:@"%d",[strSoapMsg length]];
[req addValue:@"application/soap+xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[req addValue:@"http://tempuri.org/CheckTransactionIDExist" forHTTPHeaderField:@"SOAPAction"];
[req addValue:msgLength forHTTPHeaderField:@"Content-Length"];

//---set the HTTP method and body---

[req setHTTPMethod:@"POST"];
[req setHTTPBody: [strSoapMsg dataUsingEncoding:NSUTF8StringEncoding]];

您可以使用以下代码

NSMutableURLRequest *req = [[NSMutableURLRequest alloc]initWithURL:[NSURL URLWithString:url]];
    NSString *post = @"your json";
    //NSLog(@"%@",post);
    NSData *postdata = [NSData dataWithBytes:[post UTF8String] length:[post length] ];
    [req setHTTPMethod:@"POST"];
    [req setValue:@"application/json" forHTTPHeaderField:@"content-type"];
    [req setValue:@"kI7wGju76kjhJHGklk76" forHTTPHeaderField:@"Content-Length"];
    [req setValue:[NSString stringWithFormat:@"%d",[postdata length]] forHTTPHeaderField:@"X-Testing-Auth-Secret"];
    [req setHTTPBody:postdata];

    (void)[NSURLConnection connectionWithRequest:req delegate:self];

您可以使用以下代码

NSMutableURLRequest *req = [[NSMutableURLRequest alloc]initWithURL:[NSURL URLWithString:url]];
    NSString *post = @"your json";
    //NSLog(@"%@",post);
    NSData *postdata = [NSData dataWithBytes:[post UTF8String] length:[post length] ];
    [req setHTTPMethod:@"POST"];
    [req setValue:@"application/json" forHTTPHeaderField:@"content-type"];
    [req setValue:@"kI7wGju76kjhJHGklk76" forHTTPHeaderField:@"Content-Length"];
    [req setValue:[NSString stringWithFormat:@"%d",[postdata length]] forHTTPHeaderField:@"X-Testing-Auth-Secret"];
    [req setHTTPBody:postdata];

    (void)[NSURLConnection connectionWithRequest:req delegate:self];

请求解析xml数据

假设这是请求的方法-

-(无效)产后{

NSString *soapMsg =
[NSString stringWithFormat:
 @"<?xml version=\"1.0\" encoding=\"utf-8\"?>"
 "<soap:Envelope xmlns:xsi="
 "\"http://www.w3.org/2001/XMLSchema-instance\" "
 "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" "
 "xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
 "<soap:Body>"
 "<PostPipeSeAamAnomaly xmlns=\"http://tempuri.org/\">"
 "<PDAIRID>%d</PDAIRID>n"
 "<AssociatedItemIDILI>%@</AssociatedItemIDILI>n"
 "<AssociatedItemIDOther>%@</AssociatedItemIDOther>n"
 "<RGW>%@</RGW>n"
 "<StartDistance>%@</StartDistance>n"
 "<Length>%@</Length>n"
 "<Width>%@</Width>n"
 "<Orientation1>%@</Orientation1>n"
 "<Orientation2>%@</Orientation2>n"
 "<AnomalyType>%@</AnomalyType>n"
 "<OrientationToWeld>%@</OrientationToWeld>n"
 "<LongestLinearIndication>%@</LongestLinearIndication>n"
 "<LocalWallThickness>%@</LocalWallThickness>n"
 "<AnomalyDepthMinimum>%@</AnomalyDepthMinimum>n"
 "<AnomalyDepthAverage>%@</AnomalyDepthAverage>n"
 "<AnomalyDepthMaximum>%@</AnomalyDepthMaximum>n"
 "<AnomalyDepthSurface>%@</AnomalyDepthSurface>n"
 "<AnomalyDepthMaximumPercentage>AA</AnomalyDepthMaximumPercentage>n"
 "<AnomalyDepthMinimumPercentage>AA</AnomalyDepthMinimumPercentage>n"
 "<SizingMethod>%@</SizingMethod>n"
 "<AnomalyRemoved>%@</AnomalyRemoved>n"
 "<RemedialActionRequired>%@</RemedialActionRequired>n"
 "<RemedialActionIncidental>%@</RemedialActionIncidental>n"
 "<Comment>%@</Comment>n"
 "</PostPipeSeAamAnomaly>n"
 "</soap:Body>"
 "</soap:Envelope>",PDAirID ,[arrPipeSeamAnomaly objectAtIndex:2] ,[arrPipeSeamAnomaly objectAtIndex:3],[arrPipeSeamAnomaly objectAtIndex:4] ,[arrPipeSeamAnomaly objectAtIndex:5] ,[arrPipeSeamAnomaly objectAtIndex:6],[arrPipeSeamAnomaly objectAtIndex:7] ,[arrPipeSeamAnomaly objectAtIndex:8] ,[arrPipeSeamAnomaly objectAtIndex:9],[arrPipeSeamAnomaly objectAtIndex:10],[arrPipeSeamAnomaly objectAtIndex:11] ,[arrPipeSeamAnomaly objectAtIndex:12] ,[arrPipeSeamAnomaly objectAtIndex:13],[arrPipeSeamAnomaly objectAtIndex:2] ,[arrPipeSeamAnomaly objectAtIndex:2],[arrPipeSeamAnomaly objectAtIndex:14] ,[arrPipeSeamAnomaly objectAtIndex:15],[arrPipeSeamAnomaly objectAtIndex:16] ,[arrPipeSeamAnomaly objectAtIndex:17],[arrPipeSeamAnomaly objectAtIndex:18],[arrPipeSeamAnomaly objectAtIndex:19] ,[arrPipeSeamAnomaly objectAtIndex:20]];

//NSLog(@"%@",soapMsg);
NSURL *url = [NSURL URLWithString:
              WEBSERVICEURL];
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];

//---set the various headers---
NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMsg length]];
[req addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[req addValue:@"http://tempuri.org/PostPipeSeAamAnomaly" forHTTPHeaderField:@"SOAPAction"];
[req addValue:msgLength forHTTPHeaderField:@"Content-Length"];

//---set the HTTP method and body---
[req setHTTPMethod:@"POST"];
[req setHTTPBody:[soapMsg dataUsingEncoding:NSUTF8StringEncoding]];

//[activityIndicator startAnimating];


conn = [[NSURLConnection alloc] initWithRequest:req delegate:self];
if (conn) {
    webData = [NSMutableData data];


    //NSLog(@"go to delegateMethods");
}
NSString*soapMsg=
[NSString stringWithFormat:
@""
""
""
""
“%dn”
“%@n”
“%@n”
“%@n”
“%@n”
“%@n”
“%@n”
“%@n”
“%@n”
“%@n”
“%@n”
“%@n”
“%@n”
“%@n”
“%@n”
“%@n”
“%@n”
“安”
“安”
“%@n”
“%@n”
“%@n”
“%@n”
“%@n”
“n”
""
“”,PDAirID,[ArrpipeSeamAbectatindex:2],[ArrpipeSeamAbectatindex:3],[ArrpipeSeamAbectatindex:4],[ArrpipeSeamAbectatindex:5],[ArrpipeSeamAbectatindex:6],[ArrpipeSeamAbectatindex:7],[ArrpipeSeamAbectatindex:8],[ArrpipeSeamAbectatindex:9],[ArrpeSeamAbectatindex:10],[ArrPipeSeam异常对象索引:11],[ArrPipeSeam异常对象索引:12],[ArrPipeSeam异常对象索引:13],[ArrPipeSeam异常对象索引:2],[ArrPipeSeam异常对象索引:2],[ArrPipeSeam异常对象索引:14],[ArrPipeSeam异常对象索引:15],[ArrPipeSeam异常对象索引:16],[ArrPipeSeam异常对象索引:17],[ArrPipeSeamAboration对象索引:18],[ArrPipeSeamAboration对象索引:19],[ArrPipeSeamAboration对象索引:20];
//NSLog(@“%@”,soapMsg);
NSURL*url=[NSURL URLWithString:
WEBSERVICEURL];
NSMutableURLRequest*req=[NSMutableUrlRequestWithURL:url];
//---设置各种标题---
NSString*msgLength=[NSString stringWithFormat:@“%d”,[soapMsg长度]];
[req addValue:@“text/xml;charset=utf-8”用于httpheaderfield:@“Content Type”];
[请求添加值:@”http://tempuri.org/PostPipeSeAamAnomalyforHTTPHeaderField:@“SOAPAction”];
[req addValue:msgLength for HttpHeaderField:@“内容长度”];
//---设置HTTP方法和正文---
[req setHTTPMethod:@“POST”];
[req setHTTPBody:[soapMsg dataUsingEncoding:NSUTF8StringEncoding]];
//[活动指示器启动激活];
conn=[[NSURLConnection alloc]initWithRequest:req委托:self];
如果(康涅狄格州){
webData=[NSMutableData];
//NSLog(@“转到委派方法”);
}

}解析xml数据的请求

假设这是请求的方法-

-(无效)产后{

NSString *soapMsg =
[NSString stringWithFormat:
 @"<?xml version=\"1.0\" encoding=\"utf-8\"?>"
 "<soap:Envelope xmlns:xsi="
 "\"http://www.w3.org/2001/XMLSchema-instance\" "
 "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" "
 "xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
 "<soap:Body>"
 "<PostPipeSeAamAnomaly xmlns=\"http://tempuri.org/\">"
 "<PDAIRID>%d</PDAIRID>n"
 "<AssociatedItemIDILI>%@</AssociatedItemIDILI>n"
 "<AssociatedItemIDOther>%@</AssociatedItemIDOther>n"
 "<RGW>%@</RGW>n"
 "<StartDistance>%@</StartDistance>n"
 "<Length>%@</Length>n"
 "<Width>%@</Width>n"
 "<Orientation1>%@</Orientation1>n"
 "<Orientation2>%@</Orientation2>n"
 "<AnomalyType>%@</AnomalyType>n"
 "<OrientationToWeld>%@</OrientationToWeld>n"
 "<LongestLinearIndication>%@</LongestLinearIndication>n"
 "<LocalWallThickness>%@</LocalWallThickness>n"
 "<AnomalyDepthMinimum>%@</AnomalyDepthMinimum>n"
 "<AnomalyDepthAverage>%@</AnomalyDepthAverage>n"
 "<AnomalyDepthMaximum>%@</AnomalyDepthMaximum>n"
 "<AnomalyDepthSurface>%@</AnomalyDepthSurface>n"
 "<AnomalyDepthMaximumPercentage>AA</AnomalyDepthMaximumPercentage>n"
 "<AnomalyDepthMinimumPercentage>AA</AnomalyDepthMinimumPercentage>n"
 "<SizingMethod>%@</SizingMethod>n"
 "<AnomalyRemoved>%@</AnomalyRemoved>n"
 "<RemedialActionRequired>%@</RemedialActionRequired>n"
 "<RemedialActionIncidental>%@</RemedialActionIncidental>n"
 "<Comment>%@</Comment>n"
 "</PostPipeSeAamAnomaly>n"
 "</soap:Body>"
 "</soap:Envelope>",PDAirID ,[arrPipeSeamAnomaly objectAtIndex:2] ,[arrPipeSeamAnomaly objectAtIndex:3],[arrPipeSeamAnomaly objectAtIndex:4] ,[arrPipeSeamAnomaly objectAtIndex:5] ,[arrPipeSeamAnomaly objectAtIndex:6],[arrPipeSeamAnomaly objectAtIndex:7] ,[arrPipeSeamAnomaly objectAtIndex:8] ,[arrPipeSeamAnomaly objectAtIndex:9],[arrPipeSeamAnomaly objectAtIndex:10],[arrPipeSeamAnomaly objectAtIndex:11] ,[arrPipeSeamAnomaly objectAtIndex:12] ,[arrPipeSeamAnomaly objectAtIndex:13],[arrPipeSeamAnomaly objectAtIndex:2] ,[arrPipeSeamAnomaly objectAtIndex:2],[arrPipeSeamAnomaly objectAtIndex:14] ,[arrPipeSeamAnomaly objectAtIndex:15],[arrPipeSeamAnomaly objectAtIndex:16] ,[arrPipeSeamAnomaly objectAtIndex:17],[arrPipeSeamAnomaly objectAtIndex:18],[arrPipeSeamAnomaly objectAtIndex:19] ,[arrPipeSeamAnomaly objectAtIndex:20]];

//NSLog(@"%@",soapMsg);
NSURL *url = [NSURL URLWithString:
              WEBSERVICEURL];
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];

//---set the various headers---
NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMsg length]];
[req addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[req addValue:@"http://tempuri.org/PostPipeSeAamAnomaly" forHTTPHeaderField:@"SOAPAction"];
[req addValue:msgLength forHTTPHeaderField:@"Content-Length"];

//---set the HTTP method and body---
[req setHTTPMethod:@"POST"];
[req setHTTPBody:[soapMsg dataUsingEncoding:NSUTF8StringEncoding]];

//[activityIndicator startAnimating];


conn = [[NSURLConnection alloc] initWithRequest:req delegate:self];
if (conn) {
    webData = [NSMutableData data];


    //NSLog(@"go to delegateMethods");
}
NSString*soapMsg=
[NSString stringWithFormat:
@""
""
""
""
“%dn”
“%@n”
“%@n”
“%@n”
“%@n”
“%@n”
“%@n”
“%@n”
“%@n”
“%@n”
“%@n”
“%@n”
“%@n”
“%@n”
“%@n”
“%@n”
“%@n”
“安”
“安”
“%@n”
“%@n”
“%@n”
“%@n”
“%@n”
“n”
""
“”,PDAirID,[ArrpipeSeamAbectatindex:2],[ArrpipeSeamAbectatindex:3],[ArrpipeSeamAbectatindex:4],[ArrpipeSeamAbectatindex:5],[ArrpipeSeamAbectatindex:6],[ArrpipeSeamAbectatindex:7],[ArrpipeSeamAbectatindex:8],[ArrpipeSeamAbectatindex:9],[ArrpeSeamAbectatindex:10],[ArrPipeSeam异常对象索引:11],[ArrPipeSeam异常对象索引:12],[ArrPipeSeam异常对象索引:13],[ArrPipeSeam异常对象索引:2],[ArrPipeSeam异常对象索引:2],[ArrPipeSeam异常对象索引:14],[ArrPipeSeam异常对象索引:15],[ArrPipeSeam异常对象索引:16],[ArrPipeSeam异常对象索引:17],[ArrPipeSeamAboration对象索引:18],[ArrPipeSeamAboration对象索引:19],[ArrPipeSeamAboration对象索引:20];
//NSLog(@“%@”,soapMsg);
NSURL*url=[NSURL URLWithString:
WEBSERVICEURL];
NSMutableURLRequest*req=[NSMutableUrlRequestWithURL:url];
//---设置各种标题---
NSString*msgLength=[NSString stringWithFormat:@“%d”,[soapMsg长度]];
[req addValue:@“text/xml;charset=utf-8”用于httpheaderfield:@“Content Type”];
[请求添加值:@”http://tempuri.org/PostPipeSeAamAnomalyforHTTPHeaderField:@“SOAPAction”];
[req addValue:msgLength for HttpHeaderField:@“内容长度”];
//---设置HTTP方法和正文---
[req setHTTPMethod:@“POST”];
[req setHTTPBody:[soapMsg dataUsingEncoding:NSUTF8StringEncoding]];
//[活动指示器启动激活];
conn=[[NSURLConnection alloc]initWithRequest:req委托:self];
如果(康涅狄格州){
webData=[NSMutableData];
//NSLog(@“转到委派方法”);
}

}

您可以使用一些委托方法

-(void) parser:(NSXMLParser *) parser
didStartElement:(NSString *) elementName``
  namespaceURI:(NSString *) namespaceURI
 qualifiedName:(NSString *) qName
   attributes:(NSDictionary *) attributeDict {

    if( [elementName isEqualToString:@"AddGeneralInformationResult"])
   {
    if (!soapResults)
    {
        soapResults = [[NSMutableString alloc] init];
    }
    elementFound = YES;
   }


    }

  -(void)parser:(NSXMLParser *) parser foundCharacters:(NSString *)string
   {
if (elementFound)
{
    [soapResults appendString: string];
}
}

-(void)parser:(NSXMLParser *)parser
 didEndElement:(NSString *)elementName
  namespaceURI:(NSString *)namespaceURI
  qualifiedName:(NSString *)qName
 {

 if ([elementName isEqualToString:@"AddGeneralInformationResult"])
 {
    elementFound = NO;
    //NSLog(@"AddGeneralInformationResult %@",soapResults);
    if([soapResults isEqualToString:@"true"])
    {


  [objDB updateDatabaseTable:@"GeneralInformation":@"Yes":
    [[arrGenralInfo          objectAtIndex:0] intValue]];
    }
    [soapResults setString:@""];
    elementFound = FALSE;
   }


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

//NSLog(@"receive response");
  }  

 -(void) connection:(NSURLConnection *) connection
 didReceiveData:(NSData *) data {
  [webData appendData:data];
  }

    -(void) connection:(NSURLConnection *) connection
       didFailWithError:(NSError *) error {

    //NSLog(@"Server error");

    }

  -(void) connectionDidFinishLoading:(NSURLConnection *) connection {
     //NSLog(@"DONE. Received Bytes: %d", [webData length]);
      NSString *theXML = [[NSString alloc]
                    initWithBytes: [webData mutableBytes]
                    length:[webData length]
                    encoding:NSUTF8StringEncoding];
//---shows the XML---
//NSLog(@"%@",theXML);

// [activityIndicator stopAnimating];
if (xmlParser)
{

}
xmlParser = [[NSXMLParser alloc] initWithData: webData];
[xmlParser setDelegate: self];
[xmlParser setShouldResolveExternalEntities:YES];
[xmlParser parse];

}

您可以使用一些委托方法

-(void) parser:(NSXMLParser *) parser
didStartElement:(NSString *) elementName``
  namespaceURI:(NSString *) namespaceURI
 qualifiedName:(NSString *) qName
   attributes:(NSDictionary *) attributeDict {

    if( [elementName isEqualToString:@"AddGeneralInformationResult"])
   {
    if (!soapResults)
    {
        soapResults = [[NSMutableString alloc] init];
    }
    elementFound = YES;
   }


    }

  -(void)parser:(NSXMLParser *) parser foundCharacters:(NSString *)string
   {
if (elementFound)
{
    [soapResults appendString: string];
}
}

-(void)parser:(NSXMLParser *)parser
 didEndElement:(NSString *)elementName
  namespaceURI:(NSString *)namespaceURI
  qualifiedName:(NSString *)qName
 {

 if ([elementName isEqualToString:@"AddGeneralInformationResult"])
 {
    elementFound = NO;
    //NSLog(@"AddGeneralInformationResult %@",soapResults);
    if([soapResults isEqualToString:@"true"])
    {


  [objDB updateDatabaseTable:@"GeneralInformation":@"Yes":
    [[arrGenralInfo          objectAtIndex:0] intValue]];
    }
    [soapResults setString:@""];
    elementFound = FALSE;
   }


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

//NSLog(@"receive response");
  }  

 -(void) connection:(NSURLConnection *) connection
 didReceiveData:(NSData *) data {
  [webData appendData:data];
  }

    -(void) connection:(NSURLConnection *) connection
       didFailWithError:(NSError *) error {

    //NSLog(@"Server error");

    }

  -(void) connectionDidFinishLoading:(NSURLConnection *) connection {
     //NSLog(@"DONE. Received Bytes: %d", [webData length]);
      NSString *theXML = [[NSString alloc]
                    initWithBytes: [webData mutableBytes]
                    length:[webData length]
                    encoding:NSUTF8StringEncoding];
//---shows the XML---
//NSLog(@"%@",theXML);

// [activityIndicator stopAnimating];
if (xmlParser)
{

}
xmlParser = [[NSXMLParser alloc] initWithData: webData];
[xmlParser setDelegate: self];
[xmlParser setShouldResolveExternalEntities:YES];
[xmlParser parse];

}

是的,你可以从iPhone和Android手机发送头文件

是的,你可以从iPhone和Android手机发送头文件

在Android header中可以传递头文件参数,所以iPhone肯定应该能够发这篇文章,如何在Android header参数中使用iPhone传递头文件可以传递,所以iPhone肯定应该是能够看到这篇文章,如何使用iPhone传递标题Hi@Rushi感谢您的回复。我只想知道iPhone和android能够随GET/post请求发送标题。是的。您可以在iPhone和android中随GET/post方法发送标题。Hi@Rushi感谢您的回复。我只想知道iPhone和android是能够随GET/POST请求一起发送标题。是。您可以随发送标题