3

请帮助我,我在使用 api v1.1 将图像上传到 Twitter 时遇到问题,一个月前它运行良好,但现在无法运行。我正在使用 Abraham twitteroauth https://github.com/abraham/twitteroauth 并且我使用 enctype="multipart/form-data" 作为表单标签中的属性,这是我的代码:

             require 'inc/twitter.class.php';   

             if(!empty($_FILES['media']['name'])){    // tweet with media
                  $file_name  = $_FILES['media']['name'];
                  $file_type  = $_FILES['media']['type'];
                  $file_size  = $_FILES['media']['size'];
                  $file_temp  = $_FILES['media']['tmp_name'];
                  $file_error = $_FILES['media']['error'];

                  $handle    = fopen($file_temp,'rb');
                  $image     = fread($handle,filesize($file_temp));
                  fclose($handle);
                  $tweet = new TwitterOAuth($YOUR_CONSUMER_KEY, $YOUR_CONSUMER_SECRET, $row_user_info['access_token_oauth_token'], $row_user_info['access_token_oauth_token_secret']);
                  $result = $tweet->post('statuses/update_with_media',array('media[]' => "@{$image};type={$file_type};filename={$file_temp}",'status' => 'test'));

                  echo '<pre>';
                  print_r($result);
                  echo '</pre>';

结果是:

stdClass Object
(
[errors] => Array
    (
        [0] => stdClass Object
            (
                [code] => 195
                [message] => Missing or invalid url parameter.
            )

    )
)

我不知道其中缺少什么,请帮助我

4

3 回答 3

1

与通过媒体发送推文有关的亚伯拉罕 twitteroauth 存在问题。你可以在这里找到固定版本的 Twitteroauth:

https://github.com/tomi-heiskanen/twitteroauth/blob/77795ff40e4ec914bab4604e7063fa70a27077d4/twitteroauth/twitteroauth.php

使用这个库你必须调用:

$result = $tweet->post('statuses/update_with_media',array('media[]' => "@{$image};type={$file_type};filename={$file_temp}",'status' => 'test'), true);

最后一个参数true表示您正在发送多部分请求。

于 2013-12-18T12:25:34.120 回答
1

这对我有用

---html---

<form action="" method="POST" enctype="multipart/form-data">
    <div>
      <label for="status">Tweet Text</label>
      <textarea type="text" name="status" rows="5" cols="60"></textarea>
      <br />

      <label for="image">Photo</label>
      <input type="file" name="image" />
      <br />
      <input type="submit" value="Submit" />
    </div>
  </form> 

---php---

if (!empty($_FILES)) {
      // we set the type and filename are set here as well
      $params = array(
        'media[]' => "@{$_FILES['image']['tmp_name']};type={$_FILES['image']['type']};filename={$_FILES['image']['name']}",
        'status'  => $_POST['status']
      );

      $dd = $twitteroauth->post('statuses/update_with_media',$params,true);

      var_dump($dd);
}

使用这个修改后的库

https://github.com/tomi-heiskanen/twitteroauth/blob/77795ff40e4ec914bab4604e7063fa70a27077d4/twitteroauth/twitteroauth.php

于 2014-01-04T01:26:53.873 回答
0

非常感谢,但我编辑了代码,现在可以正常工作了

if (!empty($_FILES)) {
    // we set the type and filename are set here as well
                    $handle    = fopen($_FILES['image']['tmp_name'],'rb');
                    $image     = fread($handle,filesize($_FILES['image']['tmp_name']));
                    fclose($handle);
    $params = array(
      'media[]' => "@{$image};type={$_FILES['image']['type']};filename={$_FILES['image']['name']}",
      'status'  => $_POST['status']
    );
    $twitteroauth = new TwitterOAuth($YOUR_CONSUMER_KEY, $YOUR_CONSUMER_SECRET, $row_user_info['access_token_oauth_token'], $row_user_info['access_token_oauth_token_secret']);
    $dd = $twitteroauth->post('statuses/update_with_media',$params,true);

    var_dump($dd);

}

于 2014-01-05T23:58:46.693 回答