Swader / diffbot-php-client

[Deprecated - Maintenance mode - use APIs directly please!] The official Diffbot client library
MIT License
53 stars 20 forks source link

Bad dates in articles cause exceptions when using \Carbon #57

Closed jonathantullett closed 6 years ago

jonathantullett commented 6 years ago

When using the getDate() method from an Article, if the field has an unparsable date and Carbon is being used, it throws an exception:

PHP Fatal error:  Uncaught Exception: DateTime::__construct(): Failed to parse time string (Robin Murray / / 28 · 03 · 2017
1508454066 0) at position 0 (R): The timezone could not be found in the database in lib/vendor/nesbot/carbon/src/Carbon/Carbon.php:291
1508454066 Stack trace:
1508454066 #0 lib/vendor/nesbot/carbon/src/Carbon/Carbon.php(291): DateTime->__construct('Robin Murray / ...', Object(DateTimeZone))
1508454066 #1 lib/vendor/swader/diffbot-php-client/src/Entity/Article.php(68): Carbon\Carbon->__construct('Robin Murray / ...', 'GMT')
1508454066 #2 /path/to/my/code.php(95): Swader\Diffbot\Entity\Article->getDate()

I have patched our code to catch the exception but feel this should be handled more gracefully within Article.php

An example URL where this came from is http://www.clashmusic.com/features/sorcerer-the-perpetual-transition-of-jordan-rakei where you can see the byline is ROBIN MURRAY FEATURES 20 · 10 · 2017 (perhaps an opportunity to improve the article processing algo too?)

Thank you!

Swader commented 6 years ago

Hmm, thanks for reporting, hand't thought of this. This is pretty easy to fix on the PHP side, but it wouldn't be in-sync with the rest of the system. Best would be to create a custom API that transforms the date into something normal, but then again, other pages will fail.

I'll fix the PHP code for now, setting a reminder, and will train this site for better date parsing on Diffbot's end in the meanwhile.

Swader commented 6 years ago

Date should return accurately now from that domain. Code fix soon.