Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Use YQL to get title and description from website
<?php
// Set root url, which point to yahoo api
$root = 'http://query.yahooapis.com/v1/public/yql?format=json&diagnostics=true&callback=cbfunc';
// Get title from google
$titleUrl = $root . '&q=' . urlencode("select content from html where url = 'http://google.com' and xpath = '//title'");
// Get description from google
$descUrl = $root . '&q=' . urlencode("select content from html where url='http://google.com' and xpath = '//meta' and name='description'");
// Process it
$c = curl_init();
curl_setopt($c, CURLOPT_URL, $titleUrl);
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($c, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($c, CURLOPT_SSL_VERIFYHOST, false);
$title = curl_exec($c);
curl_setopt($c, CURLOPT_URL, $descUrl);
$description = curl_exec($c);
curl_close($c);
var_dump($title);
var_dump($description);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment