I am trying to make connection with a API and i want to learn to work with it. (I work with Laravel) I am trying to convert a JSON to a string but if i echo the converted string it gives me this error:
ErrorException in helpers.php line 531:
htmlentities() expects parameter 1 to be string, array given (View: /home/stackingcoder/development/PHP/internetstuffer/resources/views/index.blade.php)
This is my HomeController.php :
public function index()
{
$url = 'https://www.quandl.com/api/v3/databases/WIKI.json';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
$string = json_decode($data, true);
curl_close($ch);
return view('index', compact('string'));
}
I am using templating engine Blade, so my echo looks like this:
{{ $string }}
Edit:
As end result i just need an array, how do i convert the API call to an array? So in can split the data like this:
echo $data['database']['name'];
just change your last line. once you use json_decode
with true
flag, it will return array.
public function index()
{
$url = 'https://www.quandl.com/api/v3/databases/WIKI.json';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
$string = json_decode($data, true);
curl_close($ch);
// name can be called here as $string['database']['name']
// once passed the string to view, called inside blade as $database['name']
// It seems i have to use compact, otherwise it will give me the
// error: Undifined variable: $string
return view('index', compact('string'));
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments