views:

103

answers:

1

Hello, I wrote the class Link which has a method shortTolong() this should return the real URL for a shortend 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 analize from some reason i get as a result the shorl url instaed 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 massage: "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 envoirment (no changes) i suspect it has somthing to do with FeedParser using curl too....

+1  A: 

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

Sabeen Malik
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
Sab - i guess i owe you an apologize :) though i doubted the trim() solution it worked out like a charm! - RESPECT!
Yaniv
i am glad it worked for u :)
Sabeen Malik