如何使用PHP将图像发布到Twitter?

时间:2013-12-17 18:58:25

标签: php curl twitter

我试图通过PHP和cURL向Twitter发布带有图片的消息。它正在为消息工作,但我不知道如何将图像添加到消息中。

来自documentation

  

与POST状态/更新不同,此方法   期望原始多部分数据。您的POST请求的内容类型应该是   使用media []参数

设置为multipart / form-data

这是PHP:

<?php
class Tweet {
public $url = 'https://api.twitter.com/1.1/statuses/update_with_media.json';

function the_nonce(){
    $nonce = base64_encode(uniqid());
    $nonce = preg_replace('~[\W]~','',$nonce);
    return $nonce;
}

function get_DST($status){

    $url = $this->url;

    $consumer_key = "[removed]";
    $nonce = $this->the_nonce();
    $sig_method = 'HMAC-SHA1';
    $timestamp = time();
    $version = "1.0";
   $token = "[removed]";
    $access_secret = "[removed]";
    $consumer_secret =  "[removed]";

    $param_string = 'oauth_consumer_key='.$consumer_key.
            '&oauth_nonce='.$nonce.
            '&oauth_signature_method='.$sig_method.
            '&oauth_timestamp='.$timestamp.
            '&oauth_token='.$token.
            '&oauth_version='.$version.
            '&status='.rawurlencode($status);
    $sig_base_string = 'POST&'.rawurlencode($url).'&'.rawurlencode($param_string);
    $sig_key = rawurlencode($consumer_secret).'&'.rawurlencode($access_secret);

    $tweet_sig = base64_encode(hash_hmac('sha1', $sig_base_string, $sig_key, true));

    $DST = 'OAuth oauth_consumer_key="'.rawurlencode($consumer_key).'",'.
        'oauth_nonce="'.rawurlencode($nonce).'",'.
        'oauth_signature="'.rawurlencode($tweet_sig).'",'.
        'oauth_signature_method="HMAC-SHA1",'.
        'oauth_timestamp="'.rawurlencode($timestamp).'",'.
        'oauth_token="'.rawurlencode($token).'",'.
        'oauth_version="1.0"';
    return $DST;
}

function set($status){
$url = $this->url;
$ch = curl_init();

curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: ' . $this->get_DST($status)));
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'status='.rawurlencode($status));
curl_setopt($ch, CURLOPT_URL, $url);
$result = json_decode(curl_exec($ch));
$resultString =  print_r($result,true);;
curl_close($ch);
}

$status->set("hello");
?>

如何将 media [] 部分添加到其中?

2 个答案:

答案 0 :(得分:0)

不确定Matt Harris'库是否适合您,但代码相当简单

require_once ('your_app_tokens.php');
require_once ('tmhOAuth.php');

$connection = new tmhOAuth(array(
  'consumer_key'    => $consumer_key,
  'consumer_secret' => $consumer_secret,
  'user_token'      => $user_token,  //stored in session or whatever else
  'user_secret'     => $User_secret
));
$message ="test";
    $image = "image.jpg"; // must be in the same directory
    $extension = "jpg";
        $post = $connection->request(
          'POST',
         'https://api.twitter.com/1.1/statuses/update_with_media.json',
          array(
            'media[]'  => "@{$image};type=image/{$extension};filename={$image}",
            'status'   => $message
          ),


     true,
              true
            );
            $code = $connection->response;

//if the code is 200 it's all fine

答案 1 :(得分:0)

我已经创建了一个脚本,允许您发布包含图片的推文。我正在使用使用url将图像上传到服务器的php功能。之后,它会从您的服务器上传到Twitter服务器。您有一个图像ID,可用于发送带有图片的推文。

这是我的github projet。一切都在里面,易于使用和安装。

https://github.com/florianchapon/twitter-poster