Home php教程 php手册 php 获取qq用户昵称和在线状态实例

php 获取qq用户昵称和在线状态实例

Jun 02, 2016 am 09:14 AM
explode foreach include preg_match substr

如果我们利用php获取QQ用户名与在线状态QQ并未给我们提供api接口了,如果要获取我们可以通过QQ空间或QQ网页版聊天来实现。

QQ通过返回不同的图片,来表示在线或离线,图标也随之变换

既然图片不同,那么,返回的HTTP头信息中的Content-Length 也一定不同,而且,彩色图片一定会比同样子的暗色图片要大,于是,找出某个样式的彩色与暗色图片的中间值,就能达到通过判断头部返回长度的方法来获取QQ在线状态

以下是代码

<?php
function get_qq_status($uin) {
    error_reporting(0);
    $f = file_get_contents(&#39;http://wpa.qq.com/pa?p=1:&#39; . $uin . &#39;:4&#39;);
    if (!$f) return (true);
    foreach ($http_response_header as $val) {
        if (strpos($val, &#39;Content-Length&#39;) !== false) {
            return (intval(substr($val, 16, 50)) > 1000);
        }
    }
}
?>
Copy after login

上面比较简单,下面来个更好的

<?php
function tphp_qq_online($uin) {
    $reques = "GET /pa?p=1:" . $uin . ":1 HTTP/1.1rn";
    $reques.= "Host: wpa.qq.comrn";
    $reques.= "User-Agent: PHP_QQ_SPYrnrn";
    if (!($socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP))) return (-1);
    if (!(socket_connect($socket, "wpa.qq.com", 80))) return (-1);
    if (!(socket_write($socket, $reques))) return (-1);
    if (!($respon = socket_read($socket, 1024, PHP_BINARY_READ))) return (-1);;
    socket_close($socket);
    $field = explode("rn", $respon);
    for ($i = 0; $i < count($field); $i++) {
        if (strncasecmp($field[$i], "Location:", 9) == 0) {
            if (strpos($field[$i], "online")) {
                $ret = 1;
            } else if (strpos($field[$i], "offline")) {
                $ret = 0;
            } else {
                $ret = - 1;
            } // if
            break;
        } // if
        
    } // for
    return ($ret);
}
/* }}} */
echo tphp_qq_online(561272831);
?>
Copy after login

例,qq用户昵称和在线状态

<?php
//获取QQ状态
function getQQState($qq) {
    $url = &#39;http://wpa.qq.com/pa?p=2:&#39; . $qq . &#39;:41&r=&#39; . time();
    $headInfo = get_headers($url, 1);
    $length = $headInfo[&#39;Content-Length&#39;];
    if ($length == 1243) {
        return true;
    } else {
        return false;
    }
}
//获取QQ昵称
function getQQNick($qq) {
    $str = file_get_contents(&#39;http://r.qzone.qq.com/cgi-bin/user/cgi_personal_card?uin=&#39; . $qq);
    $pattern = &#39;/&#39; . preg_quote(&#39;"nickname":"&#39;, &#39;/&#39;) . &#39;(.*?)&#39; . preg_quote(&#39;",&#39;, &#39;/&#39;) . &#39;/i&#39;;
    preg_match($pattern, $str, $result);
    return $result[1];
}
//获取QQ姓名
function getQQName($qq) {
    //$qqArr = include &#39;friendArr.php&#39;;//预先设置的
    //$username = $qqArr[$qq];
    if (!$username) {
        $username = getQQNick($qq);
    }
    return $username;
}
?>
Copy after login


本文地址:

转载随意,但请附上文章地址:-)

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

What is the difference between using foreach and iterator to delete elements when traversing Java ArrayList? What is the difference between using foreach and iterator to delete elements when traversing Java ArrayList? Apr 27, 2023 pm 03:40 PM

1. The difference between Iterator and foreach is the polymorphic difference (the bottom layer of foreach is Iterator) Iterator is an interface type, it does not care about the type of collection or array; both for and foreach need to know the type of collection first, even the type of elements in the collection; 1. Why is it said that the bottom layer of foreach is the code written by Iterator: Decompiled code: 2. The difference between remove in foreach and iterator. First, look at the Alibaba Java Development Manual, but no error will be reported in case 1, and an error will be reported in case 2 (java. util.ConcurrentModificationException) first

How to determine the number of foreach loop in php How to determine the number of foreach loop in php Jul 10, 2023 pm 02:18 PM

​The steps for PHP to determine the number of the foreach loop: 1. Create an array of "$fruits"; 2. Create a counter variable "$counter" with an initial value of 0; 3. Use "foreach" to loop through the array, and Increase the value of the counter variable in the loop body, and then output each element and their index; 4. Output the value of the counter variable outside the "foreach" loop to confirm which element the loop reaches.

What is the difference between php include and include_once What is the difference between php include and include_once Mar 22, 2023 am 10:38 AM

When we write web pages using PHP, sometimes we need to include code from other PHP files in the current PHP file. At this time, you can use the include or include_once function to implement file inclusion. So, what is the difference between include and include_once?

PHP returns an array with key values ​​flipped PHP returns an array with key values ​​flipped Mar 21, 2024 pm 02:10 PM

This article will explain in detail how PHP returns an array after key value flipping. The editor thinks it is quite practical, so I share it with you as a reference. I hope you can gain something after reading this article. PHP Key Value Flip Array Key value flip is an operation on an array that swaps the keys and values ​​in the array to generate a new array with the original key as the value and the original value as the key. Implementation method In PHP, you can perform key-value flipping of an array through the following methods: array_flip() function: The array_flip() function is specially used for key-value flipping operations. It receives an array as argument and returns a new array with the keys and values ​​swapped. $original_array=[

How to use PHP explode function and solve errors How to use PHP explode function and solve errors Mar 10, 2024 am 09:18 AM

The explode function in PHP is a function used to split a string into an array. It is very common and flexible. In the process of using the explode function, we often encounter some errors and problems. This article will introduce the basic usage of the explode function and provide some methods to solve the error reports. 1. Basic usage of the explode function In PHP, the basic syntax of the explode function is as follows: explode(string$separator,string$stri

Split and merge strings using the explode and implode functions Split and merge strings using the explode and implode functions Jun 15, 2023 pm 08:42 PM

In PHP programming, processing strings is a frequently required operation. Among them, splitting and merging strings are two common requirements. In order to perform these operations more conveniently, PHP provides two very practical functions, namely the explode and implode functions. This article will introduce the usage of these two functions, as well as some practical skills. 1. The explode function The explode function is used to split a string according to the specified delimiter and return an array. Its function prototype is as follows: arra

PHP returns the ASCII value of the first character of the string PHP returns the ASCII value of the first character of the string Mar 21, 2024 am 11:01 AM

This article will explain in detail the ASCII value of the first character of the string returned by PHP. The editor thinks it is very practical, so I share it with you as a reference. I hope you can gain something after reading this article. PHP returns the ASCII value of the first character of a string Introduction In PHP, getting the ASCII value of the first character of a string is a common operation that involves basic knowledge of string processing and character encoding. ASCII values ​​are used to represent the numeric value of characters in computer systems and are critical for character comparison, data transmission and storage. The process of getting the ASCII value of the first character of a string involves the following steps: Get String: Determine the string for which you want to get the ASCII value. It can be a variable or a string constant

Common errors and solutions when using the explode function in PHP Common errors and solutions when using the explode function in PHP Mar 11, 2024 am 08:33 AM

Title: Common errors and solutions when using the explode function in PHP In PHP, the explode function is a common function used to split a string into an array. However, some common errors can occur due to improper use or incorrect data format. This article will analyze the problems you may encounter when using the explode function, and provide solutions and specific code examples. Mistake 1: The delimiter parameter is not passed in. When using the explode function, one of the most common mistakes is that the delimiter parameter is not passed in.

See all articles