Created
November 17, 2012 01:14
-
-
Save dmouse/4092383 to your computer and use it in GitHub Desktop.
new implementation of https://gist.github.com/1250703
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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) | |
{ | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
cool gist, thank you.