Home Backend Development PHP Tutorial php使用新浪微博API开发用户授权功能

php使用新浪微博API开发用户授权功能

Jun 13, 2016 am 10:30 AM

新浪微博API开发的资源比较多,新浪微博提供了一个开发者的平台,它里面有很全面的新浪微博开发的资料,包括开发者的使用和介绍,各种语言的API函数介绍文档,SDK等多种资料。

首先,在新浪微博开放平台下载基于PHP的SDK开发包,将下载包放到开发环境中并解压,在其中也包含了demo演示程序,可以参考其样例程序进行编写。

新浪微博API开发最重要的用户授权过程

1.首先,获取未授权的Request Token 

<span style="font-family: 微软雅黑,Microsoft YaHei;"> $o = new WeiboOAuth( WB_AKEY , WB_SKEY ); <br/>$keys = $o->getRequestToken(); <br/>//echo($keys[&#39;oauth_token&#39;].&#39; : &#39;.$keys[&#39;oauth_token_secret&#39;]);</span>
Copy after login

需要在新浪微博开放平台中注册一个帐号,或直接使用新浪微博帐号登录,进入应用,然后按照提示创建属于自己的第三方应用,创建完成之后可以得到两个授权的App Key和App Secret值,这两个值就是开发应用的关键。
得到授权值后,就可以利用上面的代码获得未授权的Request Token值了,它们会保存在$key数组变量中。

2.请求用户授权Token

<span style="font-family: 微软雅黑,Microsoft YaHei;">$_SESSION[&#39;keys&#39;] = $keys; <br/>aurl = $o->getAuthorizeURL( $keys[&#39;oauth_token&#39;] ,false , &#39;http://localhost/callback.php&#39;);</span>
Copy after login

得到未授权的Request Token值后,就利用上面的代码可以开始准备去新浪微博授权页面进行授权,$aurl就是授权链接页面,得到$aurl后就可以利用header()直接跳转到该授权页面,然后用户输入新浪微博帐号和密码进行授权,授权完成后,自动跳回你在最后一个参数里面设置的回调页面:http://localhost/callback.php,该链接你可以设置为上一个页面,这样授权完成之后就会自动又跳转回去了。

注意:设置session的keys的值是必须的,它在下面获取到授权的Access Token中是需要用到的。很多的朋友可能会参考其开放平台上面的说明来进行授权时,可发现总是出错,一般都是这个问题,你并未设置session的keys值,在下面当然取不到Access Token的值了,这个一定要记住了。

3.得到用户授权的Access Token

<span style="font-family: 微软雅黑,Microsoft YaHei;"><?php<br/>$o = new WeiboOAuth( WB_AKEY , <br/>WB_SKEY , <br/>$_SESSION[&#39;keys&#39;][&#39;oauth_token&#39;] , <br/>$_SESSION[&#39;keys&#39;][&#39;oauth_token_secret&#39;] ); <br/>$last_key = $o->getAccessToken( $_REQUEST[&#39;oauth_verifier&#39;] ) ; <br/>echo($last_key[&#39;oauth_token&#39;]); </span>
Copy after login

以上代码,最终获得了用户授权的Access Token,共两个值,它们保存在$last_key数组变量里面,也可以看到,后面的两个参数就是前面设置的session值。到此就基本完成了,这就是新浪微博用户授权的一个完整的过程。

授权完成后的工作

在授权完成之后,就可以开始调用新浪微博提供的各类API函数接口进行实际应用的开发了,在这里对获取最新微博记录这个接口作下简单说明,其他都类似。
获取最新新浪微博信息的API接口函数是:public_timeline(),样例代码看下面:

<span style="font-family: 微软雅黑,Microsoft YaHei;"> <?php<br/>//获取前20条最新更新的公共微博消息 <br/>$c = new WeiboClient( WB_AKEY , <br/>WB_SKEY , <br/>$oauth_token , <br/>$oauth_token_secret ); <br/>$msg = $c->public_timeline(); <br/>if ($msg === false || $msg === null){ <br/>echo "Error occured"; <br/>return false; <br/>} <br/>if (isset($msg[&#39;error_code&#39;]) && isset($msg[&#39;error&#39;])){ <br/>echo (&#39;Error_code: &#39;.$msg[&#39;error_code&#39;].&#39;; Error: &#39;.$msg[&#39;error&#39;] ); <br/>return false; <br/>} <br/>print_r($msg);<br/></span>
Copy after login

通常在得到用户授权的Access Token值之后,就把它们保存在的用户表中,与的应用中的帐号进行对应,之后在调用新浪微博各api接口时就不用每次都去认证了。

实例化WeiboClient对象,然后直接调用接口函数public_timeline就可以得到返回的信息,如果没有错误的话。通常新浪微博api接口返回的数据格式一般为Json格式或xml格式,而在此是用php进行开发,则使用Json格式的数据就有先天的优势,如果返回Json格式数据的话,直接使用php函数json_decode()就可以转换为php常用的array数组格式了。

更多相关教程请访问   php编程从入门到精通全套视频教程

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

See all articles