如何将此api添加到wordpress中以自动工作


How can I add this api to wordpress to work automatic?

伙计们,我想把ouo.io的短链接添加到我的wordpress网站上。我想使用他们的API:

您也可以使用我们简单的API来缩短您的链接。以下链接将生成一个新的缩短链接并在空白页中打印出来,它是非常容易将此API注入到您的应用程序中。

http://ouo.io/api/WYTlzR4X?s=yourdestinationlink.com

我添加链接通过额外提交(op1)在帖子:

<a href="<?php echo get_sub_field('op1'); ?>" rel='nofollow' target="_blank" class="prv">

我想缩短这个链接,因为现在我使用它就像:

<a href="http://ouo.io/s/WYTlzR4X?s=<?php echo get_sub_field('op1'); ?>" rel='nofollow' target="_blank" class="prawyklik">

我得到的是:

http://ouo.io/s/WYTlzR4X?s=https://link-added-to-op1.com

我希望它是:

http://ouo.io/39pkesT

我想隐藏真实的链接并显示短路。请告诉我怎样才能这样工作?

只是为了补充Chay22的答案,您可能应该利用瞬态,这样您就不会不断地向URL缩短器API发送相同的请求。

// Get our sub field and create an md5 hash
$sub = get_sub_field('op1');
$md5 = md5( $sub );
// Check if the url has been shortened before, if it hasn't...
if ( false === ( $url = get_transient( 'short-' . $md5 ))) {
    // Grab the shortened url and save the transient for future use
    $url = file_get_contents( 'http://ouo.io/s/WYTlzR4X?s=' . $sub )
    set_transient( 'short-'. $md5, $url );
}
<a href="<?= $url ?>" rel="nofollow" target="_blank" class="prawyklik">

我确信使用file_get_contents 可以做到这一点

file_get_contents('http://ouo.io/s/WYTlzR4X?s=https://link-added-to-op1.com');

所以它应该像这个

<a href="<?php echo file_get_contents('http://ouo.io/s/WYTlzR4X?s='.get_sub_field('op1')); ?>" rel='nofollow' target="_blank" class="prawyklik">