Symfony\Component\DomCrawler\Crawler::attr PHP Method

attr() public method

Returns the attribute value of the first node of the list.
public attr ( string $attribute ) : string | null
$attribute string The attribute name
return string | null The attribute value or null if the attribute does not exist
    public function attr($attribute)
    {
        if (!$this->nodes) {
            throw new \InvalidArgumentException('The current node list is empty.');
        }
        $node = $this->getNode(0);
        return $node->hasAttribute($attribute) ? $node->getAttribute($attribute) : null;
    }

Usage Example

コード例 #1
0
ファイル: Plan.php プロジェクト: siad007/scryphp
 public function process()
 {
     $crawler = $this->client->request($this->plan['method'], $this->plan['uri']);
     if (isset($this->plan['selector'])) {
         $selection = $crawler->filter($this->plan['selector']);
     } elseif (isset($this->plan['xpath'])) {
         $selection = $crawler->filterXPath($this->plan['path']);
     }
     if ($this->plan['images']) {
         $images = $selection->filterXPath('//img');
         if (iterator_count($images) > 1) {
             foreach ($images as $image) {
                 $crawler = new Crawler($image);
                 $info = parse_url($this->plan['uri']);
                 $url = $info['scheme'] . '://' . $info['host'] . '/' . $crawler->attr('src');
                 if (strpos($crawler->attr('src'), 'http') === 0) {
                     $url = $info['scheme'] . '://' . $info['host'] . '/' . $this->plan['path'] . $crawler->attr('src');
                 }
                 copy($url, SCRYPHP_STORAGE_PATH_IMG . DIRECTORY_SEPARATOR . substr(strrchr($url, "/"), 1));
             }
         }
     }
     file_put_contents(SCRYPHP_STORAGE_PATH_TXT . DIRECTORY_SEPARATOR . time() . uniqid(time(), true) . '.txt', $selection->text());
     return $selection->text();
 }
All Usage Examples Of Symfony\Component\DomCrawler\Crawler::attr