首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 網頁程式設計 >> PHP 程式設計 >> PHP 綜合知識

PHP 透過 ICQ 網關發送手機簡訊完整實作

     編輯:PHP 綜合知識

簡介

早期 ICQ 提供 Web SMS 服務,開發者可透過模擬瀏覽器登入並發送表單的方式,免費發送簡訊至手機。本文提供完整 PHP 實作代碼,包含登入取得 Cookie、組裝 multipart/form-data 請求、發送簡訊及結果判斷。

前置條件

  • 已在 ICQ SMS 登入頁 註冊並綁定手機號碼。
  • PHP 環境需支援 fsockopen、fputs、fgets 等 Socket 函數(免費虛擬主機通常限制此功能)。
  • 關閉 safe_mode 或具備足夠權限建立外部連線。

核心流程

  1. 登入取得 Cookie:POST https://web.icq.com/karma/dologin/1,,,00.html,攜帶 UIN、密碼、國家代碼等欄位,解析回應中的 Set-Cookie: KarmaLogin=...。
  2. 組裝發送請求:POST https://web.icq.com/sms/send_history/1,,,00.html,使用 multipart/form-data 格式,帶入 carrier、prefix、tophone、msg 等欄位,並在 Cookie 中攜帶步驟 1 取得的 KarmaLogin。
  3. 判斷結果:若回應包含 Moved Permanently 字串,視為發送成功。

完整 PHP 代碼

<?php
// 設定參數
$uin = '';          // ICQ 號碼
$passw = '';        // ICQ 密碼
$prefix = '';       // 國碼,如 886
$phonenumber = '';  // 手機號碼(不含國碼)
$message = 'Hello!'; // 簡訊內容

// ---------- 步驟 1:登入取得 Cookie ----------
$contentlength = 37 + strlen($uin) + strlen($passw);
$post = "POST https://web.icq.com/karma/dologin/1,,,00.html HTTP/1.0\r\n"
      . "Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-powerpoint, application/vnd.ms-excel, application/msword, */*\r\n"
      . "Referer: https://web.icq.com/sms/login/1,,,00.html\r\n"
      . "Accept-Language: en\r\n"
      . "Content-Type: application/x-www-form-urlencoded\r\n"
      . "Accept-Encoding: gzip, deflate\r\n"
      . "User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)\r\n"
      . "Host: web.icq.com\r\n"
      . "Content-Length: $contentlength\r\n"
      . "Proxy-Connection: Keep-Alive\r\n"
      . "Pragma: no-cache\r\n"
      . "Cookie: uin=$uin; sms_country=$prefix; KarmaService1=Yes\r\n\r\n"
      . "uService=1&uLogin=$uin&uPassword=$passw&x=0&y=0";

$remote = fsockopen('web.icq.com', 80, $errno, $errstr, 30);
if (!$remote) die("連線失敗: $errstr ($errno)");
fputs($remote, $post);
$htmlreply = '';
while (!feof($remote)) { $htmlreply .= fgets($remote, 128); }
fclose($remote);

// 解析 KarmaLogin Cookie
preg_match('/Set-Cookie:\s*KarmaLogin=([^;]+)/i', $htmlreply, $m);
$karmaLogin = $m[1] ?? '';
if (!$karmaLogin) die('登入失敗,無法取得 KarmaLogin');

// ---------- 步驟 2:發送簡訊 ----------
$contentprefix = (strlen($prefix) === 2) ? ' ' . $prefix : $prefix;
$charcount = 160 - strlen($message);
$contentlength = 1561 + strlen($message) + strlen($charcount) + strlen($phonenumber) + strlen($prefix);

$boundary = '---------------------------7d12442eab4';
$post = "POST https://web.icq.com/sms/send_history/1,,,00.html HTTP/1.0\r\n"
      . "Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-powerpoint, application/vnd.ms-excel, application/msword, */*\r\n"
      . "Referer: https://web.icq.com/sms/send_session/1,,,00.html?prefix=+$prefix&carrier=&tophone=$phonenumber\r\n"
      . "Accept-Language: en\r\n"
      . "Content-Type: multipart/form-data; boundary=$boundary\r\n"
      . "Accept-Encoding: gzip, deflate\r\n"
      . "User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)\r\n"
      . "Host: web.icq.com\r\n"
      . "Content-Length: $contentlength\r\n"
      . "Proxy-Connection: Keep-Alive\r\n"
      . "Pragma: no-cache\r\n"
      . "Cookie: uin=$uin; sms_country=$prefix; KarmaService1=Yes; KarmaLogin=$karmaLogin\r\n\r\n"
      . "--$boundary\r\n"
      . "Content-Disposition: form-data; name=\"carrier\"\r\n\r\n\r\n"
      . "--$boundary\r\n"
      . "Content-Disposition: form-data; name=\"prefix\"\r\n\r\n$contentprefix\r\n"
      . "--$boundary\r\n"
      . "Content-Disposition: form-data; name=\"tophone\"\r\n\r\n$phonenumber\r\n"
      . "--$boundary\r\n"
      . "Content-Disposition: form-data; name=\"uSession\"\r\n\r\n1\r\n"
      . "--$boundary\r\n"
      . "Content-Disposition: form-data; name=\"uReply\"\r\n\r\n\r\n"
      . "--$boundary\r\n"
      . "Content-Disposition: form-data; name=\"uLastId\"\r\n\r\n\r\n"
      . "--$boundary\r\n"
      . "Content-Disposition: form-data; name=\"uSend\"\r\n\r\n1\r\n"
      . "--$boundary\r\n"
      . "Content-Disposition: form-data; name=\"uNextId\"\r\n\r\n\r\n"
      . "--$boundary\r\n"
      . "Content-Disposition: form-data; name=\"uHistoryCounter\"\r\n\r\n1\r\n"
      . "--$boundary\r\n"
      . "Content-Disposition: form-data; name=\"count\"\r\n\r\n0\r\n"
      . "--$boundary\r\n"
      . "Content-Disposition: form-data; name=\"uSubmitCount\"\r\n\r\n0\r\n"
      . "--$boundary\r\n"
      . "Content-Disposition: form-data; name=\"checkNewMsg\"\r\n\r\n180000\r\n"
      . "--$boundary\r\n"
      . "Content-Disposition: form-data; name=\"charcount\"\r\n\r\n$charcount\r\n"
      . "--$boundary\r\n"
      . "Content-Disposition: form-data; name=\"msg\"\r\n\r\n$message\r\n"
      . "--$boundary\r\n"
      . "Content-Disposition: form-data; name=\"x\"\r\n\r\n30\r\n"
      . "--$boundary\r\n"
      . "Content-Disposition: form-data; name=\"y\"\r\n\r\n16\r\n"
      . "--$boundary--\r\n";

$remote = fsockopen('web.icq.com', 80, $errno, $errstr, 30);
if (!$remote) die("連線失敗: $errstr ($errno)");
fputs($remote, $post);
$htmlreply = '';
while (!feof($remote)) { $htmlreply .= fgets($remote, 128); }
fclose($remote);

// ---------- 步驟 3:判斷結果 ----------
if (stripos($htmlreply, 'Moved Permanently') !== false) {
    echo '簡訊發送成功!';
} else {
    echo '簡訊發送失敗!';
    // 可選:輸出 $htmlreply 除錯
}
?>

注意事項

  • ICQ Web SMS 服務已於 2010 年左右停止,此代碼僅供歷史參考或教學用途。
  • 現代簡訊發送建議使用 Twilio、Nexmo、Plivo 等專業 SMS API 服務,或各國電信商提供的 HTTP API。
  • 若需在現代 PHP 專案中實作,請改用 cURL 或 Guzzle 等 HTTP 客戶端庫,代碼更簡潔健壯。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved