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)?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With