首页 > 解决方案 > 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

等等。

标签: laravelapi

解决方案


您无需创建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链接未完成。


推荐阅读