user1400290 user1400290 - 1 year ago 124
PHP Question

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('', HttpRequest::METH_GET);
$r->addQueryData(array('SessionID' => $arrGetParams['SessionID']));
try {
} 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.

Answer Source

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

Docs are here:

Typical usage:

$service = new RestfulService('', 1200); //domain, cache duration
    'SessionID' => $arrGetParams['SessionID'],
$response = $service->request();
$body = $response->getBody();

If you want to use PHP's HTTPRequest you'll have to installthe http extension (

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download