How to perform an external request in Kohana 3?
Asked Answered
B

3

8

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.

    $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.

So, am I doing this correctly, or should I just use cURL (or file_get_contents() with context)?

Brinkmanship answered 15/4, 2010 at 3:11 Comment(0)
R
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:

$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.

Recruit answered 25/5, 2011 at 1:58 Comment(4)
According to the Kohana docs kohanaframework.org/3.1/guide/api/Request#execute, $request->execute() returns a Request object. No need to call $request->execute()->response();Kaliningrad
Actually, you do have to call the response() function - otherwise you're still dealing with the Request object. Both the Request class and the Response class have a body that you can read. The body() of a Request is exactly that - the request body. Mostly that means any form data that came in, etc. Not the behavior you'd want, and would certainly be weird to get back exactly what you sent!Recruit
Jason, using 3.13 I think, I get results similar to Diego. Your example errors out with "Undefined method: Response::response()." If I change that line to "$response = $request->execute();" it works as intended.Juneberry
@SkippyFlipjack, you're right. Since Diego's comment said execute() returned a Request I got confused. The execute() method does in fact return a Response object. I've updated the answer accordingly :)Recruit
B
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.

Also see this discussion.

Brinkmanship answered 15/4, 2010 at 4:1 Comment(0)
L
-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:

$page = file_get_contents('http://google.com');
Levitus answered 19/4, 2010 at 17:14 Comment(7)
I have to create a stream context for the POST I want to send though.Brinkmanship
I am under the impression that allowing external urls via file_get_contents is a security risk and bad practiceJustifier
@mike: You have the wrong impression then. Assuming you have a hardcoded URL (like in my example), there is absolutely nothing unsecure about this.Levitus
Of course it depends on your implementation. The above example is secure, where it becomes insecure, is when people use it with a dynamic input and don't limit the http(s) domains request, thus opening yourself up to remote code injections.Justifier
@mike: That would only be a problem if you were for some reason executing the string returned from this function, which you shouldn't be doing with anything regardless.Levitus
I totally agree, but have cleaned up a few messes from developers making these mistakes, and work with admins that always shut this setting off.Justifier
You can now make external requests through the Request factory. See my answer for details.Recruit

© 2022 - 2024 — McMap. All rights reserved.