1

I am trying to get stock prices from this api using curl

//Initialize cURL.
$ch = curl_init();
 
//Set the URL that you want to GET by using the CURLOPT_URL option.
curl_setopt($ch, CURLOPT_URL, 'https://cloud.iexapis.com/stable/stock/market/batch?symbols=aapl,msft&types=quote&filter=latestPrice&token=(redacted)');
 
//Set CURLOPT_RETURNTRANSFER so that the content is returned as a variable.
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
 
//Set CURLOPT_FOLLOWLOCATION to true to follow redirects.
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
 
//Execute the request.
$data = curl_exec($ch);
 
//Close the cURL handle.
curl_close($ch);
 
$stockdata = json_decode($data, true);

Right now I am getting this error: Notice: Undefined offset: 0

Here is the json I have converted into an array:

{"AAPL":{"quote":{"latestPrice":425.04}},"MSFT":{"quote":{"latestPrice":205.01}}}

Here is my code I am using to try and access the array, later I want to loop through all of the prices.

echo $stockdata[0]['quote']['latestPrice'];
5
  • 2
    echo $stockdata['AAPL']['quote']['latestPrice']; gives 425.04. Commented Aug 1, 2020 at 15:49
  • I want to try and cycle through all of the quotes I get using a loop later Commented Aug 1, 2020 at 15:55
  • Does this answer your question? How do I extract data from JSON with PHP? Commented Aug 1, 2020 at 15:55
  • Updated answer, when first key name is unknown. Commented Aug 1, 2020 at 16:01
  • Updated answer, lopping thru stocks. Commented Aug 1, 2020 at 16:17

1 Answer 1

2

When you use an associated array, it is not numeric indexed anymore and you need to access its key by name which is 'AAPL'.

echo $stockdata['AAPL']['quote']['latestPrice'];

prints out

425.04


Update 1

When you do not know the first key, just get it like this

echo $stockdata[array_keys($stockdata)[0]]['quote']['latestPrice'];

Explanation
array_keys($stockdata) will give you another array with all keynames as a numeric index based array. With [0] you access the first element (0th element) which is 'AAPL' in this example.


Update 2

Based on comments for looping through, you could do it like this

foreach($stockdata as $stock => $data) {
    echo "{$stock} => {$data['quote']['latestPrice']}\n";
}

AAPL => 425.04
MSFT => 205.01

Sign up to request clarification or add additional context in comments.

3 Comments

@IncredibleHat Did not see that comment during writing the anwer. Thanks for the hint. I've updated the answer.
Well, I actually meant foreach($stockdata as $stock => $info) { .. } so they can list out all the stock names with $stock and then access the info of it like $info['quote']['latestPrice']. Either way, its pretty easy to get at all the data from multiple angles, so its good to have multiple examples of such.
@IncredibleHat Ah, I see. Updated again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.