PHP:如何扩展/收缩TinyURL

PHP:如何扩展/收缩TinyURL,php,Php,在PHP中,如何在search.twitter.com上复制tinyurl的扩展/收缩功能?如果您想知道tinyurl的去向,请使用fsockopen在端口80上获得到tinyurl.com的连接,并向其发送如下HTTP请求 GET /dmsfm HTTP/1.0 Host: tinyurl.com 您得到的回复如下所示 HTTP/1.0 301 Moved Permanently Connection: close X-Powered-By: PHP/5.2.6 Location: http

在PHP中,如何在search.twitter.com上复制tinyurl的扩展/收缩功能?

如果您想知道tinyurl的去向,请使用fsockopen在端口80上获得到tinyurl.com的连接,并向其发送如下HTTP请求

GET /dmsfm HTTP/1.0
Host: tinyurl.com
您得到的回复如下所示

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
示例代码

<?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

}
?>

下面是如何使用TinyURL API压缩任意URL。一般的调用模式是这样的,它是一个带有参数的简单HTTP请求:

这将为返回相应的TinyURL。在PHP中,您可以将其包装在fsockopen()调用中,或者为了方便起见,只需使用file()函数检索它:

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'));

另一种简单易行的方法:

<?php
function getTinyUrl($url) {
return file_get_contents('http://tinyurl.com/api-create.php?url='.$url);
}
?>

当人们以编程方式回答如何创建和解决tinyurl.com重定向时,我想(强烈)提出一些建议:缓存

在twitter示例中,如果每次单击“展开”按钮时,它都会执行XmlHTTPRequest,例如,
/api/resolve\u tinyurl/http://tinyurl.com/abcd
,然后服务器创建了到tinyurl.com的HTTP连接,并检查了标题-这将破坏twitter和tinyurl的服务器

一种更加明智的方法是执行类似Python'y伪代码的操作

def resolve_tinyurl(url):
    key = md5( url.lower_case() )
    if cache.has_key(key)
        return cache[md5]
    else:
        resolved = query_tinyurl(url)
        cache[key] = resolved
        return resolved

其中,
缓存
的项目会神奇地备份到内存和/或文件中,而query_tinyurl()的工作方式与Paul Dixon的答案相同。

如果您只需要位置,则执行HEAD请求而不是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

下面是通过CURL库解码短URL的另一种方法:

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);
    preg_match('/Location: (.*)\n/', $response, $a);
    if (!isset($a[1])) return $url;
    return $a[1];
}
这是有描述的。

在PHP中,还有一个函数可以用来解码微小的URL。

来自@Pons解决方案的解决方案,并不是单独在我的php7.3服务器上重新发布stackexchange URL,比如
https://stackoverflow.com/q/62317

这就解决了它:

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];
}