0

I wrote the class Link which has a method shortTolong() this should return the real URL for a shortened url by returning the 'location' response header. i tested it and it works OK here is the code

public function shortTolong()
    {
        $urlMatch = array();
        $ch = curl_init();

        $options = array
        (
            CURLOPT_URL=>$this->getUrl(),
            CURLOPT_HEADER=>true,
            CURLOPT_RETURNTRANSFER=>true,
            CURLOPT_FOLLOWLOCATION=>false,
            CURLOPT_NOBODY=>true);
        curl_setopt_array($ch, $options);
        $server_output = curl_exec($ch);
        preg_match_all(LINK, $server_output,&$urlMatch,PREG_SET_ORDER);
        if($urlMatch)
        {
            foreach($urlMatch as $set)
            {
                $extracted_url = $set[2].'://'.$set[3];
            }
            return $extracted_url;
        }
        else
        {
            return $this->getUrl();
        }
    }

the problem starts when i try to use this method on other file which uses FeedParser to get feed entries that contain this short urls i ned to analyze from some reason i get as a result the short url instead of the long one here is the code:

foreach($parser->getItems() as $item)
{
    $idpreg = '/\d+/';
    preg_match_all($idpreg, $item['ID'],$statusid);
    $retweetid = ($statusid[0][1]);
    $datetime = $item['PUBLISHED'];
    $user = $item['AUTHOR']['NAME'];
    preg_match_all(LINK, $item['TITLE'], &$linkMatch);
    $final = $linkMatch[0][0];
    //if($linkMatch[0][0])
        echo '<p>';
        $link = new Link($final);
        echo $link->getUrl();
        echo '<br>';
        echo $link->shortTolong();
        echo '<br>';
        echo $user;
        echo '<br>';
        echo $retweetid;
        echo '</p>';


}

from some reason i get the same result for getUrl() and shortTolong() and i know for certain this is an error.

any ideas why this is happening? Thanks

Edit- I added an error notice to the method with curl_eror i get this error message: "Protocol http not supported or disabled in libcurl" as i said i tested this method from the and it's working fine as as stand alone in the same environment (no changes) i suspect it has something to do with FeedParser using curl too....

Falko
  • 17,076
  • 13
  • 60
  • 105
Yaniv Golan
  • 982
  • 5
  • 15
  • 28

1 Answers1

2

i think you should trim() the url and that should resolve the issue.

Sabeen Malik
  • 10,816
  • 4
  • 33
  • 50
  • ok so not sure ... ur edit "Protocol http not supported or disabled in libcurl" .. when does that come up? if it comes up when ur requesting from feed loop .. the chances are the url has some extra stuff around it which u cant see .. give it a try .. otherwise .. there is no reason for this to happen the way it is happening .. also its good to send explicit curl_close($ch); after the request is done – Sabeen Malik Oct 03 '09 at 23:59
  • Sab - i guess i owe you an apologize :) though i doubted the trim() solution it worked out like a charm! - RESPECT! – Yaniv Golan Oct 04 '09 at 00:02