Tinyurl API 示例 - 我做得对吗:D

发布于 2024-08-17 18:30:25 字数 1275 浏览 4 评论 0原文

我们在应用程序中使用超长哈希值来注册新用户。问题是这些哈希值在某些电子邮件客户端中被破坏,导致链接无法使用。

我尝试通过一个简单的调用来实现 Tinyurl - API,但我认为有时会超时......有时邮件无法到达用户。

我更新了代码,但现在 URL 从未转换。 Tinyurl 真的这么慢还是我做错了什么? (我的意思是,嘿,5 秒在这个时代已经很了)

有人能给我推荐更可靠的服务吗?

都是我的错,忘记了 fopen 中的错误。但我将把这个代码示例留在这里,因为我经常看到这个示例,我认为它工作得不太可靠:

return file_get_contents('http://tinyurl.com/api-create.php?url='.$u);

这是 - 我认为完全有效的示例。我想听听有关改进的信息。

static function gettinyurl( $url ) {

    $context =
        stream_context_create(
            array(
                'http' => array(
                    'timeout' => 5  // 5 Seconds should be enough
                )
            )
        );

    // get tiny url via api-create.php
    $fp = fopen( 'http://tinyurl.com/api-create.php?url='.$url, 'r', $context); // open (read) api-create.php with long url as get parameter

    if( $fp ) { // check if open was ok
        $tinyurl = fgets( $fp ); // read response

        if( $tinyurl && !empty($tinyurl) ) // check if response is ok
            $url = $tinyurl; // set response as url

        fclose( $fp ); // close connection
    }

    // return
    return $url; // return (tiny) url

}

we use super-long Hashes for the Registration of new Users in our Application. The Problem is that these Hashes break in some Email Clients - making the Links unusable.

I tried implementing the Tinyurl - API, with a simple Call, but i think it times out sometimes ... sometimes the mail does not reach the user.

I updated the Code, but now the URL is never converted. Is Tinyurl really so slow or am i doing something wrong? (I mean hey, 5 Seconds is much in this Times)

Can anybody recommend me a more reliable service?

All my Fault, forgot a false in the fopen. But i will leave this sample of code here, because i often see this sample, wich i think does not work very reliable:

return file_get_contents('http://tinyurl.com/api-create.php?url='.$u);

This is the - i think fully working sample. I would like to hear about Improvements.

static function gettinyurl( $url ) {

    $context =
        stream_context_create(
            array(
                'http' => array(
                    'timeout' => 5  // 5 Seconds should be enough
                )
            )
        );

    // get tiny url via api-create.php
    $fp = fopen( 'http://tinyurl.com/api-create.php?url='.$url, 'r', $context); // open (read) api-create.php with long url as get parameter

    if( $fp ) { // check if open was ok
        $tinyurl = fgets( $fp ); // read response

        if( $tinyurl && !empty($tinyurl) ) // check if response is ok
            $url = $tinyurl; // set response as url

        fclose( $fp ); // close connection
    }

    // return
    return $url; // return (tiny) url

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

浮生面具三千个 2024-08-24 18:30:25

您可能需要使用 urlencode() 作为 url 参数。

还建议检查 fgets() 是否为 false。然后,您可以通过将响应与空字符串进行比较来保存 empty() 函数调用,例如:

$line = fgets($fp);

if ($line !== false && $line !== '') {
    // ...
}

通常,建议首先检查所有内容是否为 false,如果该函数返回不同类型的值,例如整数或布尔值。这一点至关重要,因为 0false 在比较中意味着相同。由于 PHP 缺乏类型安全性,强烈建议始终检查类型相等性。甚至在某些情况下,文档明确建议这样做,例如 strpos() 的情况。同时,我强迫自己使用 === 代替 ==,对于 `!=' 等也是如此。它需要更多的输入,但绝对值得付出努力因为你消除了可能的陷阱。

你的其余代码对我来说看起来不错。

You might want to use urlencode() for the url parameter.

It is also recommendable to check fgets() against false. Then, you could save the empty() function call by just comparing the response to an empty string, like:

$line = fgets($fp);

if ($line !== false && $line !== '') {
    // ...
}

Generally, it is advisable to check everything against false first, if the function returns values of different types such as integer or boolean. This can be crucial because 0 and false mean in comparisons the same. Because of PHP's lack for type safety, it is strongly recommended to always check for type equality. There are even cases when the documentation recommends this explicitly, e.g. in the case of strpos(). Meanwhile, I've forced myself to use === over to ==, same for `!=' etc. It requires more typing but it is definitely worth the effort because you eliminate possible pitfalls.

The rest of your code looks good to me.

眼眸印温柔 2024-08-24 18:30:25

您可以尝试bit.ly服务。据我所知,它得到了谷歌的支持。

Api

You can try bit.ly service. It supported by google as i know.

Api

安静 2024-08-24 18:30:25

我不知道您的哈希值到底有多长,但并非所有服务(浏览器、服务器等)都可以处理长度超过 255 个字符的 URL。你可以查看 php 的 Pack()< /a>

I don't know exactly how long your hash is, but not all services (browsers, servers etc) can handle URLs longer than 255 chars. You could look into php's Pack()

淡淡の花香 2024-08-24 18:30:25

如果您立即将其缩短为 7-8 个字符的tinyurl,那么使用“超长哈希”有什么意义呢?

没有人会费心去猜测长哈希,而是会破解tinyurl。

自己使用 10 个字符的哈希,比现在安全。

What is the point of using a "super-long hash", if you are immediately shortening it to a 7-8 character tinyurl?

Nobody would bother with guessing the long hash, and would crack the tinyurl instead.

Use a 10-character hash yourself and be more secure than you are now.

青丝拂面 2024-08-24 18:30:25

这是另一个版本:

function getTinyUrl($url)
{
    $ch = curl_init();
    $timeout = 5;
    curl_setopt($ch, CURLOPT_URL,'http://' . 'tinyurl.com/api-create.php?url=' . $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    $tinyUrl = curl_exec($ch);
    curl_close($ch);
    
    if ($tinyUrl === false) {
        throw new RuntimeException("Could not create URL");
    }
    
    return $tinyUrl;
}

我不得不拆分tinyurl url,因为这样不允许我发布答案。

Here's another version:

function getTinyUrl($url)
{
    $ch = curl_init();
    $timeout = 5;
    curl_setopt($ch, CURLOPT_URL,'http://' . 'tinyurl.com/api-create.php?url=' . $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    $tinyUrl = curl_exec($ch);
    curl_close($ch);
    
    if ($tinyUrl === false) {
        throw new RuntimeException("Could not create URL");
    }
    
    return $tinyUrl;
}

I had to split the tinyurl url because SO wouldn't let me post the answer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文