Tradeking / Ally Invest API PHP的市场报价请求

时间:2017-11-13 03:42:23

标签: php api

我正在尝试使用Ally Invest / Tradeking PHP API来请求市场报价:https://www.ally.com/api/invest/documentation/market-ext-quotes-get-post/

使用文档,我安装了PHP OAuth并创建了以下代码,但是当我拨打电话时它似乎没有产生输出:

$consumer_key     = 'Z3fPyf181adB63Gq4gQZAjL1b7xgx7jLGA92DQ9jnuk4';
$consumer_secret  = 'feDtEmHTWoRb4rDMZkF6gvXj0xd2lP5vGDXPCUiZJww7';
$access_token     = '251cgNlYK21b6tqbqYSImAiqJKk3c51lHu4UOOF71ZI4';
$access_secret    = 'aLth5Imx0VgB67IgFhDxmW3QVYKPxdtzNMmjbiNWYXI7';

$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://api.tradeking.com/v1/market/ext/quotes.xml?symbols=fb");
$url = 'https://api.tradeking.com/v1/market/ext/quotes.xml?symbols=fb';
$header = [
    "GET /v1/market/ext/quotes.xml?symbols=fb HTTP/1.1 \n" .
    "Authorization: OAuth \n" .
    'oauth_consumer_key="'.$consumer_key.'"' . " \n" .
    'oauth_nonce="'.$noonce.'"' .  " \n" .
    'oauth_signature="'.$signature.'"' . " \n" .
    'oauth_signature_method="HMAC-SHA1"' . " \n" .
    'oauth_timestamp="'.time().'"' . " \n" .
    'oauth_token="'.$access_token.'"' . " \n" .
    'oauth_version="1.0"' . " \n" .
    'Connection: close' . " \n" .
    'Host: api.tradeking.com'

];
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPHEADER,$header);
$response = curl_exec($ch);

1 个答案:

答案 0 :(得分:0)

您不需要手动构建HTTP标头 - PHP的OAuth库将为您完成。这就是你所需要的:

$oauth = new OAuth($consumer_key,$consumer_secret,OAUTH_SIG_METHOD_HMACSHA1,OAUTH_AUTH_TYPE_AUTHORIZATION);
$oauth->setToken($access_token,$access_secret);
$url = 'https://api.tradeking.com/v1/market/ext/quotes.xml?symbols=fb';
$oauth->fetch($url);
print($oauth->getLastResponse());