PHP: How to expand/contract Tinyurls

2020-06-25 05:15发布

In PHP, how can I replicate the expand/contract feature for Tinyurls as on search.twitter.com?

标签: php
8条回答
爷的心禁止访问
2楼-- · 2020-06-25 05:24

The Solution here from @Pons solution, didn't work alone on my php7.3 server reslolving stackexchange URLs like https://stackoverflow.com/q/62317

This solved it:

public function doShortURLDecode($url) {
    $ch = @curl_init($url);
    @curl_setopt($ch, CURLOPT_HEADER, TRUE);
    @curl_setopt($ch, CURLOPT_NOBODY, TRUE);
    @curl_setopt($ch, CURLOPT_FOLLOWLOCATION, FALSE);
    @curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    $response = @curl_exec($ch);
    $cleanresponse= preg_replace('/[^A-Za-z0-9\- _,.:\n\/]/', '', $response);
    preg_match('/Location: (.*)[\n\r]/', $cleanresponse, $a);
    if (!isset($a[1])) return $url;
    return parse_url($url, PHP_URL_SCHEME).'://'.parse_url($url, PHP_URL_HOST).$a[1];
}
查看更多
Anthone
3楼-- · 2020-06-25 05:26

Another simple and easy way:

<?php
function getTinyUrl($url) {
return file_get_contents('http://tinyurl.com/api-create.php?url='.$url);
}
?>
查看更多
Ridiculous、
4楼-- · 2020-06-25 05:27

And here is how to contract an arbitrary URL using the TinyURL API. The general call pattern goes like this, it's a simple HTTP request with parameters:

http://tinyurl.com/api-create.php?url=http://insertyourstuffhere.com

This will return the corresponding TinyURL for http://insertyourstuffhere.com. In PHP, you can wrap this in an fsockopen() call or, for convenience, just use the file() function to retrieve it:

function make_tinyurl($longurl)
{
  return(implode('', file(
    'http://tinyurl.com/api-create.php?url='.urlencode($longurl))));
}

// make an example call
print(make_tinyurl('http://www.joelonsoftware.com/items/2008/09/15.html'));
查看更多
Lonely孤独者°
5楼-- · 2020-06-25 05:35

If you just want the location, then do a HEAD request instead of GET.

$tinyurl  = 'http://tinyurl.com/3fvbx8';
$context  = stream_context_create(array('http' => array('method' => 'HEAD')));
$response = file_get_contents($tinyurl, null, $context);

$location = '';
foreach ($http_response_header as $header) {
    if (strpos($header, 'Location:') === 0) {
        $location = trim(strrchr($header, ' '));
        break;
    }
}
echo $location;
// http://www.pingdom.com/reports/vb1395a6sww3/check_overview/?name=twitter.com%2Fhome
查看更多
祖国的老花朵
6楼-- · 2020-06-25 05:35

In PHP there is also a get_headers function that can be used to decode tiny urls.

查看更多
相关推荐>>
7楼-- · 2020-06-25 05:37

If you want to find out where a tinyurl is going, use fsockopen to get a connection to tinyurl.com on port 80, and send it an HTTP request like this

GET /dmsfm HTTP/1.0
Host: tinyurl.com

The response you get back will look like

HTTP/1.0 301 Moved Permanently
Connection: close
X-Powered-By: PHP/5.2.6
Location: http://en.wikipedia.org/wiki/TinyURL
Content-type: text/html
Content-Length: 0
Date: Mon, 15 Sep 2008 12:29:04 GMT
Server: TinyURL/1.6

example code...

<?php
$tinyurl="dmsfm";

$fp = fsockopen("tinyurl.com", 80, $errno, $errstr, 30);
if (!$fp) {
    echo "$errstr ($errno)<br />\n";
} else {
    $out = "GET /$tinyurl HTTP/1.0\r\n";
    $out .= "Host: tinyurl.com\r\n";
    $out .= "Connection: Close\r\n\r\n";

    $response="";

    fwrite($fp, $out);
    while (!feof($fp)) {
        $response.=fgets($fp, 128);
    }
    fclose($fp);

    //now parse the Location: header out of the response

}
?>
查看更多
登录 后发表回答