SilverStripe: How to Make an HTTP Request to Another Site?

I am trying to make an HTTP request to another website inside a controller method. I have searched for solutions, but I cannot 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 am getting 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 to work?

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

+3


source to share


2 answers


The built-in way to send requests to external sites or resources is by using RestfulService

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

Typical use:



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

you will need to install the http extension ( http://php.net/manual/en/http.install.php )

+5


source


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

This PECL extension is not related to PHP.



This question has nothing to do with SilverStripe. You need to install the module or use curl (which wampserver comes 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.

+1


source







All Articles