Symfony \ Component \ HttpFoundation和Tesco API

时间:2018-02-14 10:47:07

标签: php symfony http

我正在尝试使用Symfony \ Component \ HttpFoundation重新创建以下Tesco API代码:

<?php
// This sample uses the Apache HTTP client from HTTP Components (http://hc.apache.org/httpcomponents-client-ga/)
require_once 'HTTP/Request2.php';

$request = new Http_Request2('https://dev.tescolabs.com/grocery/products/?query={query}&offset={offset}&limit={limit}');
$url = $request->getUrl();

$headers = array(
    // Request headers
    'Ocp-Apim-Subscription-Key' => '{subscription key}',
);

$request->setHeader($headers);

$parameters = array(
    // Request parameters
);

$url->setQueryVariables($parameters);

$request->setMethod(HTTP_Request2::METHOD_GET);

// Request body
$request->setBody("{body}");

try
{
    $response = $request->send();
    echo $response->getBody();
}
catch (HttpException $ex)
{
    echo $ex;
}

?>

我是php的新手,我正在进行我的第一个Symfony项目。有人请帮助我使用Symfony HttpFoundation重新创建上面的代码吗?

我尝试过以下代码,但我什么也没有回复:

        $req2 = Request::create('https://dev.tescolabs.com/grocery/products/?query={query}&offset={offset}&limit={limit}', 'GET');
        $req2->headers->set('Ocp-Apim-Subscription-Key', 'my_api_key');

        $params = array(
            'query' => 'walkers',
            'offset' => '0',
            'limit' => '10',
        );
        $req2->query->add($params);    

        try
        {
            $response = new Response();
            var_dump($response);die;
        }
        catch (HttpException $ex)
        {
            die ('EX: '.$ex);
        }

1 个答案:

答案 0 :(得分:0)

Symfony的Request类用于向Symfony发送传入请求。也许您应该看看Guzzle使用面向对象的方法来创建Symfony2 - How to perform an external Request中提出的请求或cURL