如何使用restful web服务器端在php中进行获取和发布

时间:2021-11-22 19:34:28

Hi can anyone past some code how to do a restful web service get and post method. This would be server side so I would be able to invoke the two services from a client.

嗨,任何人都可以通过一些代码如何做一个宁静的Web服务获取和发布方法。这将是服务器端,所以我可以从客户端调用这两个服务。

Thanks!

2 个解决方案

#1


Assume you have a script index.php. You might have two functions inside of it, showForm() and handleForm().

假设你有一个脚本index.php。你可能有两个函数,showForm()和handleForm()。

Assume a request comes in to index.php.

假设有一个请求进入index.php。

if (! empty($_SERVER['REQUEST_METHOD'])) {
    if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST')
    {
        doSomething();
    }
    else
    {
        showSomething();
    }
}

There you have it. REST. If you send a GET request to index.php, you'll show some output, and if you send a POST request to index.php, you'll perform some data manipulation. You can take if from there for the other RESTful HTTP request types, such as DELETE, etc.

你有它。休息。如果您向index.php发送GET请求,您将显示一些输出,如果您向index.php发送POST请求,您将执行一些数据操作。您可以从那里获取其他RESTful HTTP请求类型,例如DELETE等。

Obviously this is a very simple example, and I wouldn't want to create an entire site in this manner. It's best to go about creating a RESTful site in an architecturally sound way. Many frameworks can assist with this.

显然这是一个非常简单的例子,我不想以这种方式创建整个站点。最好以体系结构的方式创建RESTful站点。许多框架都可以为此提供帮助。

REST is a hot topic right now, it seems everyone wants their apps to be RESTful. There's a lot of articles and guides out there on Google, you'd probably do well to spend some time researching various approaches.

REST现在是一个热门话题,似乎每个人都希望他们的应用程序是RESTful。 Google上有很多文章和指南,你可能花一些时间研究各种方法。

Note about URLs: URIs don't have to be pretty to be RESTful. However, a key point of REST is that all URIs should represent a single resource. As query parameters are not part of a URI, "/index.php?show=2" is not considered RESTful. You'll find that a lot of applications use URL rewriting to convert query parameters into something like "/index/2" instead.

关于URL的注意事项:URI不必非常漂亮。但是,REST的一个关键点是所有URI都应该代表单个资源。由于查询参数不是URI的一部分,因此“/ index.php?show=2”不被视为RESTful。您会发现许多应用程序使用URL重写将查询参数转换为类似“/ index / 2”的内容。

That being said, there's nothing wrong with having "/index.php" as a URI, just as long as it only represents a single state.

话虽如此,将“/index.php”作为URI,只要它只代表一个状态就没有错。

#2


POST:
<?
// Helloworld_post.php
echo "Hello world <pre>"
print_r($_POST['data'];
?>

GET
<?
//helloworld_get.php
echo "Hello world <pre>"
print_r($_GET['data'];
?>

What exactly are you trying to do? You need to use mod_rewrite or its equivalent to make pretty /helloworld/ urls. The beauty of REST is it is just a standard http request. It doesn't specify json encoding or xml encoding or "wtf I am the 1337" encoding.

你究竟想做什么?你需要使用mod_rewrite或它的等价物来制作漂亮的/ helloworld / urls。 REST的魅力在于它只是一个标准的http请求。它没有指定json编码或xml编码或“wtf我是1337”编码。

#1


Assume you have a script index.php. You might have two functions inside of it, showForm() and handleForm().

假设你有一个脚本index.php。你可能有两个函数,showForm()和handleForm()。

Assume a request comes in to index.php.

假设有一个请求进入index.php。

if (! empty($_SERVER['REQUEST_METHOD'])) {
    if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST')
    {
        doSomething();
    }
    else
    {
        showSomething();
    }
}

There you have it. REST. If you send a GET request to index.php, you'll show some output, and if you send a POST request to index.php, you'll perform some data manipulation. You can take if from there for the other RESTful HTTP request types, such as DELETE, etc.

你有它。休息。如果您向index.php发送GET请求,您将显示一些输出,如果您向index.php发送POST请求,您将执行一些数据操作。您可以从那里获取其他RESTful HTTP请求类型,例如DELETE等。

Obviously this is a very simple example, and I wouldn't want to create an entire site in this manner. It's best to go about creating a RESTful site in an architecturally sound way. Many frameworks can assist with this.

显然这是一个非常简单的例子,我不想以这种方式创建整个站点。最好以体系结构的方式创建RESTful站点。许多框架都可以为此提供帮助。

REST is a hot topic right now, it seems everyone wants their apps to be RESTful. There's a lot of articles and guides out there on Google, you'd probably do well to spend some time researching various approaches.

REST现在是一个热门话题,似乎每个人都希望他们的应用程序是RESTful。 Google上有很多文章和指南,你可能花一些时间研究各种方法。

Note about URLs: URIs don't have to be pretty to be RESTful. However, a key point of REST is that all URIs should represent a single resource. As query parameters are not part of a URI, "/index.php?show=2" is not considered RESTful. You'll find that a lot of applications use URL rewriting to convert query parameters into something like "/index/2" instead.

关于URL的注意事项:URI不必非常漂亮。但是,REST的一个关键点是所有URI都应该代表单个资源。由于查询参数不是URI的一部分,因此“/ index.php?show=2”不被视为RESTful。您会发现许多应用程序使用URL重写将查询参数转换为类似“/ index / 2”的内容。

That being said, there's nothing wrong with having "/index.php" as a URI, just as long as it only represents a single state.

话虽如此,将“/index.php”作为URI,只要它只代表一个状态就没有错。

#2


POST:
<?
// Helloworld_post.php
echo "Hello world <pre>"
print_r($_POST['data'];
?>

GET
<?
//helloworld_get.php
echo "Hello world <pre>"
print_r($_GET['data'];
?>

What exactly are you trying to do? You need to use mod_rewrite or its equivalent to make pretty /helloworld/ urls. The beauty of REST is it is just a standard http request. It doesn't specify json encoding or xml encoding or "wtf I am the 1337" encoding.

你究竟想做什么?你需要使用mod_rewrite或它的等价物来制作漂亮的/ helloworld / urls。 REST的魅力在于它只是一个标准的http请求。它没有指定json编码或xml编码或“wtf我是1337”编码。