2015-06-19 70 views
1

我目前正在尝试使用GuzzleHttp与Laravel一起访问基于用户输入的API。GuzzleHttp和Laravel - 语法错误 - T_String

我的建立至今:

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

,但返回的错误是:

FatalErrorException在ClinicController.php行129:语法错误,意外 '输入'(T_STRING)

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

任何帮助为什么会发生这将非常感激。

回答

0

这是一个简单的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') 

而且,要考虑的事情 - 在生产应用程序,你想要么消毒或验证Input::get('postcode')在使用URL请求之前实际上包含邮政编码。总是假定有人会尝试恶意使用您的系统,并且永远不要相信用户输入将包含您认为它包含的内容。

+0

谢谢Alan,我认为'Input ::'确实会清理所有输入?在使用之前,邮编通过“正则表达式”检查器。 – Ben

0

尝试如下:

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