/** * Downloads the target of the given link, image or other linking element * * This method will search for the given matching criteria (XPath), and * download whatever is in the href or src attribute of the given element * @param String $match XPath to element * @param String $filename Path to download file to * @return Browser Returns this browser object for chaining */ public function download($match, $filename) { $e = $this->_navigator->query($match); if (!$e || $e->length != 1) { throw new \Exception(intval($e->length) . " elements found matching: " . $match); } $e = $e->item(0); // If we don't have a linking attribute, we fail if (!$e->hasAttribute('src') && !$e->hasAttribute('href')) { throw new \Exception("No downloadable attribute found for element matching: " . $match); } // Resolve the linked resource $url = $this->_resolveUrl($e->hasAttribute('src') ? $e->getAttribute('src') : $e->getAttribute('href')); // Open a file handle, and download the file $fh = fopen($filename, 'w'); $this->_curl->fetch_into_file($url, $fh); fclose($fh); // Chain! return $this; }