Reputation: 3
I am trying to do api integration with a news providing website through curl.I am able to retrieve the data but it is not in json format so it is difficult to display that data on my portal.
Below is the code which i am using.
Please someone help
curl.php
<?php
function file_get_contents_curl()
{
$url='https://newsapi.org/v2/top-headlines?'.
'country=us&'.
'apiKey=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
$content = false;
$headers = array('Content-Type:application/json');
if (function_exists('curl_init')) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_MAXREDIRS, 5);
$content = curl_exec($curl);
curl_close($curl);
}
return $content;
}
$result=file_get_contents_curl();
echo $result;
?>
Below is the result which i am getting if i echo the response received from above code
{"status":"ok","totalResults":36,"articles":[{"source":{"id":null,"name":"Tmz.com"},"author":"TMZ Staff","title":"'90 Day Fiance' Colt Johnson Files for Divorce from Larissa After Fight and Arrest - TMZ","description":"Colt has filed for divorce from Larissa after the \"90 Day Fiance\" couple's bizarre fight and her arrest.","url":"https://www.tmz.com/2019/01/12/90-day-fiance-colt-johnson-files-divorce-larissa-dos-santos-lima-bloody-fight-arrest/","urlToImage":"https://images.tmz.com/2019/01/12/0112-larissa-johnson-colt-johnson-instagram-01-1200x630.jpg","publishedAt":"2019-01-12T20:55:00Z","content":null},{"source":{"id":null,"name":"Kstp.com"},"author":"The Associated Press","title":"Granddad: Wisconsin girl has no link to suspected kidnapper - KSTP","description":"The grandfather of a northwestern Wisconsin girl who authorities say was abducted during a home invasion that left her parents dead said Saturday that the family has no connection to the suspect and doesn't understand why he targeted her, deepening a mystery …","url":"https://kstp.com/news/granddad-wisconsin-girl-has-no-link-to-suspected-kidnapper-/5207911/","urlToImage":"https://kstp.com/kstpImages/repository/2019-01/800JakeThomasPattersonSuspectNewser2.jpeg","publishedAt":"2019-01-12T20:53:05Z","content":"Jayme had been missing for nearly three months Thursday when she approached a stranger near the small, isolated north woods town of Gordon and pleaded for help. Officers arrested 21-year-old Jake Thomas Patterson minutes later based on Jayme's description of … [+4624 chars]"},{"source":{"id":null,"name":"Vox.com"},"author":"Amanda Sakuma","title":"Megyn Kelly finalizes split with NBC - Vox.com","description":"The talk show host reportedly wanted to be the next Oprah. As she leaves NBC with a record of uncomfortable gaffes, it seems unlikely that will happen.","url":"https://www.vox.com/2019/1/12/18179908/megyn-kelly-nbc-split-oprah","urlToImage":"https://cdn.vox-cdn.com/thumbor/WiGGM5Fmnb_o5PN4zrXsPHnQRx4=/0x215:3000x1786/fit-in/1200x630/cdn.vox-cdn.com/uploads/chorus_asset/file/13321989/Kelly.jpg","publishedAt":"2019-01-12T19:57:29Z","content":"Megyn Kelly has officially split with NBC, taking the reminder of her $69 million contract with her, but leaving behind any aspiration of becoming the next queen of talk-show TV. The parties have resolved their differences, and Megyn Kelly is no longer an emp… [+3732 chars]"},
Upvotes: 0
Views: 310
Reputation: 746
Use json_decode to turn the json you've received into an array!
$result = json_decode($result);
print_r($result);
Upvotes: 1