Search code examples
phplaravel

How to save API endpoint into database?


I have this endpoint that is being displayed. But I have an API endpoint that I want to save into the database.

     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;
    }  

I want to save it into this table

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;

Model is NewsFeed

How do I write a foreach statement after

return $array_content

and save into the table

news_feeds

$newobj->id into id

$newobj->id into headline

$newobj->body into news body

$newobj->image_teaser into image_teaser

and so on.


Solution

  • You do not need to create a stdClass or add your looped data to a new array to save records. You can save records using laravel eloquent models.

    After your curl call, you can use your NewsFeed model class and the ::create() method to create a new newsfeed record:

    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'
        ]);
    }
    

    Here, we initially check if the newsfeed exists by checking if there are any with the same headline we are trying to save. If it exists, we skip it.

    If no newsfeed with the headline exists, we then use the ::create() method to create the new NewsFeed. This should create a new record.

    Note: Your image_source link was not completed.