Home  >  Article  >  Backend Development  >  PHP Get QQ user nickname and online status example_PHP tutorial

PHP Get QQ user nickname and online status example_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:43:381091browse

If we use php to obtain QQ username and online status, QQ does not provide us with an API interface. If we want to obtain it, we can achieve it through QQ space or QQ web version chat.

QQ returns different pictures to indicate online or offline, and the icons also change accordingly

Since the images are different, the Content-Length in the returned HTTP header information must also be different, and the color image must be larger than the dark image of the same size, so find out the color and dark images of a certain style The intermediate value can be used to obtain the QQ online status by judging the return length of the header

The following is the code

The code is as follows Copy code
 代码如下 复制代码

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

?>

function get_qq_status($uin)
{
error_reporting(0);
$f=file_get_contents('http://wpa.qq.com/pa?p=1:'.$uin.':4');
if(!$f) return(true);
foreach($http_response_header as $val)
{
if(strpos($val,'Content-Length')!==false)
{
Return(intval(substr($val,16,50))>1000);
}
}
}
 代码如下 复制代码


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 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 );

?>

?>
The above is relatively simple, here is a better one
The code is as follows Copy code

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 If ( strncasecmp($field[$i], "Location:", 9) == 0 ) {
                  if ( strpos( $field[$i], "online") ) {
                   $ret = 1;
                } else if ( strpos( $field[$i], "offline") ) {
                   $ret = 0;
                                                                                                                      $ret = -1;
                 } // if
             break;
            } // if
} // for
         
Return( $ret );
}
/* }}} */

<🎜> <🎜>echo tphp_qq_online( 561272831 );<🎜> <🎜>?>

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

 代码如下
 代码如下 复制代码

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

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

http://www.bkjia.com/PHPjc/633157.htmlwww.bkjia.comtrue
http://www.bkjia.com/PHPjc/633157.html
TechArticle
如果我们利用php获取QQ用户名与在线状态QQ并未给我们提供api接口了,如果要获取我们可以通过QQ空间或QQ网页版聊天来实现。 QQ通过返回不同...
Statement:
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