[英]How to save curl_exec result into variable in Laravel
I'm new to Laravel.我是 Laravel 的新手。 Trying to work with external API.
尝试使用外部 API。
In controller i wont just to make variable with json:在控制器中,我不会只是用 json 来创建变量:
$testapi = new TestApi();
$data['books'] = $testapi->books();
return view('books.index', $data);
In model (just php class without any extends) this:在模型中(只是没有任何扩展的 php 类):
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.但是 json 内容放在 HTML 文件的开头,甚至在 META 信息之前。 curl_exec don't save result to variable, but transfer it to global result.
curl_exec 不将结果保存到变量,而是将其传输到全局结果。 How to save this json to variable and then use inside template?
如何将此json保存到变量然后在模板中使用?
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.这将允许将返回值放入您的 $result 变量中,而不是直接输出。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.