SilverStripe:如何向其他网站发出HTTP请求?

时间:2015-07-01 11:28:02

标签: php httprequest silverstripe

我正在尝试向控制器方法内的其他网站发出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请求正常工作?

我在Windows 7计算机上使用WAMP上的SilverStripe。

2 个答案:

答案 0 :(得分:5)

向外部网站或资源发出请求的内置方式是使用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

答案 1 :(得分:1)

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

  

这个»PECL扩展不与PHP捆绑在一起。

此问题与SilverStripe本身无关。您需要安装模块,或使用curl(与wampserver捆绑在一起)。 How to enable curl in Wamp server

http://docs.silverstripe.org/en/3.1/developer_guides/integration/restfulservice/,但我不推荐它。

相关问题