Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SilverStripe: How do I make HTTP Request to another website?

I am trying to make a HTTP request to another website inside a controller method. I searched for solutions but I can't find any working examples.

Here is my code:

$r = new HttpRequest('http://community.bba.org/home', HttpRequest::METH_GET);
$r->addQueryData(array('SessionID' => $arrGetParams['SessionID']));
try {
    $r->send();
} catch (HttpException $ex) {}

I get the following error:

Fatal error: Class 'HttpRequest' not found in C:\wamp\www\abb\mysite\code\form\ALoginForm.php on line 215

How can I get this HTTP request working?

I am using SilverStripe on WAMP on a Windows 7 machine.

like image 771
user1400290 Avatar asked Dec 05 '25 05:12

user1400290


2 Answers

The built in way to make requests to external sites or resources is by using the RestfulService

Docs are here: http://docs.silverstripe.org/en/3.1/developer_guides/integration/restfulservice/

Typical usage:

$service = new RestfulService('http://community.bba.org/home', 1200); //domain, cache duration
$service->setQueryString(array(
    'SessionID' => $arrGetParams['SessionID'],
));
$response = $service->request();
$body = $response->getBody();

If you want to use PHP's HTTPRequest you'll have to installthe http extension (http://php.net/manual/en/http.install.php)

like image 131
Dan Hensby Avatar answered Dec 07 '25 20:12

Dan Hensby


http://php.net/manual/en/http.install.php

This » PECL extension is not bundled with PHP.

This issue has nothing to do with SilverStripe itself. You need to install the module, or use curl (which wampserver does come bundled with). How to enable curl in Wamp server

There is http://docs.silverstripe.org/en/3.1/developer_guides/integration/restfulservice/ but I don't recommend it.

like image 26
user5069811 Avatar answered Dec 07 '25 20:12

user5069811



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!