将CSV文件上传到Twitter TON存储桶

时间:2015-11-15 10:29:49

标签: php api twitter oauth twitter-oauth

我正在尝试将数据上传到Twitter中的TON存储桶并将其另存为CSV。 它适用于twurl,但我需要使用PHP的curl来完成它。

这是我的代码,如果有人能看到问题。为什么我得到403回应。 我正在运行以下代码:

$this->http("https://ton.twitter.com/1.1/ton/bucket/ta_partner", "100000075");


function http($url, $postfields = NULL)
{
    $ci = curl_init();
    curl_setopt($ci, CURLOPT_RETURNTRANSFER, TRUE);
    $headers = array(
        'Content-Type: text/csv',
        'Content-Length: 0',
        'X-TON-Expires: '. date('D, d M Y H:i:s T', strtotime("+6 day")),
    );
    $oauth = array(
        'oauth_consumer_key' => $this->app_id,
        'oauth_nonce' => md5(mt_rand()), // random number
        'oauth_signature_method' => 'HMAC-SHA1',
        'oauth_timestamp' => time(),
        'oauth_token' => $this->access_token, // connection token
        'oauth_version' => '1.0',
    );

    $oauth = $this->_createSignature($oauth, $url);

    array_push($headers, $this->_buildAuthorizationHeader($oauth));
    curl_setopt($ci, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ci, CURLOPT_HEADER, true);
    curl_setopt($ci, CURLOPT_POST, TRUE);
    curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields);

    curl_setopt($ci, CURLOPT_URL, $url);
    $response = curl_exec($ci);
    $pieces  = explode("\\r\\r", $response);
    curl_close ($ci);
    return $response;
}

function _createSignature($oauth, $url)
{
    $composite_key = rawurlencode($this->app_secret) . '&' . rawurlencode($this->user_secret);
    $oauth_signature = base64_encode(hash_hmac('sha1', $this->_buildBaseString($url, 'POST', $oauth), $composite_key));
    $oauth['oauth_signature'] = $oauth_signature;
    return $oauth;
}

function _buildBaseString($baseURI, $method, &$params)
{
    $r = array();
    ksort($params);
    foreach($params as $key=>$value){
        $r[] = "$key=" . rawurlencode($value);
    }
    return $method."&" . rawurlencode($baseURI) . '&' . rawurlencode(implode('&', $r));
}

function _buildAuthorizationHeader($oauth)
{
    ksort($oauth);
    $r = 'Authorization: OAuth ';
    $values = array();
    foreach($oauth as $key=>$value)
        $values[] = $key. '=\"' . rawurlencode($value) . '\"';
    $r .= implode(', ', $values);
    return $r;
}

0 个答案:

没有答案