W
W
w_b_x2019-02-17 19:09:14
symfony
w_b_x, 2019-02-17 19:09:14

How to use Guzzle from Symfony controller?

Hello saviors!
There is a task:
To give the controller the result from the external API.
I installed the Symfony Guzzle bundle to make a get request - this one .
And I have a controller that does this task:

/**
     * @Route("/api/data")
     */
    public function getData()
    {
        $url = "http://127.0.0.1:8000/test_guzzle";
        $guzzle = $this->get('eight_points_guzzle.client.my_client');
        $result = $guzzle->request('GET', $url);
        return $this->json([$result]);
    }

The /test_guzzle controller looks like this:
/**
     * @Route("/test_guzzle")
     */
    public function getThread(){
        return new Response("HELLO!!!");
 }

But I am getting Exception:
cURL error 28: Operation timed out after 30000 milliseconds with 0 bytes received (see http://curl.haxx.se/libcurl/c/libcurl-errors.html)

I don't understand why the request fails.
And from the browser in incognito, the link 127.0.0.1:8000/test_guzzle opens perfectly and shows "HELLO!!!"
What could be the snag?
Addendum:
Changed $url: Code worked perfectly. What could it be? (I am currently testing both the API and the call on the same server, i.e. the url to the first controller looks like this: 127.0.0.1:8000/api/data)
$url = "http://httpbin.org/get";

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vadim, 2019-02-20
@Matmode

w_b_x The problem is not Guzzle.
You most likely started the server with the php app/console server:start command . This command uses the built-in php server. It, according to the documentation, can only process one request at a time.
php.net/manual/en/features.commandline.webserver.php
This is easy to verify if you open the following pages at the same time

/**
     * @Route("/api", name="api")
     */
    public function apiAction()
    {
        sleep(10);

        return new Response('API page');
    }

    /**
     * @Route("/test", name="test")
     */
    public function testAction()
    {
        return new Response('Test page');
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question