Created
November 3, 2012 07:00
-
-
Save tagroup/4006361 to your computer and use it in GitHub Desktop.
TradeKing PHP Streaming Quote API Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Very quick and dirty expansion on the TradeKing PHP example to allow you to handle and process streaming quotes | |
see more at http://thomasloughlin.com | |
enjoy at your own risk | |
*/ | |
date_default_timezone_set('America/Chicago'); | |
// Your keys/secrets for access | |
$consumer_key = 'uqM2342345234523452345234520NT'; | |
$consumer_secret = 'WA5MuYsIkcFGEASqbN6jjSUqwereroKxA'; | |
$access_token = '325234DSDDSFB7K91GrCRses83fF'; | |
$access_secret = 're345ws346dfE456436456FDF3'; | |
$symbols="AAPL,QQQ,MSFT,VXX"; | |
try { | |
// Setup an OAuth consumer | |
$oauth = new OAuth($consumer_key,$consumer_secret,OAUTH_SIG_METHOD_HMACSHA1,OAUTH_AUTH_TYPE_AUTHORIZATION); | |
$oauth->setToken($access_token,$access_secret); | |
$noonce=date('U') ; | |
$oauth->setNonce($noonce); | |
$signature= $oauth->generateSignature("GET", "https://stream.tradeking.com/v1/market/quotes.json?symbols=". $symbols);//https://stream.tradeking.com/v1/market/quotes.xml?symbols=AAPL,QQQ,MSFT"); | |
$fp = fsockopen("ssl://stream.tradeking.com",443, $errno, $errstr, 30); | |
if (!$fp) { | |
echo "$errstr ($errno)<br />\n"; | |
} else { | |
$out = "GET /v1/market/quotes.json?symbols=". $symbols ." HTTP/1.1\r\n"; | |
$out.="User-Agent: ThomasLoughlin.com/1.0\r\n"; | |
$out .= "Host: stream.tradeking.com\r\n"; | |
$out.="Accept: */*\r\n"; | |
$out.="Authorization: OAuth "; | |
$out .="oauth_consumer_key=".'"' .$consumer_key .'"' .","; | |
$out .="oauth_nonce=".'"' .$noonce .'"' .","; | |
$out .="oauth_signature=".'"' .urlencode($signature) .'"' .","; | |
$out .="oauth_signature_method=".'"' ."HMAC-SHA1" .'"' .","; | |
$out .="oauth_timestamp=".'"' .date('U') .'"' .","; | |
$out .="oauth_token=".'"' .$access_token .'"' .","; | |
$out .="oauth_version=".'"' ."1.0" .'"' ."\r\n"; | |
$out .= "Connection: Close\r\n\r\n"; | |
fwrite($fp, $out); | |
echo $out; | |
sleep(2); //Chill for a couple seconds because no one wants to be rushed | |
$temp_unfinished =""; //This will hold the unfinished chunks since we are reading 512 chars at a time | |
$start=false; | |
while (!feof($fp)) { | |
$temp = fgets($fp, 512); //read in whatever is ready | |
$temp=$temp_unfinished . $temp; //add the extra that was not parsed previously. | |
$temp_unfinished=""; //clear the var just to be careful - not needed but I did this fast | |
if($start==false) | |
{ | |
/* | |
The intent of this was to trash the header information and get started with | |
the first chunk (more effective when I was using a bigger buffer | |
*/ | |
$data=explode("\r\n\r\n", $temp, 2); | |
if(count($data)==2) | |
{ | |
$start=true; | |
$temp=$data[1]; | |
} | |
} | |
////////////////////// | |
// Since we sent HTTP/1.1, we have to handle the chunking | |
////////////////////// | |
$chunks=explode("\r\n",$temp); | |
$count=count($chunks); | |
for($i=0;$i<$count;$i++) | |
{ | |
if(ctype_xdigit($chunks[$i])) | |
{ | |
//If it looks hex, let's compare to the next length of the next message | |
if(hexdec($chunks[$i])==strlen($chunks[($i+1)])) | |
{ | |
//If it is the right size, we have a full chunk | |
$i++; | |
//handle chunk however you want. for now I am just printing | |
echo "This chunk can be handled:". $chunks[$i] . "\n------------------\n"; | |
$i++; | |
} | |
else | |
{ | |
// echo "Compare: " . hexdec($chunks[$i]) . " == " . strlen($chunks[($i+1)]) . "\n"; | |
//echo "Chunk: " . $chunks[$i] . " == " . $chunks[($i+1)] . "\n"; | |
//we don't have a full chunk so let's rebuild a string and bounce | |
$temp_unfinished=$chunks[$i]; | |
for($i=$i+1; $i<$count;$i++) | |
{ | |
$temp_unfinished.= "\r\n" . $chunks[$i]; | |
} | |
//for debug | |
// echo "\nUnfinished String: ". $temp_unfinished ."\n--------------\n"; | |
} | |
} | |
else | |
{ | |
//debugging | |
//echo "\nNOT HEX " . $temp ."\n"; | |
//$temp_unfinished=""; | |
} | |
} | |
} | |
} | |
fclose($fp); | |
} catch(OAuthException $E) { | |
// Display any errors | |
echo "Exception caught!\n"; | |
echo "Response: ". $E->lastResponse . "\n"; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I get this as a result of this program running.
HTTP/1.1 400 Bad Request
X-Powered-By: Express
Content-Type: text/plain
Connection: close
Transfer-Encoding: chunked
24
24
Server error: could not authenticate
0
I have changed the keys, etc to my own and yet I still get this.
I'm running this on php5 with php5-oauth on ubuntu.
Thanks in advance.