我有一个正在显示的端点。但是我有一个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->body news body $newobj->图像触发器进入image_teaser
诸若此类。
发布于 2019-06-06 16:16:43
您不需要创建一个stdClass或将您的循环数据添加到一个新数组以保存记录。你可以用拉拉雄辩的模型保存唱片。
调用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链接没有完成。
https://stackoverflow.com/questions/56481169
复制相似问题