laravel - Laravel - 如何将 api 端点保存到数据库中
问题描述
我有这个正在显示的端点。但是我有一个要保存到数据库中的 API 端点。
public function apifeed(Request $request)
{
$array_content=[];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://api.company.com/article/6spf2p?_fmt=xml&_rt=b&_fld=hl,img,bd&lnk=urn:perform:image&_lcl=en");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //Important
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
$result = curl_exec($ch);
//$array = json_decode($result, true);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close ($ch);
$plainXML = self::mungXML($result);
$arrayResult = json_decode(json_encode(SimpleXML_Load_String($plainXML, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
$i=0;
foreach($arrayResult['article'] as $value)
{
$newobj = new stdClass();
$newobj->id = $value['@attributes']['id'];
$newobj->headline = $value['headline'];
$newobj->body = $value['body'];
$newobj->image_header ='https://images.performgroup.com'.
$value['links']['link'][0]['@attributes']['url'];
$newobj->image_teaser ='https://images.performgroup.com'.
$value['links']['link'][1]['@attributes']['url'];
$newobj->image_mobile ='https://images.performgroup.com'.
$value['links']['link'][2]['@attributes']['url'];
$newobj->image_source = 'https://images.performgroup.com'.
array_push($array_content,$newobj);
$i++;
}
return $array_content;
}
我想把它保存到这个表中
新闻提要
CREATE TABLE `news_feeds` (
`id` varchar(80) NOT NULL,
`headline` varchar(255) NOT NULL,
`news_body` text NOT NULL,
`image_teaser` varchar(300) NOT NULL,
`image_mobile` varchar(300) NOT NULL,
`image_source` varchar(300) NOT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
模型是 NewsFeed
之后我如何写一个foreach语句
返回 $array_content
并保存到表中
新闻提要
$newobj->id 变成 id
$newobj->id 进入标题
$newobj->body 进入新闻正文
$newobj->image_teaser 到 image_teaser
等等。
解决方案
您无需创建stdClass
循环数据或将循环数据添加到新数组即可保存记录。您可以使用 laravel eloquent 模型保存记录。
调用后curl
,您可以使用NewsFeed
模型类和::create()
方法创建新newsfeed
记录:
foreach($arrayResult['article'] as $value)
{
if (NewsFeed::where('headline', $value['headline'])->exists()) {
continue;
}
NewsFeed::create([
'id' => $value['@attributes']['id'],
'headline' => $value['headline'],
'body' => $value['body'],
'image_header' => 'https://images.performgroup.com'.$value['links']['link'][0]['@attributes']['url'],
'image_teaser' => 'https://images.performgroup.com'.$value['links']['link'][1]['@attributes']['url'],
'image_mobile' => 'https://images.performgroup.com'.$value['links']['link'][2]['@attributes']['url'],
'image_source' => 'https://images.performgroup.com'
]);
}
在这里,我们首先newsfeed
通过检查是否存在与headline
我们尝试保存的相同来检查是否存在。如果存在,我们跳过它。
如果不newsfeed
存在headline
,我们然后使用该::create()
方法创建新的NewsFeed
。这应该会创建一个新记录。
注意:您的image_source
链接未完成。
推荐阅读
- java - JVM 什么时候开始忽略堆栈跟踪?
- c# - Dapper 更新返回日期时间格式
- c# - 基于 CompareExchange 的联锁实现是否应该使用 SpinWait?
- r - 如何将数据框从单列值重组为重复的列行
- javascript - 获取表中输入文本框的未定义值?
- .net - 将网站发布到 AZURE 后出现浏览器错误
- javascript - 动态 svg xlink:href 不在浏览器中显示图标
- html - Thymeleaf 条件有两个变量
- laravel - 热模块替换不适用于 Laravel Mix 和 Vagrant
- maxima - Maxima CAS 的限制错误