I'm new to Laravel. Trying to work with external API.
In controller i wont just to make variable with json:
$testapi = new TestApi();
$data['books'] = $testapi->books();
return view('books.index', $data);
In model (just php class without any extends) this:
public function books()
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $this->host."/books");
curl_setopt($curl, CURLOPT_HTTPHEADER, $this->headers);
$result = curl_exec($curl);
$result = json_decode($result);
curl_close($curl);
return $result;
}
But json content is placed at the beggining of HTML file, even before META information. curl_exec don't save result to variable, but transfer it to global result. How to save this json to variable and then use inside template?
I believe you're missing
curl_setopt($curl, CURLOPT_RETURNTRANSFER,TRUE);
that will allow the return to be put into your $result variable, instead of outputting directly.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.