如何使用oauth用php4更新twitter?

时间:2011-09-04 15:30:32

标签: twitter oauth php4

我坚持使用php4(特别是4.3.9)。我想通过一些PHP代码更新我的推特状态。我收到了一个消费者密钥和一个消费者密钥,我找到了一个很好的库,理论上让我用php4 [https://github.com/iamcal/lib_oauth]来使用oauth。

我可以完成第一次握手 - 我可以通过Twitter成功回复https://api.twitter.com/oauth/request_token的电话:

Array
(
[url] => https://api.twitter.com/oauth/request_token
[content_type] => text/html; charset=utf-8
[http_code] => 200
[header_size] => 1086
[request_size] => 375
[filetime] => -1
[ssl_verify_result] => 0
[redirect_count] => 0
[total_time] => 0.482927
[namelookup_time] => 0.022033
[connect_time] => 0.121786
[pretransfer_time] => 0.354956
[size_upload] => 0
[size_download] => 147
[speed_download] => 304
[speed_upload] => 0
[download_content_length] => 147
[upload_content_length] => 0
[starttransfer_time] => 0.482918
[redirect_time] => 0
)

cURL error number:0
cURL error:

但是当我尝试接下来的电话时,我得到了401。 任何建议都表示赞赏; oauth似乎打破了我的大脑:(

==更新9/5/11 =>这是我的代码==

function oauth_http_request($url, $method="POST", $postdata=null){

    echo("<br />=============");
    echo("URL=");
    echo("<br />");
    print_r($url);

    $ch = curl_init();

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));     // Get around error 417
    //curl_setopt($ch, CURLOPT_VERBOSE, true); // Display communication with server     
    curl_setopt($ch, CURLOPT_HEADER, FALSE);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_TIMEOUT, $GLOBALS['oauth_http_timeout']);

    echo("<br />====");
    echo("POST DATA:");
    echo("<br />");
    print_r($postdata);

    if ($method == 'GET'){
        # nothing special for GETs
    } else if ($method == 'POST'){
        curl_setopt($ch, CURLOPT_POST, TRUE);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
    } else{
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
    }

    $response = curl_exec($ch);
    $headers = curl_getinfo($ch);

    print "<pre>\n";
    print_r(curl_getinfo($ch));  // get error info
    echo "\n\ncURL error number:" .curl_errno($ch); // print error info
    echo "\n\ncURL error:" . curl_error($ch); 
    print "</pre>\n";

    echo("<br />=============");
    echo("HEADERS=");
    echo("<br />");
    print_r($headers);

    // Not getting a curl response

    echo("<br />=============");
    echo("RESPONSE=");
    echo("<br />");
    print_r($response);

    curl_close($ch);

    $GLOBALS['oauth_last_request'] = array(
        'request'   => array(
            'url'       => $url,
            'method'    => $method,
            'postdata'  => $postdata,
        ),
        'headers'   => $headers,
        'body'      => $response,
    );

    #if ($headers['http_code'] != "200"){
        #return '';
    #}

    return $response;
}

然后返回:

=============URL=
https://api.twitter.com/oauth/access_token
====POST DATA:
oauth_version=1.0&oauth_nonce=e5b76ddf5e013fe85cd553dcc5120631&oauth_timestamp=1315259833&oauth_consumer_key=[REDACTED]&oauth_token=vUwvbMpeqGO99vlro4zujhN892wt6BNqE07vvbUYQ&oauth_signature_method=HMAC-SHA1&oauth_signature=Mvcekz1WZbCPwIl6whf7rZAr4nQ%3D

Array
(
[url] => https://api.twitter.com/oauth/access_token
[content_type] => text/html; charset=utf-8
[http_code] => 401
[header_size] => 1061
[request_size] => 424
[filetime] => -1
[ssl_verify_result] => 0
[redirect_count] => 0
[total_time] => 0.426447
[namelookup_time] => 3.4E-05
[connect_time] => 0.093783
[pretransfer_time] => 0.311701
[size_upload] => 0
[size_download] => 1
[speed_download] => 2
[speed_upload] => 0
[download_content_length] => 1
[upload_content_length] => 0
[starttransfer_time] => 0.426437
[redirect_time] => 0
)

cURL error number:0
cURL error:

0 个答案:

没有答案