tp/test_sms_gateway_tasklist.php
2026-04-01 10:12:37 +08:00

76 lines
2.2 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
declare(strict_types=1);
$envPath = __DIR__ . DIRECTORY_SEPARATOR . '.env';
$lines = @file($envPath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
if (!$lines) {
echo "failed to read env\n";
exit(1);
}
$map = [];
foreach ($lines as $line) {
$line = trim((string)$line);
if ($line === '' || strpos($line, '=') === false) continue;
[$k, $v] = explode('=', $line, 2);
$map[trim($k)] = trim($v);
}
$dsn = 'mysql:host=' . $map['DB_HOST'] . ';port=' . $map['DB_PORT'] . ';dbname=' . $map['DB_NAME'] . ';charset=' . $map['DB_CHARSET'];
$pdo = new PDO($dsn, $map['DB_USER'], $map['DB_PASS'], [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
$stmt = $pdo->prepare('SELECT label,value FROM ' . $map['DB_PREFIX'] . 'system_site_settings WHERE label IN (?, ?) ORDER BY id ASC');
$stmt->execute(['backendUrl', 'apiKey']);
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
$backendUrl = '';
$apiKey = '';
foreach ($rows as $r) {
if ($r['label'] === 'backendUrl') $backendUrl = (string)$r['value'];
if ($r['label'] === 'apiKey') $apiKey = (string)$r['value'];
}
if ($backendUrl === '' || $apiKey === '') {
echo "missing backendUrl/apiKey\n";
exit(1);
}
$url = rtrim($backendUrl, '/') . '/api/v1/business/outbound-tasks?limit=3';
$headers = [
'X-Api-Key: ' . $apiKey,
'Accept: application/json',
];
$headerStr = implode("\r\n", $headers);
$context = stream_context_create([
'http' => [
'method' => 'GET',
'header' => $headerStr,
'timeout' => 10,
// 关键:即便 HTTP code 非 2xx也让 file_get_contents 返回响应体
'ignore_errors' => true,
],
]);
$respBody = @file_get_contents($url, false, $context);
$status = 0;
if (!empty($http_response_header) && is_array($http_response_header)) {
foreach ($http_response_header as $line) {
if (preg_match('#^HTTP/\\S+\\s+(\\d+)#', $line, $m)) {
$status = (int)$m[1];
break;
}
}
}
echo "GET $url\n";
echo "HTTP status=$status\n";
if ($respBody === false) {
echo "body=false\n";
} else {
$preview = substr((string)$respBody, 0, 500);
echo "body_preview=" . str_replace(["\r", "\n"], ['\\r', '\\n'], $preview) . "\n";
}