Skip to content

Instantly share code, notes, and snippets.

@dmouse
Created November 17, 2012 01:14
Show Gist options
  • Save dmouse/4092383 to your computer and use it in GitHub Desktop.
Save dmouse/4092383 to your computer and use it in GitHub Desktop.
new implementation of https://gist.github.com/1250703
<?php
#
# Using PHP Web Scraper Goutte in a Console Task in a Silex project
#
# http://www.testically.org/2011/09/30/using-php-web-scraper-goutte-in-a-console-task-in-a-silex-project/
#
namespace Caefer;
use Silex\Application;
use Silex\ServiceProviderInterface;
use Goutte\Client;
class GoutteServiceProvider implements ServiceProviderInterface
{
public function register(Application $app)
{
$app['goutte.client'] = $app->share(function () use ($app) {
return new Client();
});
if (isset($app['goutte.class_path'])) {
$app['autoloader']->registerNamespaces(array(
'Goutte' => $app['goutte.class_path'],
'Zend' => $app['goutte.class_path'].'/../vendor/zend/library',
));
}
}
public function boot(Application $app)
{
}
}
<?php
#
# Autoload
#
require_once __DIR__.'/../src/goutte.phar';
require_once __DIR__.'/../vendor/autoload.php';
require_once __DIR__.'/../src/Caefer/GoutteServiceProvider.php';
#
# Init application
#
$app = new Silex\Application();
#
# Application
#
$app->register(new Caefer\GoutteServiceProvider(), array(
'goutte.class_path' => 'phar://'.__DIR__.'/goutte.phar/src',
));
$app->get('/', function ( ) use ($app) {
$client = $app['goutte.client'];
$crawler = $client->request('GET', 'http://cnn.com/');
$crawler -> filter ('h1') -> each(function ($node, $i) {
print $node->nodeValue."<br>";
});
return "";
});
#
# Debug application comment for not show errors
#
$app['debug'] = true;
#
# Run application
#
$app->run();
@wesamly
Copy link

wesamly commented Dec 28, 2013

cool gist, thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment