0

I'm trying to use a finhub.io api to retrieve news and format it on my page. The documentation page offers a CURL address with an example of the return feed:

Finhub documentation page

The sample of the response to expect is:

[
{
"category": "technology",
"datetime": 1567054115,
"headline": "Facebook acknowledges flaw in Messenger Kids app",
"id": 25040,
"image": "https://s3.reutersmedia.net/resources/r/?m=02\u0026d=20190829\u0026t=2\u0026i=1423882334\u0026w=1200\u0026r=LYNXNPEF7S07O",
"related": "",
"source": "Reuters",
"summary": "Facebook Inc  acknowledged a flaw in its Messenger Kids app, weeks after two U.S. senators raised privacy concerns about the application, and said that it spoke to the U.S. Federal Trade Commission about the matter.",
"url": "https://www.reuters.com/article/us-facebook-privacy/facebook-acknowledges-flaw-in-messenger-kids-app-idUSKCN1VJ08X"
},{
"category": "top news",
"datetime": 1567053948,
"headline": "Swedish teen climate activist arrives in New York by boat for U.N. summit",
"id": 25060,
"image": "https://s3.reutersmedia.net/resources/r/?m=02\u0026d=20190828\u0026t=2\u0026i=1423796908\u0026w=1200\u0026r=LYNXNPEF7R24P",
"related": "",
"source": "Reuters",
"summary": "Teenage climate activist Greta Thunberg sailed into New York Harbor on Wednesday in a zero-carbon emissions vessel, completing her nearly 14-day journey from England to take part in a United Nations climate summit.",
"url": "https://www.reuters.com/article/us-global-climate-thunberg/swedish-teen-climate-activist-arrives-in-new-york-by-boat-for-u-n-summit-idUSKCN1VI1F1"
}]

I have managed to put together the code to process it which looks like this:

function news(){
$response = get_web_page("https://finnhub.io/api/v1/news?category=general&token=bqiarn7rh5rcatj3vgi0");
$resArr = array();
$resArr = json_decode($response);
//print_r($resArr);

echo "<pre>"; print_r($resArr); echo "</pre>";
}
function get_web_page($url) {
$options = array(
    CURLOPT_RETURNTRANSFER => true,   // return web page
    CURLOPT_HEADER         => false,  // don't return headers
    CURLOPT_FOLLOWLOCATION => true,   // follow redirects
    CURLOPT_MAXREDIRS      => 10,     // stop after 10 redirects
    CURLOPT_ENCODING       => "",     // handle compressed
    CURLOPT_USERAGENT      => "test", // name of client
    CURLOPT_AUTOREFERER    => true,   // set referrer on redirect
    CURLOPT_CONNECTTIMEOUT => 120,    // time-out on connect
    CURLOPT_TIMEOUT        => 120,    // time-out on response
); 

$ch = curl_init($url);
curl_setopt_array($ch, $options);

$content  = curl_exec($ch);

curl_close($ch);

return $content;
}

The output looks like this when I call the function news()

Array(
[0] => stdClass Object
    (
        [category] => company news
        [datetime] => 1588093445
        [headline] => UPDATE 3-Pfizer plans expanded coronavirus vaccine trials, sees 'negligible' hit from outbreak
        [id] => 3596983
        [image] => https://s4.reutersmedia.net/resources/r/?m=02&d=20200428&t=2&i=1516759737&w=1200&r=LYNXNPEG3R1VZ
        [related] => 
        [source] => Reuters
        [summary] => Pfizer Inc on Tuesday said it expects its experimental coronavirus vaccine to move into expanded clinical trials by October that could allow for emergency use or accelerated approval, as it ramps up efforts to combat the pandemic.
        [url] => https://www.reuters.com/article/us-pfizer-results/pfizer-plans-expanded-coronavirus-vaccine-trials-sees-negligible-hit-from-outbreak-idUSKCN22A1M0
    )
[1] => stdClass Object
    (
        [category] => top news
        [datetime] => 1588093312
        [headline] => Trump says will sign order on virus-related liability problems
        [id] => 3596993
        [image] => https://image.cnbcfm.com/api/v1/image/106509962-15880889172020-04-28t154424z_1743814945_rc2rdg9nmwqx_rtrmadp_0_health-coronavirus-usa.jpeg?v=1588089010
        [related] => 
        [source] => CNBC
        [summary] => U.S. President Donald Trump said on Tuesday he will sign an executive order later in the day that addresses employer liability issues that have arisen from the coronavirus outbreak.
        [url] => https://www.cnbc.com/2020/04/28/trump-says-will-sign-order-on-virus-related-liability-problems.html
    )
)

I'm now stuck on how to access each sub-associative array to format and use within a loop of the outer main array.

Can someone assist?

petworthnick
  • 205
  • 2
  • 11
  • Does this answer your question? [How can I access an array/object?](https://stackoverflow.com/questions/30680938/how-can-i-access-an-array-object) – Zak Apr 28 '20 at 17:52
  • If I adjust and add a loop to loop through the items in the main array such as this: for ($i = 0; $i < count($resArr); $i++) { echo $resArr[$i]['category']; echo $resArr[$i]['datetime']; } I get this error: Fatal error: Uncaught Error: Cannot use object of type stdClass as array. This is clear in the output code, but I'm not sure how and why it gets added. – petworthnick Apr 28 '20 at 18:02

0 Answers0