How to tweet an image using PHP from my website?

2019-04-11 12:05发布

I am trying to tweet an image using php. I have read the documentation and followed a some tutorials. I don't have any problem when sending a message; however, it does not work with images. I can not find where my mistake is, can anyone help?? I would deeply appreciate it.

 <?php
        $comments = $_POST['comment'];
        if (isset($_POST['Twitter']))

        {

    require_once('twitter/codebird-php/src/codebird.php');
    \Codebird\Codebird::setConsumerKey("xxxxxx","xxxxxx");
    $cb = \Codebird\Codebird::getInstance();
    $cb->setToken("xxxxxx", "xxxxxxxxxx");

    $params = array(
    'status' => $comments,
    'media[]' => "/images/image1.jpg"
    );
    $reply = $cb->statuses_update($params);

    echo "You have posted your message succesfully";

    }

    else{

        echo "You havent posted anythingh";
    }
        ?>

标签: php twitter
1条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-04-11 12:22

You need to supply Twitter the fully qualified URI to your image.

You have the following code:

$params = array(
'status' => $comments,
'media[]' => "/images/image1.jpg"
);

Unfortunately, that's not a complete URL to an image, it's a relative URL. Instead, you need to provide something along the lines of this:

$params = array(
'status' => $comments,
'media[]' => "http://www.example.com/images/image1.jpg"
);

In addition, according to the Twitter API v1 documentation, you need to use statuses/update_with_media instead of statuses/update.

If you are using Twitter API v1, Twitter also recommends using v1.1 instead.

查看更多
登录 后发表回答