3

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.

3dgoo
  • 15,716
  • 6
  • 46
  • 58
user1400290
  • 1,682
  • 5
  • 23
  • 43

2 Answers2

5

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)

Dan Hensby
  • 1,118
  • 6
  • 11
  • Thanks, now I can see the response, I have a scenario where I need to pass the query string value on another website, this creates session on that website, I am able to do so if I have hidden Iframe on my webpage. Is there a way to achieve this in php code by making http request, I tried it with your code but it doesn't creates session when i access that website in same browser? – user1400290 Jul 02 '15 at 12:25
  • You'll need to open a new question for that, I think. But you won't be able to start anger session in your user's browser in this way. – Dan Hensby Jul 02 '15 at 14:06
1

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.

Community
  • 1
  • 1