简体   繁体   English

我如何将参数传递给Swift / iOS的php文件中的类函数?

[英]How would I pass a parameter into a class function in a php file from Swift/iOS?

Say I have a php file on my web server which contains a class 'Database'. 假设我的网络服务器上有一个php文件,其中包含“数据库”类。 Inside that class is a public function, lets call it 'data_select', which queries MySql database: 在该类内部是一个公共函数,可将其称为“ data_select”,它查询MySql数据库:

public function data_select($query) {
...
return $rows;

How would I pass in the '$query' parameter to this function from a Swift iOS application? 如何从Swift iOS应用程序将'$ query'参数传递给此函数?

There are a number of answers online regarding $_POST from swift, but these often concern SQL queries such as 'INSERT INTO ...', whereas my case is a 'SELECT...' query. 网上有很多关于$ _POST的答案,但是这些通常涉及SQL查询,例如“ INSERT INTO ...”,而我的情况是“ SELECT ...”查询。 I also cannot seem to find anything regarding triggering class functions from Swift, although I know it must be possible. 我似乎也找不到从Swift触发类函数的任何东西,尽管我知道这是有可能的。

Have I got the wrong idea here? 我在这里有个错误的主意吗? I understand how to pull data from a php file on web server when the sql query and everything is included in the same file, but here the parameters of the query can differ, thus I have the basic SELECT query as part of a database class which can be called with different parameters on my test website, but I cannot work out how to pass in the query parameter from swift and trigger the function. 我了解当sql查询和所有内容都包含在同一文件中时,如何从Web服务器上的php文件中提取数据,但是这里的查询参数可能不同,因此我将基本的SELECT查询作为数据库类的一部分可以在我的测试网站上用不同的参数调用,但是我无法弄清楚如何从swift中传递查询参数并触发函数。

I guess I could trigger the functions by making a call to a different php file containing an instance of the function with the $query parameter passed in from swift. 我想我可以通过调用另一个包含该函数实例的不同php文件来调用这些函数,并带有从swift传入的$ query参数。 But would I then need to create multiple php files for each different 'SELECT...' query which sort of defeats the point of having the functions in a class??? 但是我接下来需要为每个不同的“ SELECT ...”查询创建多个php文件吗?这有点使类中的函数失去作用了?

PHP is scripting language, mainly used as a server side web language. PHP是脚本语言,主要用作服务器端Web语言。 Swift can communicate with any server side language that uses HTTP. Swift可以与使用HTTP的任何服务器端语言进行通信。 You can use any of the HTTP verbs such as GET , POST , PUT etc to send and receive data from PHP using Swift. 您可以使用任何HTTP动词(例如GETPOSTPUT等)使用Swift来从PHP发送和接收数据。

So you'd want to create an endpoint with PHP. 因此,您想使用PHP创建一个端点。 You can make an NSMutableURLRequest in swift to send data as a URL parameter. 您可以迅速创建一个NSMutableURLRequest以将数据作为URL参数发送。

Take a look at this link to make a GET request in swift. 看一下此链接 ,快速提出GET请求。

And take a look at this link to handle a GET request in PHP. 并查看此链接以处理PHP中的GET请求。

For the multiple files part of your question, you could put many of these endpoints all within one file but that is an implementation and design choice that depends on how you want your application and server to flow. 对于问题的多个文件部分,您可以将许多端点都放在一个文件中,但这是一种实现和设计选择,具体取决于您希望应用程序和服务器如何流动。

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

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