I've always used cURL for this sort of stuff, but this article got me thinking I could request another page easily using the Request object in Kohana 3.
我总是使用cURL来做这类事情,但是这篇文章让我想到我可以使用Kohana 3中的Request对象轻松地请求另一个页面。
$url = 'http://www.example.com';
$update = Request::factory($url);
$update->method = 'POST';
$update->post = array(
'key' => 'value'
);
$update->execute();
echo $update->response;
However I get the error
但是我得到了错误
Accessing static property Request::$method as non static
From this I can assume it means that the method method is static, but that doesn't help me much. I also copied and pasted the example from that article and it threw the same error.
从这里我可以假设这意味着方法方法是静态的,但这对我没有多大帮助。我也复制并粘贴了那篇文章的例子,它也犯了同样的错误。
Basically, I'm trying to POST to a new page on an external server, and do it the Kohana way.
基本上,我正在尝试POST到外部服务器上的新页面,并以Kohana方式执行。
So, am I doing this correctly, or should I just use cURL (or file_get_contents()
with context)?
那么,我正确地做了这个,还是应该只使用cURL(或file_get_contents()与上下文)?
3 个解决方案
#1
10
I don't know if this was initially written when the OP was using Kohana 3.0, but the major release Kohana 3.1 has made this significantly easier to do. The Remote::get() is deprecated (and wasn't that good to begin with). To accomplish this in Kohana 3.1 is a simple matter, and you pretty much had it:
我不知道这是在OP使用Kohana 3.0时是否最初编写的,但主要版本Kohana 3.1使这更容易做到。不推荐使用Remote :: get()(开始时效果不佳)。要在Kohana 3.1中实现这一点是一件简单的事情,你几乎拥有它:
$url = 'http://www.example.com';
$request = Request::factory($url)
->method('POST')
->post('key', 'value');
$response = $request->execute();
echo $response->body();
I moved some stuff around to take advantage of the succinctness of the chaining syntax. With the response, you can check the response code as well. For more information read the 3.1 API docs for Request and Request_Client_External (which handles these external i.e. not within-app requests.
我移动了一些东西,以利用链接语法的简洁性。通过响应,您还可以检查响应代码。有关更多信息,请阅读Request for Request和Request_Client_External的3.1 API文档(它处理这些外部即不是app内请求。
#2
4
Just read this at the bottom
请在底部阅读此内容
The request class used in this example is currently available as part of a Kohana Core development branch within my personal github account, which can be obtained from http://github.com/samsoir/core. If using the official Kohana PHP 3.0 download, a custom extension of the request class is required.
此示例中使用的请求类目前作为我个人github帐户中的Kohana Core开发分支的一部分提供,可从http://github.com/samsoir/core获取。如果使用官方Kohana PHP 3.0下载,则需要自定义扩展请求类。
Also see this discussion.
另见本讨论。
#3
-2
The Request object is used to request pages within your application. You can't use it for external URLs. Oh, and you don't have to use curl, you can make it easier by doing this:
Request对象用于请求应用程序中的页面。您不能将其用于外部URL。哦,你不必使用curl,你可以通过这样做更容易:
$page = file_get_contents('http://google.com');
#1
10
I don't know if this was initially written when the OP was using Kohana 3.0, but the major release Kohana 3.1 has made this significantly easier to do. The Remote::get() is deprecated (and wasn't that good to begin with). To accomplish this in Kohana 3.1 is a simple matter, and you pretty much had it:
我不知道这是在OP使用Kohana 3.0时是否最初编写的,但主要版本Kohana 3.1使这更容易做到。不推荐使用Remote :: get()(开始时效果不佳)。要在Kohana 3.1中实现这一点是一件简单的事情,你几乎拥有它:
$url = 'http://www.example.com';
$request = Request::factory($url)
->method('POST')
->post('key', 'value');
$response = $request->execute();
echo $response->body();
I moved some stuff around to take advantage of the succinctness of the chaining syntax. With the response, you can check the response code as well. For more information read the 3.1 API docs for Request and Request_Client_External (which handles these external i.e. not within-app requests.
我移动了一些东西,以利用链接语法的简洁性。通过响应,您还可以检查响应代码。有关更多信息,请阅读Request for Request和Request_Client_External的3.1 API文档(它处理这些外部即不是app内请求。
#2
4
Just read this at the bottom
请在底部阅读此内容
The request class used in this example is currently available as part of a Kohana Core development branch within my personal github account, which can be obtained from http://github.com/samsoir/core. If using the official Kohana PHP 3.0 download, a custom extension of the request class is required.
此示例中使用的请求类目前作为我个人github帐户中的Kohana Core开发分支的一部分提供,可从http://github.com/samsoir/core获取。如果使用官方Kohana PHP 3.0下载,则需要自定义扩展请求类。
Also see this discussion.
另见本讨论。
#3
-2
The Request object is used to request pages within your application. You can't use it for external URLs. Oh, and you don't have to use curl, you can make it easier by doing this:
Request对象用于请求应用程序中的页面。您不能将其用于外部URL。哦,你不必使用curl,你可以通过这样做更容易:
$page = file_get_contents('http://google.com');