使用基于PHP XML的Web API的iOS应用程序

问题描述:

I am fairly new to iOS development and trying make a simple app which will communicate with PHP API. Data request and response will be in XML format only..

this is my php method for login,

function login()
{
    $mainframe = JFactory::getApplication();

    $xmlcnt = array();

    if (!isset($HTTP_RAW_POST_DATA))
    {
        $HTTP_RAW_POST_DATA = file_get_contents("php://input");
    }

    if(empty($HTTP_RAW_POST_DATA))
    {
        $xmlcnt['code'] = "2";
        return $xmlcnt;
    }

    $doc = new DOMDocument();

    $doc->loadXML( $HTTP_RAW_POST_DATA);

    $login_data = $doc->getElementsByTagName( "data" );

    foreach( $login_data as $login )
    {
        $user_nm = $login->getElementsByTagName( "username" );
        $user_nm = $user_nm->item(0)->nodeValue;

        $password = $login->getElementsByTagName( "password" );
        $password = $password->item(0)->nodeValue;

    } ......

and the xmldata look like this "<data><username>testuser</username><password>password</password></data>"

i want to understand how/what should i use in xcode objective-c to send and recieve XML efficiently.

Thank you verymuch

我对iOS开发很新,并尝试制作一个与PHP API通信的简单应用程序。 数据请求和响应只能是XML格式。 p>

这是我登录的php方法, p>

  function login()\  n {
 $ mainframe = JFactory :: getApplication(); 
 
 $ xmlcnt = array(); 
 
 if if(!isset($ HTTP_RAW_POST_DATA))
 {
 $ HTTP_RAW_POST_DATA = file_get_contents(“php  ://输入“); 
} 
 
 if(empty($ HTTP_RAW_POST_DATA))
 {
 $ xmlcnt ['code'] =”2“; 
返回$ xmlcnt; 
} 
  
 $ doc = new DOMDocument(); 
 
 $ doc-&gt; loadXML($ HTTP_RAW_POST_DATA); 
 
 $ login_data = $ doc-&gt; getElementsByTagName(“data”); 
 
 foreach(  $ login_data as $ login)
 {
 $ user_nm = $ login-&gt; getElementsByTagName(“username”); 
 $ user_nm = $ user_nm-&gt; item(0) - &gt; nodeValue; 
 
 $  password = $ login-&gt; getElementsByTagName(“password”); 
 $ password = $ password-&gt; item(0) - &gt; nodeValue; 
 
} ...... 
  code>   pre> 
 
 

并且xmldata看起来像这样“&lt; data&gt;&lt; username&gt; testuser&lt; / usernam e&gt;&lt;密码&gt;密码&lt; /密码&gt;&lt; / data&gt; code>“ p>

我想了解我应该如何/在xcode objective-c中使用什么来发送和 有效地接收XML。 p>

非常感谢 p> div>

you can check an XML parser for that, here some examples: http://cocoawithlove.com/2011/05/classes-for-fetching-and-parsing-xml-or.html

If you are asking which xml parser to use, Writing your own parser should help a lot in performance. We usually add data compression to keep size of data transfer low. We use simple zip library and also do encryption is needed. Zipping will help a lot if the data size is large