简体   繁体   English

如何从PHP Web服务到iPhone应用程序获取许多变量?

[英]How to get many vars from php webservice to iPhone app?

I'm linking my app with a MySQL database via a PHP script that I'm using to send and get data from my iPhone app. 我正在通过PHP脚本将应用程序与MySQL数据库链接,该脚本用于从iPhone应用程序发送和获取数据。

I'm able to get data if it's only one variable, via the response string from PHP, but what do you do if you want to get many element from a table in your database, for example? 我可以通过PHP的响应字符串获取数据(如果它只是一个变量),但是,例如,如果要从数据库表中获取许多元素,该怎么办?

您可以使用自己确定的自定义格式,也可以使用主要的运输方式之一:JSON,XML等。

Getting all the variables you need from your PHP script and encoding them into JSON is going to be the best solution to bring it into your iPhone application efficiently. 从PHP脚本中获取所需的所有变量并将其编码为JSON,将是将其有效地引入iPhone应用程序的最佳解决方案。 In PHP you'll create an array of elements like so (all the variables you want your app to have access to): 在PHP中,您将创建一个像这样的元素数组(您希望您的应用有权访问的所有变量):

$array = array('var1' => 'value1', 'var2' => 'value2', 'var3' => 'value3');

Then we'll translate the array into JSON: 然后,我们将数组转换为JSON:

$json = json_encode($array);

Which we can then return that $json variable via your PHP script. 然后我们可以通过您的PHP脚本返回该$ json变量。 Bringing it into your iOS application I suggest looking into JSONKit as it's one of the best performing JSON third party tools you can use for such a task: 将其引入您的iOS应用程序中,我建议您研究JSONKit,因为它是可用于此类任务的性能最佳的JSON第三方工具之一:

https://github.com/johnezang/JSONKit https://github.com/johnezang/JSONKit

Make that one variable contain a lot of data instead of just a simple integer or string. 使一个变量包含大量数据,而不只是一个简单的整数或字符串。 Anything coming back to your app will come in the form of a string. 返回到您的应用程序的所有内容都将以字符串形式出现。 HTTP doesn't have a practical limit on the size of that string so pack in as much as you'd like. HTTP对该字符串的大小没有实际限制,因此可以根据需要随意打包。

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

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