2

いくつかの短い URL があります。それらから元の URL を取得するにはどうすればよいですか?

4

4 に答える 4

4

URL 短縮サービスはほとんどが単純なリダイレクタであるため、ロケーション ヘッダーを使用してブラウザに移動先を伝えます。

PHP 独自のget_headers()関数を使用して、適切なヘッダーを取得できます。

$headers = get_headers('http://shorten.ed/fbsfS' , true);
echo $headers['Location'];
于 2015-02-27T11:23:20.417 に答える
0

これには次のcurl関数を使用できます。

// The short url to expand
$url = 'http://goo.gl/fbsfS';

// Prepare a request for the given URL
$curl = curl_init($url);

// Set the needed options:
curl_setopt_array($curl, array(
    CURLOPT_NOBODY => TRUE,            // Don't ask for a body, we only need the headers
    CURLOPT_FOLLOWLOCATION => FALSE,   // Don't follow the 'Location:' header, if any
));

// Send the request (you should check the returned value for errors)
curl_exec($curl);

// Get information about the 'Location:' header (if any)
$location = curl_getinfo($curl, CURLINFO_REDIRECT_URL);

// This should print:
//    http://translate.google.com.ar/translate?hl=es&sl=en&u=http://goo.gl/lw9sU
echo($location);
于 2015-02-27T11:17:10.760 に答える