Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
TradeKing PHP Streaming Quote API Example
Very quick and dirty expansion on the TradeKing PHP example to allow you to handle and process streaming quotes
see more at
enjoy at your own risk
// Your keys/secrets for access
$consumer_key = 'uqM2342345234523452345234520NT';
$consumer_secret = 'WA5MuYsIkcFGEASqbN6jjSUqwereroKxA';
$access_token = '325234DSDDSFB7K91GrCRses83fF';
$access_secret = 're345ws346dfE456436456FDF3';
try {
// Setup an OAuth consumer
$oauth = new OAuth($consumer_key,$consumer_secret,OAUTH_SIG_METHOD_HMACSHA1,OAUTH_AUTH_TYPE_AUTHORIZATION);
$noonce=date('U') ;
$signature= $oauth->generateSignature("GET", "". $symbols);//,QQQ,MSFT");
$fp = fsockopen("ssl://",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 .= "Host:\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
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
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);
// Since we sent HTTP/1.1, we have to handle the chunking
//If it looks hex, let's compare to the next length of the next message
//If it is the right size, we have a full chunk
//handle chunk however you want. for now I am just printing
echo "This chunk can be handled:". $chunks[$i] . "\n------------------\n";
// 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
for($i=$i+1; $i<$count;$i++)
$temp_unfinished.= "\r\n" . $chunks[$i];
//for debug
// echo "\nUnfinished String: ". $temp_unfinished ."\n--------------\n";
//echo "\nNOT HEX " . $temp ."\n";
} catch(OAuthException $E) {
// Display any errors
echo "Exception caught!\n";
echo "Response: ". $E->lastResponse . "\n";
Copy link

allen-zitting commented Aug 27, 2015

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


Server error: could not authenticate


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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment