These methods on theCrawlerare intended to initially populate yourCrawlerand aren't intended to
be used to further manipulate a DOM (though this is possible). However, since theCrawleris a set
ofDOMElement19objects, you can use any method or property available onDOMElement20,DOMNode21or DOMDocument22. For example, you could get the HTML of aCrawlerwith something like this:
1 2 3 4 5 $html= '';
foreach ($crawler as $domElement) {
$html .= $domElement->ownerDocument->saveHTML($domElement); }
Or you can get the HTML of the first node usinghtml()23:
$html =$crawler->html();
Links
To find a link by name (or a clickable image by itsaltattribute), use theselectLinkmethod on an existing crawler. This returns aCrawlerinstance with just the selected link(s). Callinglink()gives you a special
Link24object: 1 2 3 4 5
$linksCrawler= $crawler->selectLink('Go elsewhere...');
$link= $linksCrawler->link(); // or do this all at once
$link= $crawler->selectLink('Go elsewhere...')->link();
15. http://api.symfony.com/master/Symfony/Component/DomCrawler/Crawler.html#method_addHtmlContent 16. http://php.net/manual/en/class.domdocument.php 17. http://php.net/manual/en/class.domnodelist.php 18. http://php.net/manual/en/class.domnode.php 19. http://php.net/manual/en/class.domelement.php 20. http://php.net/manual/en/class.domelement.php 21. http://php.net/manual/en/class.domnode.php 22. http://php.net/manual/en/class.domdocument.php 23. http://api.symfony.com/master/Symfony/Component/DomCrawler/Crawler.html#method_html 24. http://api.symfony.com/master/Symfony/Component/DomCrawler/Link.html
Listing 48-24
Listing 48-25
Listing 48-26
Listing 48-27
Listing 48-28
TheLink25object has several useful methods to get more information about the selected link itself: // return the proper URI that can be used to make another request
$uri = $link->getUri();
ThegetUri()is especially useful as it cleans thehrefvalue and transforms it into how it should really
be processed. For example, for a link withhref="#foo", this would return the full URI of the current
page suffixed with#foo. The return fromgetUri()is always a full URI that you can act on.
Images
To find an image by itsaltattribute, use theselectImagemethod on an existing crawler. This returns a Crawlerinstance with just the selected image(s). Callingimage()gives you a specialImage26object:
1 2 3 4 5
$imagesCrawler= $crawler->selectImage('Kitten');
$image = $imagesCrawler->image(); // or do this all at once
$image = $crawler->selectImage('Kitten')->image();
TheImage27object has the samegetUri()method asLink28.
Forms
Special treatment is also given to forms. A selectButton() method is available on the Crawler which returns another Crawler that matches a button (input[type=submit],input[type=image], or abutton) with the given text. This method is especially useful because you can use it to return a Form29 object that represents the form that the button lives in:
1 2 3 4 5 6
$form= $crawler->selectButton('validate')->form(); // or "fill" the form fields with data
$form= $crawler->selectButton('validate')->form(array(
'name' =>'Ryan', ));
TheForm30object has lots of very useful methods for working with forms:
$uri = $form->getUri();
$method =$form->getMethod();
ThegetUri()31method does more than just return theactionattribute of the form. If the form method is GET, then it mimics the browser's behavior and returns theactionattribute followed by a query string of all of the form's values.
You can virtually set and get values on the form:
1 2 3 4 5 6
// set values on the form internally
$form->setValues(array(
'registration[username]' => 'symfonyfan', 'registration[terms]' => 1, )); 25. http://api.symfony.com/master/Symfony/Component/DomCrawler/Link.html 26. http://api.symfony.com/master/Symfony/Component/DomCrawler/Image.html 27. http://api.symfony.com/master/Symfony/Component/DomCrawler/Image.html 28. http://api.symfony.com/master/Symfony/Component/DomCrawler/Link.html 29. http://api.symfony.com/master/Symfony/Component/DomCrawler/Form.html 30. http://api.symfony.com/master/Symfony/Component/DomCrawler/Form.html 31. http://api.symfony.com/master/Symfony/Component/DomCrawler/Form.html#method_getUri
Listing 48-29 Listing 48-30 Listing 48-31 Listing 48-32 Listing 48-33 7 8 9 10 11 12
// get back an array of values - in the "flat" array like above
$values = $form->getValues();
// returns the values like PHP would see them, // where "registration" is its own array
$values = $form->getPhpValues(); To work with multi-dimensional fields:
1 2 3 4 5 <form>
<input name="multi[]" /> <input name="multi[]" />
<input name="multi[dimensional]" /> </form>
Pass an array of values:
1 2 3 4 5 6 7 8
// Set a single field
$form->setValues(array('multi' => array('value'))); // Set multiple fields at once
$form->setValues(array('multi' => array(
1 => 'value',
'dimensional' => 'an other value'
)));
This is great, but it gets better! TheForm object allows you to interact with your form like a browser, selecting radio values, ticking checkboxes, and uploading files:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
$form['registration[username]']->setValue('symfonyfan'); // check or uncheck a checkbox
$form['registration[terms]']->tick();
$form['registration[terms]']->untick(); // select an option
$form['registration[birthday][year]']->select(1984); // select many options from a "multiple" select
$form['registration[interests]']->select(array('symfony', 'cookies')); // even fake a file upload
$form['registration[photo]']->upload('/path/to/lucas.jpg');