gpt4 book ai didi

PHP:如何扩展/收缩 Tinyurls

转载 作者:行者123 更新时间:2023-12-02 05:39:56 27 4
gpt4 key购买 nike

在 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com