请帮助我,我在使用api v1.1将图片上传到Twitter时遇到问题 它在一个月前工作完美,但它现在不起作用。 我正在使用亚伯拉罕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.
)
)
)
我不知道那里缺少什么 请帮帮我
答案 0 :(得分:1)
Abraham twitteroauth与向媒体发送推文有关。你可以在这里找到Twitteroauth的固定版本:
使用此库,您必须致电:
$result = $tweet->post('statuses/update_with_media',array('media[]' => "@{$image};type={$file_type};filename={$file_temp}",'status' => 'test'), true);
最后一个参数true
表示您正在发送多部分请求。
答案 1 :(得分: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);
}
使用此修改后的库
答案 2 :(得分: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);
}