如何在Kohana 3中执行外部请求?

ale*_*lex 8 php curl kohana 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;
Run Code Online (Sandbox Code Playgroud)

但是我得到了错误

Accessing static property Request::$method as non static
Run Code Online (Sandbox Code Playgroud)

从这里我可以假设这意味着方法方法是静态的,但这对我没有多大帮助.我也复制并粘贴了那篇文章中的例子,它也犯了同样的错误.

基本上,我正在尝试POST到外部服务器上的新页面,并以Kohana方式执行.

所以,我正确地做了这个,还是应该只使用cURL(或file_get_contents()上下文)?

diu*_*ist 10

我不知道这是在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();
Run Code Online (Sandbox Code Playgroud)

我移动了一些东西,以利用链接语法的简洁性.通过响应,您还可以检查响应代码.有关更多信息,请阅读Request for RequestRequest_Client_External的3.1 API文档(它处理这些外部即不是app内请求.