2017-04-09 320 views
2

我正在嘗試進行API調用。使用配置api標記給出錯誤'URI必須是字符串或UriInterface'Laravel

如果我這樣做:$url = "url-code.com?param1=value1&param2=value2&_token=enter-key-here"; 我沒有得到任何錯誤。

如果我這樣做:$url = "url-code.com?param1=value1&param2=value2&_token="+Config::get('app.john_doe_key');

我得到一個錯誤: 'URI必須是一個字符串或UriInterface'

mycode的

$statusCode = 200; 
     $url = "url-code.com?param1=value1&param2=value2&_token="+Config::get('app.john_doe_key'); 

      $client = new Client(); 
      $res = $client->get($url); 
      //dd($res); 
      return $res->getBody(); 

.ENV

JOHN_DOE_APP_KEY=key 

config/app .PHP

'john_doe_key' => env('JOHN_DOE_APP_KEY'), 
+0

好奇,你使用配置助手會發生什麼? '配置( 'app.john_doe_key')'?這就是說,你也可以使用配置文件嗎? –

+0

似乎'Config :: get('app.john_doe_key')'爲空。它打印什麼? – Yolo

+0

你用+來追加字符串嗎? – manian

回答

2

好,基於我們對原來問題的意見的討論,這裏就是我會嘗試。

因爲一切都在它自己的工作正常,我會把所有的參數在各自爲陣:

$parameters = [ 
    'someParam' => 'value', 
    'someOtherParam' => 'value', 
    '_token' => Config::get('app.john_doe_key') 
]; 

並使用http_build_query()正確格式化:

$formattedParameters = http_build_query($parameters); 

最後,建立與我有什麼URL:

$url = "http://url-code.com?{$formattedParameters}"; 

你應該有一個correctl y格式化的URL,以便在此時使用Guzzle。

+0

你可以看看這個問題:http://stackoverflow.com/questions/43306641/how-to-update-the-data-every-1-second-on-server-side- php-laravel是基於相同的代碼。我想在服務器端每隔1秒執行一次。我將創建一個RESTful Api來訪問我的應用程序中的這些數據。 –

相關問題