GuzzleHttp and Laravel - Syntax error - T_String

775 Views Asked by At

I am currently trying to use GuzzleHttp with Laravel to access an API based on the user's input.

My set-up so far:

$client = new \GuzzleHttp\Client();
$response = $client
        ->get('https://api.postcodes.io/postcodes/'Input::get('postcode'));
dd($response->getBody());

but the error being returned is:

FatalErrorException in ClinicController.php line 129: syntax error, unexpected 'Input' (T_STRING)

Line 129 is https://api.postcodes.io/postcodes/'Input::get('postcode')

Any help why this is occurring would be hugely appreciated.

2

There are 2 best solutions below

1
On BEST ANSWER

That's a simple PHP error. Here's the line PHP is complaining about

->get('https://api.postcodes.io/postcodes/'Input::get('postcode'));

You have a string

'https://api.postcodes.io/postcodes/'

immediately followed by Input::get('postcode'). If you're trying to combine these two, you'll want to use the . operator to concatenate the strings

 'https://api.postcodes.io/postcodes/' . Input::get('postcode')

Also, something to consider -- in a production application, you'd want to either sanitize or validate that Input::get('postcode') actually contains a post code before using it in a URL request like that. Always assume someone will try to use your system maliciously, and never trust that user input will contain what you think it contains.

0
On

Try as below :

$client = new \GuzzleHttp\Client();
 $response = $client
        ->get('https://api.postcodes.io/postcodes/'.Input::get('postcode'));
 dd($response->getBody());