Skip to content

Instantly share code, notes, and snippets.

@dmouse
Created November 17, 2012 01:14
Show Gist options
  • Select an option

  • Save dmouse/4092383 to your computer and use it in GitHub Desktop.

Select an option

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
Copy Markdown

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