我正试图向控制器方法内的另一个网站发出http请求。我寻找解决办法,但找不到任何可行的例子。
这是我的代码:
$r = new HttpRequest('http://community.bba.org/home', HttpRequest::METH_GET);
$r->addQueryData(array('SessionID' => $arrGetParams['SessionID']));
try {
$r->send();
} catch (HttpException $ex) {}
我得到以下错误:
致命错误:在第215行的C:\wamp\www\abb\mysite\code\form\aloginform.php中找不到类“httprequest”
我怎样才能让这个HTTP请求工作?
我在Windows7机器上的WAMP上使用SilverStripe。
最佳答案
向外部站点或资源发出请求的内置方式是使用RestfulService
医生来了:http://docs.silverstripe.org/en/3.1/developer_guides/integration/restfulservice/
典型用法:
$service = new RestfulService('http://community.bba.org/home', 1200); //domain, cache duration
$service->setQueryString(array(
'SessionID' => $arrGetParams['SessionID'],
));
$response = $service->request();
$body = $response->getBody();
如果您想使用php的
HTTPRequest
,就必须安装http扩展(http://php.net/manual/en/http.install.php)