Home  >  Article  >  Backend Development  >  Tips on using PHP to connect to QQ interface to send messages

Tips on using PHP to connect to QQ interface to send messages

WBOY
WBOYOriginal
2023-07-05 15:49:551467browse

Tips of using PHP to connect to QQ interface to send messages

Introduction:
With the rapid development of social networks, people use various chat tools to communicate. As one of the most popular chat tools in China, QQ has opened some interfaces for developers to use. This article will introduce how to use PHP to connect to the QQ interface to send messages. We will discuss how to obtain the API key of the QQ interface, send text messages and send picture messages, and provide corresponding code examples.

Get the API key of the QQ interface:
First, we need to register an account on the QQ open platform and create an application. Log in to the QQ open platform and choose to create an application in the open platform console. Fill in the relevant information and get an App ID and App Key. This information will be used in subsequent code.

Send text messages:
To send text messages, we need to use QQ’s message push interface. The following is a sample code that demonstrates how to connect to the QQ interface and send text messages:

<?php
$appid = 'YOUR_APP_ID'; // 替换为你的App ID
$appkey = 'YOUR_APP_KEY'; // 替换为你的App Key

$userid = 'USER_ID'; // 替换为接收消息的用户ID
$content = '这是一条测试消息'; // 替换为要发送的消息内容

$url = 'https://api.q.qq.com/api/json/send_group_msg'; // QQ接口URL

$data = [
    'appid' => $appid,
    'appkey' => $appkey,
    'userid' => $userid,
    'content' => $content
];

$options = [
    'http' => [
        'header' => "Content-type: application/x-www-form-urlencoded
",
        'method' => 'POST',
        'content' => http_build_query($data)
    ]
];

$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);

echo $result;
?>

Send a picture message:
If you want to send a picture message, we need to first use QQ's picture upload interface to upload the picture to the server , and then send the URL of the image as a parameter to the message push interface. The following is a sample code that demonstrates how to connect to the QQ interface and send picture messages:

<?php
$appid = 'YOUR_APP_ID'; // 替换为你的App ID
$appkey = 'YOUR_APP_KEY'; // 替换为你的App Key

$userid = 'USER_ID'; // 替换为接收消息的用户ID
$content = '这是一张图片'; // 替换为要发送的消息内容
$imageUrl = 'https://example.com/image.jpg'; // 替换为图片的URL

$uploadUrl = 'https://api.q.qq.com/api/json/upload_image'; // QQ图片上传接口URL

$data = [
    'appid' => $appid,
    'appkey' => $appkey,
    'userid' => $userid,
    'content' => $content
];

$options = [
    'http' => [
        'header' => "Content-type: application/x-www-form-urlencoded
",
        'method' => 'POST',
        'content' => http_build_query($data)
    ]
];

$context = stream_context_create($options);
$result = file_get_contents($uploadUrl, false, $context);

$response = json_decode($result, true);

if ($response['ret'] == 0) {
    $imageUrl = $response['url'];

    $sendMessageUrl = 'https://api.q.qq.com/api/json/send_group_msg'; // QQ消息推送接口URL

    $data['image_url'] = $imageUrl;

    $options = [
        'http' => [
            'header' => "Content-type: application/x-www-form-urlencoded
",
            'method' => 'POST',
            'content' => http_build_query($data)
        ]
    ];

    $context = stream_context_create($options);
    $result = file_get_contents($sendMessageUrl, false, $context);

    echo $result;
} else {
    echo '上传图片失败';
}
?>

Summary:
This article introduces the skills of using PHP to connect to the QQ interface to send messages. We discussed how to obtain the API key for the QQ interface and provided code examples for sending text messages and sending picture messages. You can modify and adjust the code according to your needs to achieve more complex functions. I hope this article will be helpful to you, and I wish you good luck in using PHP to connect to the QQ interface!

The above is the detailed content of Tips on using PHP to connect to QQ interface to send messages. For more information, please follow other related articles on the PHP Chinese website!

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