0

how can i get the meta description , title and image , from external site url, i have achieved this using php but i dont know how i use this it in yii controller, my code is

function file_get_contents_curl($url)
{
$ch = curl_init();

curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

$data = curl_exec($ch);
curl_close($ch);

return $data;
}

$html = file_get_contents_curl("http://www.example.com");

//parsing begins here:
$doc = new DOMDocument();
@$doc->loadHTML($html);
$nodes = $doc->getElementsByTagName('title');

//get and display what you need:
$title = $nodes->item(0)->nodeValue;

$metas = $doc->getElementsByTagName('meta');

for ($i = 0; $i < $metas->length; $i++)
{
$meta = $metas->item($i);
if($meta->getAttribute('name') == 'description')
    $description = $meta->getAttribute('content');
if($meta->getAttribute('name') == 'keywords')
    $keywords = $meta->getAttribute('content');
if($meta->getAttribute('language') == 'language');
    $language = $meta->getAttribute('language');
}

echo "Title: $title". '<br/><br/>';
echo "Description: $description". '<br/><br/>';
echo "Keywords: $keywords";

im new to yii , any help

4
  • 1
    you can do the same code in your controller and it will still work Apr 19, 2014 at 7:56
  • it will shows undefined variables err
    – Steve Bals
    Apr 19, 2014 at 8:58
  • so debug you code, where is a error
    – Alex
    Apr 19, 2014 at 15:13
  • you do your code in your controller/action and then pass the variables with the render function Apr 19, 2014 at 16:31

1 Answer 1

3

I used your code (with some minor edits) to create the following file. Save it in protected/components/HttpDetails.php (note: error handling not implemented - in case of http failure or other)

class HttpDetails {

  private static function file_get_contents_curl($url) {
    $ch = curl_init();

    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

    $data = curl_exec($ch);
    curl_close($ch);

    return $data;
  }

  public static function getDetails($url) {
    $html = self::file_get_contents_curl($url);

    //parsing begins here:
    $doc = new DOMDocument();
    @$doc->loadHTML($html);
    $nodes = $doc->getElementsByTagName('title');

    //get and display what you need:
    $title = $nodes->item(0)->nodeValue;

    $metas = $doc->getElementsByTagName('meta');

    for ($i = 0; $i < $metas->length; $i++) {
      $meta = $metas->item($i);
      if ($meta->getAttribute('name') == 'description')
        $description = $meta->getAttribute('content');
      if ($meta->getAttribute('name') == 'keywords')
        $keywords = $meta->getAttribute('content');
    }

    return array(
            'title'=>isset($title)?$title:'Not set',
            'description'=> isset($description)?$description:'Not set',
            'keywords'=> isset($keywords)?$keywords:'Not set',
    );

  }

}

Edit your import array in protected\config\main.php to include 'application.components.HttpDetails'

...
'import' => array(
    ...
    'application.components.HttpDetails',
),

To read the details from a page do the following in any controller (or elsewhere in your application)

$url = "www.cnn.com";
$details = HttpDetails::getDetails($url);

$title = $details['title'];
$description = $details['description'];
$keywords = $details['keywords'];

The above exact code has been tested and works fine. If you are getting errors, you should check your php environment for DOM / libxml extensions where your Yii is hosted.

2
  • any idea to get an images , in facebook we post any link , it will shows title , description with one image , is this possible to get an image
    – Steve Bals
    Apr 21, 2014 at 4:36
  • In your code you have done a search for 'meta' tags and iterated over it to get title and so on. Do the same for 'img' tags and use the 'src' attribute of the first element to get the link of the image. Your case might require you to check if the image is an external link or on the same site, in which case you need to iterate over 'img' tag elements until you find the one meeting your criteria. Further, you could enhance it by using the php gd library to check if the image in question meets size/format and other requirements if any! Good Luck!
    – nazim
    Apr 21, 2014 at 7:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.