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

最佳答案

如果要查找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

}
?>

关于php - PHP:如何扩展/收缩Tinyurls,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/62317/

10-10 19:04