jublo / codebird-php

Easy access to the Twitter REST API, Direct Messages API, Account Activity API, TON (Object Nest) API and Twitter Ads API — all from one PHP library.
https://www.jublo.net/projects/codebird/php
GNU General Public License v3.0
776 stars 234 forks source link

Throw Exception on failed remote media download #152

Closed punkeel closed 8 years ago

punkeel commented 8 years ago

Hello,

I've received an error when trying to upload an image from an URL:

$status['media_ids'] = $cb->media_upload(
                array(
                    'media' => $imageUrl,
                )
            )->media_id_string;

Quite sure it worked before 3.0.

URL: https://epicube.fr/files/article/3853080-ragecraft.png (certificate by LetsEncrypt) Error from the API: unknown media type

Workaround: using the local file path instead.

Note: The same URL is used for the Facebook/Twitter thumbnail, so I don't think it comes from the image/webserver.

If you need more information, just tell me :) (I'll try to see if I find the origin)

Version used: 9d78f88e76f11b4595f2dc0f3a54ff3a9addbed6, PHP5

❯ php -v
PHP 5.6.14-0+deb8u1 (cli) (built: Oct  4 2015 16:13:10)
Copyright (c) 1997-2015 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2015 Zend Technologies
    with Zend OPcache v7.0.6-dev, Copyright (c) 1999-2015, by Zend Technologies

❯ php -m
[PHP Modules]
apc
apcu
bcmath
bz2
calendar
Core
ctype
curl
date
dba
dom
ereg
exif
fileinfo
filter
ftp
gd
geoip
gettext
hash
iconv
imagick
intl
json
libxml
mbstring
mcrypt
memcache
memcached
mhash
mongo
mysql
mysqli
mysqlnd
openssl
pcntl
pcre
PDO
pdo_mysql
Phar
posix
pspell
readline
redis
Reflection
session
shmop
SimpleXML
soap
sockets
SPL
standard
sysvmsg
sysvsem
sysvshm
tokenizer
wddx
xhprof
xml
xmlreader
xmlwriter
Zend OPcache
zip
zlib

[Zend Modules]
Zend OPcache
mynetx commented 8 years ago

@PunKeel First of all, thank you very much for your extensive report. This will help to find the reason for the failing media post. I’ll take a close look at what happens and get back to you afterwards.

mynetx commented 8 years ago

What does the following code show?

$reply = $cb->media_upload(
  array(
    'media' => $imageUrl
  )
);
var_dump($reply);
punkeel commented 8 years ago

With the URL given:

object(stdClass)#113 (4) {
  ["request"]=>
  string(22) "/1.1/media/upload.json"
  ["error"]=>
  string(24) "media type unrecognized."
  ["httpstatus"]=>
  int(400)
  ["rate"]=>
  object(stdClass)#111 (3) {
    ["limit"]=>
    string(3) "415"
    ["remaining"]=>
    string(3) "414"
    ["reset"]=>
    string(10) "1452110187"
  }
}

With relative file path:


object(stdClass)#113 (7) {
  ["media_id"]=>
  int(684810997559881728)
  ["media_id_string"]=>
  string(18) "684810997559881728"
  ["size"]=>
  int(310060)
  ["expires_after_secs"]=>
  int(86400)
  ["image"]=>
  object(stdClass)#111 (3) {
    ["image_type"]=>
    string(9) "image/png"
    ["w"]=>
    int(700)
    ["h"]=>
    int(400)
  }
  ["httpstatus"]=>
  int(200)
  ["rate"]=>
  object(stdClass)#153 (3) {
    ["limit"]=>
    string(3) "415"
    ["remaining"]=>
    string(3) "413"
    ["reset"]=>
    string(10) "1452110187"
  }
}
punkeel commented 8 years ago

... My bad. I var_dump'ed the downloaded file content (line 1802 in codebird.php), and ... 403 Forbidden.

I block everything with a "PHP" user agent ... Meaning Twitter/FB can still reach the webserver, but "codebird-php" can't

Sorry for the useless report (unless you turn it into a "throw exception if media download fails" ?)

mynetx commented 8 years ago

Maybe you’d like to add our user agent to the whitelist?

It’s codebird-php/3.0.0 +https://github.com/jublonet/codebird-php currently, but expect the version to increment, like 3.1.0-dev or 3.1.0-rc.1. This regex would match:

/^codebird-php\/[1-9]\d*\.\d+\.\d+(-([a-z]+\.[1-9]\d*|dev))? \+/
mynetx commented 8 years ago

@PunKeel Please test if failing remote media downloads now throw an Exception.

punkeel commented 8 years ago

Not tested but I don't think this would work, I put my var_dump right before that line: https://github.com/jublonet/codebird-php/blob/9c9a5206d9f32d1e6d5b4e94f6698e8799af2338/src/codebird.php#L1827

mynetx commented 8 years ago

@PunKeel I have amended the code and added unit tests for it, can you verify?

https://github.com/jublonet/codebird-php/blob/5682f3758ae865e8eec0ffc3aaf169e9f7062256/src/codebird.php#L1840

punkeel commented 8 years ago

Working fine, thanks!

mynetx commented 8 years ago

Great! This feature is going to be included in Codebird-PHP 3.1.0.

What about the User-Agent whitelisting?

punkeel commented 8 years ago

Disabled on my side, there's no real reason for it (legacy config to block bots) :)