我正在尝试向 Twitter 用户发送直接消息,但附有图片。我已经用代码 1 实现了这一点。但是要获得图像 ID,我必须发布一条推文(代码 2),而且我知道程序不是,有人可以帮助我举一个上传图像的正确方法的示例请不要将其作为推文发布到 Twitter。
代码 1:
<?php
session_start();
require "vendor/autoload.php";
use Abraham\TwitterOAuth\TwitterOAuth;
$consumer_key = 'Rh4UxxxxxxxxxxxIs7G';
$consumer_secret = 'fUw7mPjGkBnZKmxxxxxxxxxxxxxxxxbSHxynfkJGgz';
$oauth_token = '133xxxxxxxxxxxxxxxxx22593-xxxxxxxxxxxxxxGhpcVmrrlgNN23';
$oauth_token_secret = 'b6xyxxxxxxxxxxxxxxxgPh3QdSEBxKj1fdc';
$userId = '13367xxxxxx68';
$connection = new TwitterOAuth($consumer_key, $consumer_secret, $oauth_token, $oauth_token_secret);
$content = $connection->get("account/verify_credentials");
$connection = new TwitterOAuth($consumer_key, $consumer_secret, $oauth_token, $oauth_token_secret);
$data = [
'event' => [
'type' => 'message_create',
'message_create' => [
'target' => [
'recipient_id' => $userId
],
'message_data' => [
'attachment' => [
'type' => 'media',
'media' => [
'id' => '1365060529463697410'
]
]
]
]
]
];
$result = $connection->post('direct_messages/events/new', $data, true); // Note the true
echo json_encode($result);
?>
代码 2:
<?PHP
require_once 'twitteroauth.php';
define("CONSUMER_KEY", "Rh4USPExxxxxxxxHLIs7G");
define("CONSUMER_SECRET", "fUw7mPjxxxxxxxxxxxxxxxxxxxxSHxynfkJGgz");
define("OAUTH_TOKEN", "13xxxxxxxxxxxxx3-DPAhGlvMQxxxxxxxxxxxxxxGhpcVmrrlgNN23");
define("OAUTH_SECRET", "b6xyCQxxxxxxxxxxxxxxxgPh3QdSEBxKj1fdc");
$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, OAUTH_TOKEN, OAUTH_SECRET);
$content = $connection->get('account/verify_credentials');
$image = 'walkswithme.png';
$status_message = 'Adjuntando Imagen a un Twt';
$status = $connection->upload('statuses/update_with_media', array('status' => null, 'media[]' => file_get_contents($image)));
echo json_encode($status);
?>
谢谢大家!!