Extract original url from Google Alerts link

孤街醉人 提交于 2019-12-24 05:56:15

问题


I hope somebody can help me out with this little problem..?

I'm using Google Alerts to pull in breaking news stories to list on a website, unfortunately when I try to find the original url (prior to Google Alerts), all I get is a Google url as shown below;

http://www.google.com/url?sa=X&q=

http://www.source.com/2013/04/02/title.html

&ct=ga&cad=CAcQARgAIAAoATAAOABArOXtigVIAlAAWABiBWVuLVVT&cd=ZQHHhnCXS8w&usg=AFQjCNGGGZgSyC3KvMJUW0ICYsCtRZ2uJA

I've broken this url into the relevant sections to make it easier to follow, the 1st part is always exactly the same, however the 2nd & 3rd parts do change. The 3rd part however always starts with &ct= which I assume is part of a query..?

In the script I am using, this entire url is assigned as $link & what I would like to do if possible is to extract the original source url from the Google Alerts url, so that attribution goes where it is meant to go & not to the guy in the middle!

My php knowledge is very basic so any help on this would be greatly appreciated.

Thanks


回答1:


You can use this function which basically takes the starting URL, follows all the redirects and returns the last effective URL for it.

/**
 * Get target url from a redirect
 *
 * @param string $url Source url
 * @return string
 */

function getLastEffectiveUrl($url) {

    // initialize cURL
    $curl = curl_init($url);
    curl_setopt_array($curl, array(
        CURLOPT_RETURNTRANSFER  => true,
        CURLOPT_FOLLOWLOCATION  => true,
    ));

    // execute the request
    $result = curl_exec($curl);

    // fail if the request was not successful
    if ($result === false) {
        curl_close($curl);
        return null;
    }

    // extract the target url
    $redirectUrl = curl_getinfo($curl, CURLINFO_EFFECTIVE_URL);
    curl_close($curl);

        return $redirectUrl;
    }

The usage is straightforward. If we wanted to fetch the last effective URL for Mark Zuckerberg's profile image we would call the function like this:

    $lastEffectiveUrl = getLastEffectiveUrl('http://graph.facebook.com/4/picture');

The value of $lastEffectiveUrl after the call would be the expected:

    'http://profile.ak.fbcdn.net/hprofile-ak-snc4/157340_4_3955636_q.jpg';

ALL the credit is for the guy who wrote this post, I just did a little digging: Get the last effective URL from a series of redirects for the given URL



来源:https://stackoverflow.com/questions/15777513/extract-original-url-from-google-alerts-link

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!