PHP:如何扩展/收缩 Tinyurls

标签 php

在 PHP 中,如何复制 tinyurls 的扩展/收缩功能,就像在 search.twitter.com 上一样?

最佳答案

如果你想找出一个 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:如何扩展/收缩 Tinyurls,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62317/

相关文章:

java - PDFLib 在 PHP 中获取图层名称和句柄

curl - 当 curl 或 file_get_contents 请求 https url 时,php-fpm 崩溃

MySQL 查询的 PHP 引用

php - 从 PHP 连接到位于 Google Drive 中的 SQLite 数据库

javascript - 删除前的 Sweet Alert 确认

PHP 内部无法识别 JavaScript

php - mysql_query ("START TRANSACTION")- 超过锁定等待超时;尝试在 Codeigniter Mysql 中重新启动事务

javascript - 如何使用动态名称更新 Json 对象

PHP在mysqli执行后跳过while循环中的打印语句,但在循环后显示打印

PHPUnit错误 undefined variable