GuzzleHttp 和 Laravel - 语法错误 - T_String


GuzzleHttp and Laravel - Syntax error - T_String

我目前正在尝试将GuzzleHttp与Laravel一起使用,以根据用户的输入访问API。

到目前为止我的设置:

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

但返回的错误是:

ClinicController中的FatalErrorException.php第129行:语法错误, 意外的"输入"(T_STRING)

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

任何帮助为什么会发生这种情况将不胜感激。

这是一个简单的PHP错误。 这是PHP抱怨的那句话

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

你有一个字符串

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

紧随其后的是Input::get('postcode'). 如果尝试将这两者组合在一起,则需要使用 . 运算符连接字符串

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

另外,需要考虑的是 - 在生产应用程序中,您需要在此类URL请求中使用之前清理或验证Input::get('postcode')是否实际包含邮政编码。始终假设有人会尝试恶意使用您的系统,并且永远不要相信用户输入将包含您认为它包含的内容。

尝试如下:

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