The Beta Post

Passive income, personal finance and fintech.

How to get stock quotes using PHP and YQL

How to get stock quotes using PHP and YQL

Yahoo has a great technology called Yahoo! Query Language (abbr. YQL) which enables you to structure dynamic data from websites and perform queries on that data just as you would within a database. In this post I will illustrate how I use YQL to display stock quotes on this blog.


Please note that this guide requires some knowledge of PHP.

First off you will need to create an app over at the Yahoo developer network. As your app should be able to get stock quotes you will need to check the box Profiles (Social Directory) under API Permissions when you create the app.

They have written a thorough guide about how to do so. Be sure to follow every step until you have the access token given in step 4, we will need this later to get the market data we want.

Given that you now have an app, you can access the YQL framework and perform queries. To handle my YQL requests I use the YahooOAuth2.class.php.

By using this library, the code for getting market data is actually quite simple. The following example illustrate my code to get the latest stock quote for AT&T.

<?php

$yql_query 
'SELECT * FROM pm.finance WHERE symbol="T"'# AT&T

$url 'https://query.yahooapis.com/v1/yql?q=' url_encode($yql_euery);

$token ''# Your Token
$headers= array(
    
'Authorization: Bearer ' $token,
    
'Accept: application/json',
    
'Content-Type: application/json'
);

# Load the library
require_once('YahooOAuth2.class.php');
$oauth2client = new YahooOAuth2();

$response $oauth2client->fetch($url$postdata=''$auth=''$headers);
$json_response json_decode($response);
      
?>

The variable $json_response now contains the following information:

stdClass Object
(
    [query] => stdClass Object
        (
            [count] => 32
            [created] => 2017-10-29T13:25:06Z
            [lang] => en-US
            [results] => stdClass Object
                (
                    [quote] => Array
                        (
                            [0] => stdClass Object
                                (
                                    [symbol] => T
                                    [Name] => AT&T Inc.
                                    [Symbol] => T
                                    [Open] => 33.680000
                                    [DaysHigh] => 34.020000
                                    [DaysLow] => 33.540001
                                    [MarketCapitalization] => 208.576B
                                    [YearHigh] => 43.030000
                                    [YearLow] => 33.330000
                                    [Volume] => 52317302
                                    [AverageDailyVolume] => -
                                    [PERatio] => 15.963346
                                    [LastTradePriceOnly] => 33.970001
                                    [Change] => 0.290001
                                    [realtime_price] => 33.970001
                                    [realtime_change] => 0.290001
                                    [realtime_chg_percent] => 0.861048
                                    [eps_curr_year] => 2.128000
                                    [realtime_ts] => 10 27 2017 20:00:58 GMT
                                    [ts] => 10 27 2017 20:00:58 GMT
                                )
                        )
                )
        )
)


From the output you are able to get the data we are looking for: the latest known price and the timestamp (marked in yellow in the output above). I'm using this technique to get market data for all the stocks listed on my portfolio page.

I'll admit that if you're using Google Sheets, there are easier way to access market data. But as I want to display the data here on this blog, I find it better to access them using YQL.

But there is also another reason why I'm using YQL. As it enables you to structure data from remote websites, you can write a script which monitors any new information on dividend payouts. I'm using this to display the estimated dividend income the next 60 days. As this is automatically updated I not need to spend any time on visiting websites to get that information. YQL does it for me!

I will write another guide for accessing this information, but for now you can play around with the code above to get your market data.

Comments

Gravatar
REMONTnom
June 1st, 2018 23:40

Hello, let's be friends
Russian news We n we publish all of them fresh and important events USA, analytics experts. All evil in the world exists with the quiet tacit consent of the indifferent. No one provides us with incentives. We are Disinterested. We are building a civil society. The people are the bearer of sovereignty and the only source of power. No one can usurp power. Useful topics - Hot Events Oklahoma-city Finance

New-hampshire Business



TWITTER
FACEBOOK
GOOGLE+
trinidad-and-tobago

Gravatar
NEF6nom
June 2nd, 2018 17:31

USA news We n we publish all of them fresh and topical events USA, analytics experts. All negative on earth happens with the quiet tacit consent of the indifferent. No one provides us with incentives. We are Volunteers. We are building a civil society. The people are the bearer of sovereignty and the only source of power. No one can usurp power. Useful topics - USA and Russia San-francisco Finance


TWITTER
FACEBOOK
GOOGLE+
MalaysiaFinance

Leave a comment

Providing an email address is required but will not be available to the public. If you have a gravatar associated with that email it will be displayed next to your comment.