GuzzleHttp and Laravel

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 GuzzleHttpClient();
$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.


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.


尝试如下:

$client = new GuzzleHttpClient();
 $response = $client
        ->get('https://api.postcodes.io/postcodes/'.Input::get('postcode'));
 dd($response->getBody());
链接地址: http://www.djcxy.com/p/69514.html

上一篇: Laravel解析错误:语法错误,意外的T

下一篇: GuzzleHttp和Laravel