We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

Working on a new plug-in but getting cURL error: Couldn't resolve host

Development and Programming

Eric Barstad's avatar
Eric Barstad
198 posts
16 years ago
Eric Barstad's avatar Eric Barstad

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.

       
Eric Barstad's avatar
Eric Barstad
198 posts
16 years ago
Eric Barstad's avatar Eric Barstad

Looking at the source of the error message, I’m seeing this:

http://www.metacritic.com/games/platforms/pc/callofdutyworldatwar

How do I prevent the “/” from being turned into “& #47;”?

       
Eric Barstad's avatar
Eric Barstad
198 posts
16 years ago
Eric Barstad's avatar Eric Barstad

Figured it out – preg_replace. Huzzah!

       
Derek Jones's avatar
Derek Jones
7,561 posts
16 years ago
Derek Jones's avatar Derek Jones

No need to use regex there, Eric, you can simply use str_replace() and EE’s SLASH constant to return forward slashes to a non-entitied state.

$url = str_replace(SLASH, '/', $TMPL->fetch_param('url'));
       
Eric Barstad's avatar
Eric Barstad
198 posts
16 years ago
Eric Barstad's avatar Eric Barstad

Cool, thanks Derek!

       

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.