使用OAuth更新twitter个人资料图片

时间:2010-04-29 21:36:20

标签: php curl twitter oauth multipartform-data

我正在尝试使用OAuth使twitter update_profile_image正常工作。我使用curl进行常规身份验证,一切正常,但我使用this library切换到OAuth,现在除了update_profile_image之外的所有内容都可以正常工作。

我读了一些关于twitter OAuth having problems with multipart data的内容,但那是不久前的the plugin is supposed to have dealt with that issue

我使用curl代码进行常规身份验证

$url      = 'http://api.twitter.com/1/account/update_profile_image.xml';
  $uname    = $_POST['username'];
  $pword    = $_POST['password'];
  $img_path = 'xxx';

  $userpwd  = $uname . ':' . $pword;
  $img_post = array('image' => '@' . $img_path . ';type=image/jpeg',
            'tile'  => 'true');
  $format = 'xml'; //alternative: json
  $message = 'Test update with a random num'.rand();

  $opts = array(CURLOPT_URL => $url,
          CURLOPT_FOLLOWLOCATION => true,
          CURLOPT_RETURNTRANSFER => true,
          CURLOPT_HEADER => true,
          CURLOPT_POST => true,
          CURLOPT_POSTFIELDS => $img_post,
          CURLOPT_HTTPAUTH => CURLAUTH_ANY,
          CURLOPT_USERPWD => $userpwd,
          CURLOPT_HTTPHEADER => array('Expect:'),
          CURLINFO_HEADER_OUT => true);

  $ch = curl_init();
  curl_setopt_array($ch, $opts);
  $response = curl_exec($ch);
  $err      = curl_error($ch);
  $info     = curl_getinfo($ch);
  curl_close($ch);

我当前的OAuth代码[我不得不将其删除,所以不要轻微查找语法错误]

include 'EpiCurl.php';
include 'EpiOAuth.php';
include 'EpiTwitter.php';
include 'secret.php';

$twitterObj = new EpiTwitter($consumer_key, $consumer_secret);

$twitterObj->setToken($_GET['oauth_token']);
$token = $twitterObj->getAccessToken();
$twitterObj->setToken($token->oauth_token, $token->oauth_token_secret);

try{
  $img_path = 'xxx';
  //$twitterObj->post_accountUpdate_profile_image(array('@image' => "@".$img_path));    

   $twitterObj->post('/account/update_profile_image.json', array('@image' => "@".$img_path));

   $twitterObj->post_statusesUpdate(array('status' => 'This is my new status:'.rand())); //This works
   $twitterInfo= $twitterObj->get_accountVerify_credentials();  
   echo $twitterInfo->responseText;
}catch(Exception $e){
    echo $e->getMessage();
  }

我一直试图弄清楚这一点,任何帮助都会非常感激。我与this library没有任何关系,所以请随意推荐其他人。

1 个答案:

答案 0 :(得分:1)

我使用的库的版本已过时。一旦我更新,我不得不处理其他一些问题,包括由于错误的服务器时间导致的401错误,现在一切正常。打印$ response-> responseText会有很大帮助。