【问题标题】:Google Link shortener with custom social media icons带有自定义社交媒体图标的 Google 链接缩短器
【发布时间】:2015-11-17 19:45:58
【问题描述】:

我希望将 Google 链接缩短器与 Wordpress 一起使用。我想使用更短的链接来帮助用户在社交媒体上发布更多内容,这太贵了。谷歌给出了巨大的津贴,希望使用。 API 密钥可用,但需要在 Wordpress 渲染页面之前使用 php 实现。

当前代码

<a rel="nofollow" href="http://www.facebook.com/sharer.php?u=<?php the_permalink();?>&amp;t=<?php echo urlencode(get_the_title($id)); ?>" title="Share this post on Facebook">
	<img src="<?php echo get_template_directory_uri(); ?>/images/facebook.png" border="0" alt="Facebook"/>
</a>
<a rel="nofollow" href="http://twitter.com/home?status=<?php echo urlencode("Currently reading: "); ?><?php the_permalink(); ?>" title="Share this article with your Twitter followers">
	<img src="<?php echo get_template_directory_uri(); ?>/images//twitter.png" border="0" alt="Twitter"/>
</a>
<a href="https://plus.google.com/share?url=<?php the_permalink(); ?>" title="Share this on Google+">
	<img src="<?php echo get_template_directory_uri(); ?>/images/google_plusone_share.png" border="0" alt="Google+"/>
</a>

总而言之,我需要一个示例,说明如何在使用内置函数时强制执行 Google 链接缩短 API,例如:

the_permalink();

非常感谢。

【问题讨论】:

  • 您需要使用add_filter('the_permalink','your_custom_function'); 来自定义永久链接的输出。
  • 感谢@TeeDeJee 的回复,您能举个例子吗?
  • 我会给你指导,但 stackoverflow 不是免费的编码服务。
  • 你不想挂钩the_permalink,否则每个使用它的内部页面链接都会通过谷歌路由,这显然是个坏主意。

标签: php wordpress google-api social-media


【解决方案1】:

你不想挂钩the_permalink,否则每个使用它的内部页面链接都将通过谷歌路由,这显然是个坏主意。

改为创建一个新函数来返回缩短的链接:

//wrapper function to echo the result, consistent with Wordpress the_* and get_the_* style
function the_google_short_link(){
    echo get_the_google_short_link();
}

function get_the_google_short_link(){
    // Google::shortlink is an example, replace with whatever code you have that creates the shortlink
    return Google::shortLink(get_permalink(get_the_ID())); 
}

然后需要在您的模板中使用:

<a rel="nofollow" href="http://www.facebook.com/sharer.php?u=<?php the_google_short_link();?>&amp;t=<?php echo urlencode(get_the_title($id)); ?>" title="Share this post on Facebook">
    <img src="<?php echo get_template_directory_uri(); ?>/images/facebook.png" border="0" alt="Facebook"/>
</a>

【讨论】:

  • 请问the_permalink是如何使用google的。我以为它使用的是 site_url 然后是页面的 slug?
  • @Neil 确实如此 - 请参阅所有重要的 Google::shortlink 方法调用。基本上,您将常规 url 传递给此函数,该函数返回短 url。我不知道您实际上是如何调用 google api 的,所以我以这个虚构的函数为例。您需要用您的实际工作代码替换它
【解决方案2】:

您需要使用add_filter('the_permalink','your_custom_function'); 来自定义永久链接的输出。

your_custom_function的大致轮廓:

function your_custom_function($url){
  // Get your token (if you use OAuth)

  // Send your long url with the [HTTP API][1]

  // return the short url you got.
}

另见Steve's Answer为什么不想通过the_permalink实现它

【讨论】:

  • 非常感谢,这部分我明白了。只是身份验证和处理 JSON 响应很棘手。感谢您的帖子。
【解决方案3】:

使用此类与 google api 和 goo.gl 缩短服务进行对话:

<?php

/**
* This file is part of googl-php
*
* https://github.com/sebi/googl-php
*
* googl-php is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

class mashsuGoogl
{
    public $extended;
    private $target;
    private $apiKey;
    private $ch;

    private static $buffer = array();

    function __construct($apiKey = null) {
        # Extended output mode
        $extended = false;

        # Set Google Shortener API target
        $this->target = 'https://www.googleapis.com/urlshortener/v1/url?';

        # Set API key if available
        if ( $apiKey != null ) {
            $this->apiKey = $apiKey;
            $this->target .= 'key='.$apiKey.'&';
        }

        # Initialize cURL
        $this->ch = curl_init();
        # Set our default target URL
        curl_setopt($this->ch, CURLOPT_URL, $this->target);
        # We don't want the return data to be directly outputted, so set RETURNTRANSFER to true
        curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, true);
    }

    public function shorten($url, $extended = false) {

        # Check buffer
        if ( !$extended && !$this->extended && !empty(self::$buffer[$url]) ) {
            return self::$buffer[$url];
        }

        # Payload
        $data = array( 'longUrl' => $url );
        $data_string = '{ "longUrl": "'.$url.'" }';

        # Set cURL options
        curl_setopt($this->ch, CURLOPT_POST, count($data));
        curl_setopt($this->ch, CURLOPT_POSTFIELDS, $data_string);
        curl_setopt($this->ch, CURLOPT_HTTPHEADER, Array('Content-Type: application/json'));

        if ( $extended || $this->extended) {
            return json_decode(curl_exec($this->ch));
        } else {

                        if (isset(json_decode(curl_exec($this->ch))->id)){
                            $ret = json_decode(curl_exec($this->ch))->id;
                            self::$buffer[$url] = $ret;
                            return $ret;
                        }
                        return '';

        }
    }

    public function expand($url, $extended = false) {
        # Set cURL options
        curl_setopt($this->ch, CURLOPT_HTTPGET, true);
        curl_setopt($this->ch, CURLOPT_URL, $this->target.'shortUrl='.$url);

        if ( $extended || $this->extended ) {
            return json_decode(curl_exec($this->ch));
        } else {
            return json_decode(curl_exec($this->ch))->longUrl;
        }
    }

        public function checkApiKey($url, $extended = true) {

        # Check buffer
        if ( !$extended && !$this->extended && !empty(self::$buffer[$url]) ) {
            return self::$buffer[$url];
        }

                # Payload
        $data = array( 'longUrl' => $url );
        $data_string = '{ "longUrl": "'.$url.'" }';

        # Set cURL options
        curl_setopt($this->ch, CURLOPT_POST, count($data));
        curl_setopt($this->ch, CURLOPT_POSTFIELDS, $data_string);
        curl_setopt($this->ch, CURLOPT_HTTPHEADER, Array('Content-Type: application/json'));

        if ( $extended || $this->extended) {
            return json_decode(curl_exec($this->ch), true);
        } else {
            $ret = json_decode(curl_exec($this->ch))->id;
            self::$buffer[$url] = $ret;
            return $ret;
        }
    }

    function __destruct() {
        # Close the curl handle
        curl_close($this->ch);
        # Nulling the curl handle
        $this->ch = null;
    }
}

?>   

这是一个返回 goo.gl 链接的简短函数

function GetShortURL( $url ){
    global $post;

    $appid = '1234567...'; // your app id


        $shorturl = new mashsuGoogl($appid);
        $shorturlres = $shorturl->shorten($url, false);

    // Shorten URL
    return $shorturlres;
    unset($googl);
    }

【讨论】:

  • 感谢@Rene 朝正确方向轻推,我会检查和测试!
  • 不客气,尼尔。请让我知道您何时成功,并将此答案标记为有帮助:-)
猜你喜欢
  • 2018-01-02
  • 1970-01-01
  • 2014-07-24
  • 1970-01-01
  • 2015-12-13
  • 2014-02-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多