We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

How do I use an external library?

Hi guys, I have this external library called "simple_html_dom.php" but I don't know how to use it in my phalcon project.

I have this code:

<?php
    require 'simple_html_dom.php';

    $url = 'https://porrua.mx/busqueda/ebooks/9780545229937';
    $html=file_get_html($url); //Devuelve un array con contenido de la página

    $posts=$html->find('div[class=contenthover]');  
    foreach ($posts as $post) {
        $link=$post->find('h5',3);      
        $title=$link->innertext;
        echo $title,"\n";
    }

?>

Now I want to do the exact same thing in a Controller. But I don't know how to import and use the 'simple_html_dom.php' library inside Phalcon. I guess I just can't use the 'require' command. Can you please point me on how to use this library?

I'm still a beginner with Phalcon so I'm having trouble understanding all of these. Thank you so much for your help!

You can just use require command, no problem with that.

edited Apr '16

You can just use require command, no problem with that.

Yeah - just put that require statement in your bootstrap, then use the methods as you've shown in your example.

If you really wanted to, I suppose you could make a service out of it, then access the methods through the Dependency Injector. That's probably the method most inline with the MVC philosophy - but there is something to be said for just gettin' 'er done