如何使用PHP将图像发布到Twitter? [英] How to post an image to Twitter with PHP?

查看:180
本文介绍了如何使用PHP将图像发布到Twitter?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过PHP和cURL发布带有图像的消息到Twitter。它正在处理邮件,但我不知道如何添加图片。

I'm trying to post a message with an image to Twitter through PHP and cURL. It's working for the message but I don't know how to add the image to it.

文档


与POST状态不同/ update,这个方法
需要原始的多部分数据。您的POST请求的Content-Type应为
,并设置为media []参数的multipart / form-data

Unlike POST statuses/update, this method expects raw multipart data. Your POST request's Content-Type should be set to multipart/form-data with the media[] parameter

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 []

推荐答案

我创建了一个脚本,允许您发布带有图像的推文。我使用php fonction使用一个url上传图像到您的服务器。之后,它从您的服务器上传到twitter。您有一个图像ID,您可以使用它发送带有图片的推文。

i've created a script that allows you to post a tweet with image. I'm using the php fonction that used an url to upload the image to your server. After that, it upload from your server to the twitter one. You got an image ID that you can use to send a tweet with a picture.

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

Here is my github projet. Everything is inside, easy to used and install.

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

这篇关于如何使用PHP将图像发布到Twitter?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆