I’m working on a plug-in to grab Metacritic.com scores using cURL, but I’m getting an error when I try to get the URL from a parameter. It works if I hardcode the URL, but not if I try to pass it via the url parameter of my plug-in. Here’s my code:
class Sb_metascore
{
var $return_data = "";
function Sb_metascore()
{
global $TMPL;
// $target_url = "http://www.metacritic.com/games/platforms/pc/imperiumromanum/"; // <- does work
$target_url = $TMPL->fetch_param('url'); // <- doesn't work
$userAgent = 'Googlebot/2.1 (http://www.googlebot.com/bot.html)';
// make the cURL request to $target_url
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
curl_setopt($ch, CURLOPT_URL,$target_url);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$html= curl_exec($ch);
if (!$html) {
echo "
cURL error number: " .curl_errno($ch);
echo "
cURL error: " . curl_error($ch);
exit;
}
// parse the html into a DOMDocument
$dom = new DOMDocument();
@$dom->loadHTML($html);
// grab the score
$xpath = new DOMXPath($dom);
$imgs = $xpath->evaluate("/html/body/div[2]/div[3]/table/tr[2]/td[2]/table[3]/tr/td[2]/table/tr/td/table/tr[2]/td[1]/img");
for ($i = 0; $i < $imgs->length; $i++) {
$img = $imgs->item($i);
$alt = $img->getAttribute('alt');
$score = preg_replace('/Metascore: /si', '', $alt);
//echo "$score";
return $this->return_data = $score;
}
}
// END FUNCTION
This is my first plug-in and I’m no PHP guru, so I’m not really sure what’s causing the problem. Any help would be much appreciated.
Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.