mpratt / Embera

A Oembed consumer library, that gives you information about urls. It helps you replace urls to youtube or vimeo for example, with their html embed code. It has advanced features like offline support, responsive embeds and caching support.
MIT License
338 stars 59 forks source link

get extra info from url #13

Closed rakucmr closed 9 years ago

rakucmr commented 9 years ago

How can I get extra parameters from an url, like:

Thanks!

mpratt commented 9 years ago

Hi Raku,

It depends on the Oembed response. Some oembed providers give you the description, thumbnail url/width/height, others dont.

For example lets take a look this code:

$urls = array(
    'http://vimeo.com/47360546',
    'http://www.flickr.com/photos/bees/8597283706/in/photostream',
    'https://www.youtube.com/watch?v=L2VoJuVfbjI'
);

$config = array(
    'oembed' => true,
);
$embera = new \Embera\Embera($config);
print_r($embera->getUrlInfo($urls));

The vimeo response looks something like this

  Array
        (
            [embera_using_fake] => 0
            [type] => video
            [version] => 1.0
            [provider_name] => Vimeo
            [provider_url] => https://vimeo.com/
            [title] => Colombia - Timelapse
            [author_name] => MB Films
            [author_url] => http://vimeo.com/mbfilmsuk
            [is_plus] => 1
            [html] => .....
            [width] => 1280
            [height] => 720
            [duration] => 239
            [description] => ......
            [thumbnail_url] => http://i.vimeocdn.com/video/329159386_1280.jpg
            [thumbnail_width] => 1280
            [thumbnail_height] => 720
            [video_id] => 47360546
            [uri] => /videos/47360546
        )

As you can see, you get the description, thumbnail url, thumbnail dimensions amongst other stuff.. The youtube response on the other hand gives you:

    Array
        (
            [embera_using_fake] => 0
            [author_name] => Inside the Magic
            [height] => 270
            [html] => ....
            [type] => video
            [thumbnail_width] => 480
            [thumbnail_url] => http://i.ytimg.com/vi/L2VoJuVfbjI/hqdefault.jpg
            [provider_url] => http://www.youtube.com/
            [thumbnail_height] => 360
            [version] => 1.0
            [author_url] => http://www.youtube.com/user/InsideTheMagic
            [title] => FULL Marvel Phase 3 announcement with clips, Robert Downey Jr, Chris Evans
            [width] => 480
            [provider_name] => YouTube
        )

You can find the thumbnail url, thumbnail width/height and title, but NOT the description. Flickr on the other hand doesnt give you a title or a description.

Array
        (
            [embera_using_fake] => 0
            [type] => photo
            [title] => Durumu
            [author_name] => ‮‭‬bees‬
            [author_url] => https://www.flickr.com/photos/bees/
            [width] => 1024
            [height] => 723
            [url] => https://farm9.staticflickr.com/8385/8597283706_7b51ea50b1_b.jpg
            [web_page] => https://www.flickr.com/photos/bees/8597283706/
            [thumbnail_url] => https://farm9.staticflickr.com/8385/8597283706_7b51ea50b1_s.jpg
            [thumbnail_width] => 75
            [thumbnail_height] => 75
            [web_page_short_url] => https://flic.kr/p/e6HjVq
            [license] => All Rights Reserved
            [license_id] => 0
            [version] => 1.0
            [cache_age] => 3600
            [provider_name] => Flickr
            [provider_url] => https://www.flickr.com/
            [html] => .......
        )

So as you can see, it all depends on the provider.

Hope this helps!

EDIT: I forgot to say that you dont need to "extract" the urls into an array, you can pass text directly into the getUrlInfo() method. Remember that this method does returns an array! ***

$text = 'yadda yadda yadada, http://vimeo.com/47360546 yadda yadda yadda http://www.flickr.com/photos/bees/8597283706/in/photostream yadda yadda yadda https://www.youtube.com/watch?v=L2VoJuVfbjI';
$embera = new \Embera\Embera();
print_r($embera->getUrlInfo($text));
rakucmr commented 9 years ago

I've figured out that the problem was when I've use 'oembed' => false and there is a response from the provider, then you should return that response not a fake response.

mpratt commented 9 years ago

Hi Raku,

If you are using $config = array('oembed' => false) you are telling the library to use fake/offline responses, there is no way of getting a real response if you are telling the library to return fake/offline responses.

Fake responses are only useful when you want to get the html embed code of an Oembed resource. If you want only the "real stuff", dont pass the 'oembed' setting in the config array or at least set it to 'true'.

The behaviour is documented in the README, take a look at the Offline Support section.

.... Embera will first try to get the data from the oembed endpoint and if that fails it tries to return a fake response. This behaviour is useful when there are problems connecting with the oembed provider! This means that you get at least an html embedable code. When the oembed setting equals true the library gets the data directly from the oembed provider. If something goes wrong with the request, Embera skips the usage of fake responses .....

And regarding the getUrlInfo() method

When using the getUrlInfo() method, it is possible to see if the information from the provider came directly from the oembed endpoint or if it was generated by the offline feature. If you see in the response, the key embera_using_fake equal 0, means that the library got the results from the Oembed provider. When it equals 1, the html embed code was generated by the library.

Thanks for the report!

rakucmr commented 9 years ago

Can you include also the url in that array? For example lets say I have this url for a video: http://www.youtube.com/watch?v=ym9N2ccEShU, in resulted html I will have this url: http://www.youtube.com/embed/ym9N2ccEShU?feature=oembed. The last one I need it to be passed in the array

mpratt commented 9 years ago

Hi Raku,

I will look into it for later versions of the library, but I think that is too specific for your needs. You might want to use inheritance instead in order to provide that behaviour.

In fact, you only need to do something like this:

/**
 * Add url to the youtube stuff
 */
class MyCustomYoutubeProvider extends \Embera\Providers\Youtube
{
    protected function modifyResponse(array $response = array())
    {
        if (preg_match('~v=([a-z0-9_-]+)~i', $this->url, $matches)) {
            $response['custom_embed_url'] = 'http://www.youtube.com/embed/' . $matches['1'];
        }

        return $response;
    }
}

$urls = array(
    'http://youtube.com/watch?v=xVrJ8DxECbg&list=PLwnD0jwK0yymXOCl82nqdTdxe0ykVDcPW',
    'http://www.youtube.com/watch?v=WtPiGYsllos&index=1',
    'http://youtube.com/watch?v=mghhLqu31cQ',
    'http://youtube.com/embed/mghhLqu31cQ',
    'http://www.youtube.com/embed/mghhLqu31cQ',
);

$embera = new \Embera\Embera(array('oembed' => true));
$embera->addProvider('youtube.com', 'MyCustomYoutubeProvider');
$embera->addProvider('youtu.be', 'MyCustomYoutubeProvider');
print_r($embera->getUrlInfo($urls));

The response looks something like this

Array
(
    [http://youtube.com/watch?v=xVrJ8DxECbg&list=PLwnD0jwK0yymXOCl82nqdTdxe0ykVDcPW] => Array
        (
            ........
            ......
            [custom_embed_url] => http://www.youtube.com/embed/xVrJ8DxECbg
        )

    [http://www.youtube.com/watch?v=WtPiGYsllos&index=1] => Array
        (
            ........
            ......
            [custom_embed_url] => http://www.youtube.com/embed/WtPiGYsllos
        )

    [http://youtube.com/watch?v=mghhLqu31cQ] => Array
        (
            ........
            ......
            [custom_embed_url] => http://www.youtube.com/embed/mghhLqu31cQ
        )

    [http://youtube.com/embed/mghhLqu31cQ] => Array
        (
            ........
            ......
            [custom_embed_url] => http://www.youtube.com/embed/mghhLqu31cQ
        )

    [http://www.youtube.com/embed/mghhLqu31cQ] => Array
        (
            ........
            ......
            [custom_embed_url] => http://www.youtube.com/embed/mghhLqu31cQ
        )
)

If you want something more accurate, you might want to use something like this (untested)

if (preg_match('~src="([^"]+)"~i', $response['html'], $matches)) {
    $response['custom_embed_url'] = $matches['1'];
}

return $response;

Hope this helps!