Home > Backend Development > PHP Tutorial > Complete strategy for developing Weibo fan platform_PHP tutorial

Complete strategy for developing Weibo fan platform_PHP tutorial

WBOY
Release: 2016-07-13 10:29:51
Original
936 people have browsed it

The most convenient way to develop Weibo fan platform is to migrate from WeChat, but Weibo fan platform has made some changes to this.

The following is quoted from: http://open.weibo.com/wiki/%E5%BE%AE%E4%BF%A1%E5%BC%80%E5%8F%91%E8%80 %85%E8%BF%81%E7%A7%BB%E6%8C%87%E5%8D%97

WeChat Developer Migration Guide

If you have already developed the WeChat public platform before using the Weibo fan service platform, it is highly recommended to read this document to understand the details that need to be paid attention to during the migration process;


The Weibo fan service interface was designed with the migration issue of WeChat developers in mind, so the migration cost is not high; however, developers still need to make certain adaptations for the following three differences:

Difference 1: When applying for the message interface, fill in Token for WeChat and Appkey for Weibo

When applying for a messaging interface, Weibo requires developers to fill in the URL and Appkey, while WeChat requires developers to fill in the URL and Token;


WeChat Token is a character filled in arbitrarily by the developer and is only used to "verify the validity of the URL";

The Appkey of Weibo is provided by Weibo official. In addition to "verifying the validity of the URL", it is also used to represent the developer's identity;


In the "Verify URL Validity" link, the AppSecret corresponding to the developer's Appkey is used to generate a signature; in other words, Weibo's signature field encryption process is basically the same as WeChat's and is unique The difference is that WeChat uses the Token field for encryption, while Weibo uses the AppSecret field instead;

Difference 2: The access_token generation method is different and the validity period is also different

The way to obtain WeChat's access_token is to call the corresponding interface through the AppID and AppSecret of the official account;

The access_token certificate when calling Weibo’s fan service interface is automatically returned by the fan service platform after the URL is successfully verified when applying for the message interface (as shown below);

In addition, the access_token certificate for Weibo’s fan service interface can also be obtained through Weibo’s OAuth2.0 authorization interface, but this is currently under development and has not been launched yet;

Regarding the validity period, WeChat’s access_token is valid for 7200 seconds, and developers need to constantly refresh it; but the access_token certificate of Weibo’s fan service interface is valid for one year, and developers can find Blue V accounts after one year. Rebind or authorize;

So, if we make slight changes to the code of the WeChat platform, it can be used well on Weibo.

The source code is posted below:

<?php
 /**
 * Weibo 粉丝接口
 * @author caigen.li
 * @email it@exynoz.com
 * @date 2014-5-13  
 * 文档参考 
 * 1. http://open.weibo.com/wiki/%E5%BE%AE%E4%BF%A1%E5%BC%80%E5%8F%91%E8%80%85%E8%BF%81%E7%A7%BB%E6%8C%87%E5%8D%97
 * 2. http://open.weibo.com/wiki/%E6%B6%88%E6%81%AF%E6%8E%A8%E9%80%81%E6%9C%8D%E5%8A%A1#.E6.B6.88.E6.81.AF.E6.8E.A8.E9.80.81.E6.9C.8D.E5.8A.A1.E6.A6.82.E8.BF.B0
 */
 
// APPKEY
define("TOKEN", "XXXXXXXXXXXXXXXXX"); 
// APPSECRET
define("SECRET","XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");
// access_token,验证url成功后在开发者页面显示
define("ACTOKEN","XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX");

$wechatObj = new weiboChatApi();
$wechatObj->valid();

class weiboChatApi
{
	public function valid()
    {
        $echoStr = $_GET["echostr"]; 

        //valid signature , option
        if($this->checkSignature()){
        	echo $echoStr;
        	exit;
        }
    }

    public function responseMsg()
    {
		//get post data, May be due to the different environments
		$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];

      	//extract post data
		if (!empty($postStr)){
                
              	$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
                $fromUsername = $postObj->FromUserName;
                $toUsername = $postObj->ToUserName;
                $keyword = trim($postObj->Content);
                $time = time();
                $textTpl = "<xml>
							<ToUserName><![CDATA[%s]]></ToUserName>
							<FromUserName><![CDATA[%s]]></FromUserName>
							<CreateTime>%s</CreateTime>
							<MsgType><![CDATA[%s]]></MsgType>
							<Content><![CDATA[%s]]></Content>
							<FuncFlag>0</FuncFlag>
							</xml>";             
				if(!empty( $keyword ))
                {
              		$msgType = "text";
                	$contentStr = "Welcome to weibo world!";
                	$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
                	echo $resultStr;
                }else{
                	echo "Input something...";
                }

        }else {
        	echo "";
        	exit;
        }
    }
		
	private function checkSignature()
	{
        $signature = $_GET["signature"];
        $timestamp = $_GET["timestamp"];
        $nonce = $_GET["nonce"];	
        		
		$token = SECRET; // 与微信不同,用APP_SECRET验证
		$tmpArr = array($token, $timestamp, $nonce);
		sort($tmpArr, SORT_STRING);
		$tmpStr = implode( $tmpArr );
		$tmpStr = sha1( $tmpStr );
		
		if( $tmpStr == $signature ){
			return true;
		}else{
			return false;
		}
	}
}

?>
Copy after login

The APPKEY and APPSECRET mentioned above can be obtained after registering the application at http://open.weibo.com/apps.

-------------------------------------------------- ------------------------------------------ good lunk!

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/769760.htmlTechArticleThe most convenient way to develop Weibo fan platform is to migrate from WeChat, but Weibo fan platform has done this some changes. The following is quoted from: http://open.weibo.com/wiki/%E5%BE%AE%E4%BF%A...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template