新浪微博API开发简介之PHP基础篇-用户授权
现在玩微博的人越来越多了,而关于微博的第三方应用开发也越来越多,自己在偶然间开始接触了新浪微博API开发,新浪微博API开发的资源比较多,新浪微博提供了一个开发者的平台,网址是:http://open.weibo.com,它里面有很全面的新浪微博开发的资料,包括开发者的使用和介绍,各种语言的API函数介绍文档,SDK等多种资料。
自己在开发和学习的过程中,感觉虽然没有太大难度,但还是有一些问题是需要我们注意的,今天就我在开发和学习的过程中,简单的对利用PHP进行新浪微博API开发的内容进行一个整理和说明,
新浪微博API开发前的准备工作
首先到新浪微博开放平台下载基于PHP的SDK开发包,下载地址是:http://code.google.com/p/libweibo/downloads/detail?name=weibo-oauth-class-with-image-avatar-06-29.zip
下载完成后放到自己的开发环境中并解压,在其中也包含了demo演示程序,我们可以参考其样例程序进行编写。
新浪微博API开发最重要的用户授权过程
其实在开发过程中很多的问题都是集中在用户授权这个阶段,我开发的第三方应用,使用的是OAuth授权,关于OAuth授权的流程在新浪微博开放平台里有很清晰完整的介绍,我们可以到http://open.weibo.com/wiki/Oauth去查看,我这里从实例开发的角度进行介绍和说明。
1.首先获取未授权的Request Token
$o = new WeiboOAuth( WB_AKEY , WB_SKEY ); $keys = $o->getRequestToken(); //echo($keys['oauth_token'].' : '.$keys['oauth_token_secret']); |
我们需要在新浪微博开放平台中注册一个帐号,或直接使用我们的新浪微博帐号登录,进入我的应用,然后按照提示创建属于我们自己的第三方应用,创建完成之后我们可以得到两个授权的App Key和App Secret值,这两个值就是我们开发应用的关键。
得到授权值后,我们就可以利用上面的代码获得未授权的Request Token值了,它们会保存在$key数组变量中。
2.然后请求用户授权Token
$_SESSION['keys'] = $keys; $aurl = $o->getAuthorizeURL( $keys['oauth_token'] ,false , 'http://localhost/callback.php'); |
得到未授权的Request Token值后,我们就利用上面的代码可以开始准备去新浪微博授权页面进行授权,$aurl就是授权链接页面,我们得到$aurl后就可以利用 header()直接跳转到该授权页面,然后用户输入新浪微博帐号和密码进行授权,授权完成后,自动跳回你在最后一个参数里面设置的回调页面:http://localhost/callback.php,该链接你可以设置为上一个页面,这样授权完成之后就会自动又跳转回去了。
需要注意的是设置session的keys的值是必须的,它在下面获取到授权的Access Token中是需要用到的。很多的朋友可能会参考其开放平台上面的说明来进行授权时,可发现总是出错,一般都是这个问题,你并未设置session的 keys值,在下面当然取不到Access Token的值了,这个一定要记住了。
3.最后得到用户授权的Access Token
|
上面的代码就最终获得了用户授权的Access Token,共两个值,它们保存在$last_key数组变量里面,我们也可以看到,后面的两个参数就是前面我们设置的session值。到此就基本完成了,这就是新浪微博用户授权的一个完整的过程。
授权完成后的工作
在授权完成之后,我们就可以开始调用新浪微博提供的各类API函数接口进行实际应用的开发了,在这里我就获取最新微博记录这个接口进行一个简单说明,其他都类似。
获取最新新浪微博信息的API接口函数是:public_timeline(),样例代码看下面:
|
通常我们在得到用户授权的Access Token值之后,就把它们保存在我们的用户表中,与我们的应用中的帐号进行对应,之后我们在调用新浪微博各api接口时就不用每次都去认证了。
上面的代码很简单,实例化WeiboClient对象,然后直接调用接口函数public_timeline就可以得到返回的信息,如果没有错误的话。通常新浪微博api接口返回的数据格式一般为Json格式或xml格式,而我们在此是用php进行开发,则使用Json格式的数据就有先天的优势,如果返回Json格式数据的话,直接使用php函数json_decode()就可以转换为php常用的array数组格式了。
来源:红心草博客
原文地址:http://www.hongxincao.com/archives/579.html

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

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

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

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

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,

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

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

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 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.
