Laravel-如何将api端点保存到数据库

时间:2019-06-06 15:54:27

标签: 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;
    }  

我要将其保存到该表中

  

news_feeds

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

并保存到表格中

  

news_feeds

     

$ newobj-> id转换为id

     

$ newobj-> id进入标题

     

$ newobj->正文变为新闻正文

     

$ newobj-> image_teaser转换为image_teaser

以此类推。

1 个答案:

答案 0 :(得分:1)

您无需创建stdClass或将循环数据添加到新数组即可保存记录。您可以使用laravel雄辩的模型保存记录。

调用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的{​​{1}},则我们使用headline方法来创建新的::create()。这应该创建一个新记录。

注意:您的NewsFeed链接尚未完成。