12

I am new to Symfony development so excuse me if this is a dumb question. I am trying to GET JSON data from an external API. I have tried to run a GET request via Postman and got the correct data in JSON format back so I know my URL is correct.

I have the following code written to use Guzzle to HTTP GET a set of JSON data from my external API:

<?php

namespace AppBundle\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Psr\Http\Message\ResponseInterface;
use GuzzleHttp\Client;

class ScheduleController {

/**
 * @Route("/schedule")
 */

public function getJobs() {
    // Create a client with a base URI
    $client = new GuzzleHttp\Client(['base_uri' => 'http://my.api.url/']);
    // Send a request to http://my.api.url/site/67/module/1449/item
    $response = $client->request('GET', 'site/67/module/1449/item');
    dump($response);
}
}

I am trying to follow along as closely as I can with the Guzzle quick start guide. I am getting a 500 error from this code: Attempted to load class "Client" from namespace "AppBundle\Controller\GuzzleHttp". Did you forget a "use" statement for e.g. "Symfony\Component\BrowserKit\Client", "Symfony\Component\HttpKernel\Client" or "Symfony\Bundle\FrameworkBundle\Client"?

I have a use statement for use GuzzleHttp\Client; What am I doing wrong? Is there an easier way to do this?

Liz
  • 1,008
  • 5
  • 19
  • 49

2 Answers2

9

This line: $client = new GuzzleHttp\Client(['base_uri' => 'http://my.api.url/']);

Should be:

$client = new Client(['base_uri' => 'http://my.api.url/']);

or:

$client = new \GuzzleHttp\Client(['base_uri' => 'http://my.api.url/']);

Either one would work since you imported the namespace. You probably want the first option. When you put GuzzleHttp\Client PHP thinks it's a relative namespace that's why it's saying "AppBundle\Controller\GuzzleHttp\Client" cannot be found.

Chip Dean
  • 4,222
  • 3
  • 24
  • 36
  • I got this error from that change (I am using the first line of code you suggested): `Attempted to load class "Client" from namespace "GuzzleHttp". Did you forget a "use" statement for e.g. "Symfony\Component\BrowserKit\Client", "Symfony\Component\HttpKernel\Client" or "Symfony\Bundle\FrameworkBundle\Client"?` – Liz Jan 23 '17 at 23:37
  • 1
    Have you actually installed guzzle through composer? Based on that error it seems guzzle is not installed. – Chip Dean Jan 23 '17 at 23:43
  • I did install it. Is there a way to troubleshoot installation? – Liz Jan 23 '17 at 23:49
  • 1
    You could run 'composer require guzzlehttp/guzzle' from the command line root of your project. – Chip Dean Jan 23 '17 at 23:57
  • 1
    You can always just look under vendor but based on the error message I suspect you have another file with new Guzzle\Client in it. Or maybe another method withing the same file. Maybe you are editing the wrong file. Do a global search. – Cerad Jan 24 '17 at 01:30
1

I set the IP address from localhost:8087 to my windows IP address 192.168.x.x:8087.

It worked so there was no need to do anything with linux ports/docker ports etc.

Milo
  • 3,365
  • 9
  • 30
  • 44