PHP code example of kylin987 / netsale-report-sdk
1. Go to this page and download the library: Download kylin987/netsale-report-sdk library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
kylin987 / netsale-report-sdk example snippets
use NetsaleReportSdk\Config;
use NetsaleReportSdk\Model\ReportTicket;
use NetsaleReportSdk\NetsaleReportClient;
// --- 配置 ---
$config = new Config([
'reportUrl' => 'https://your-host/report/report',
'serviceUrl' => 'https://your-host/service',
'channelCode' => 'your-channel-code',
'certId' => 'your-cert-id',
'certFile' => '/path/to/client_cert.pem',
'keyFile' => '/path/to/client_key.pem',
'trustFile' => '/path/to/rootcert.pem',
'vstkSigner' => new HttpVstkSigner('http://127.0.0.1:18080/sign'),
]);
$client = new NetsaleReportClient($config);
// --- 售票上报 ---
$ticket = new ReportTicket();
$ticket->numberByDay = 1;
$ticket->parentChannelCode = '13090401';
$ticket->childChannelCode = '00000000';
$ticket->ticketNo = '130904010Ba0102';
$ticket->cinemaCode = '13090401';
$ticket->screenCode = '0000000000000001';
$ticket->seatCode = '88888888010010011101';
$ticket->filmCode = '000000252022';
$ticket->sessionCode = 'SE00001234567890';
$ticket->sessionDatetime = '2026-03-31 21:20:00';
$ticket->ticketPrice = 56.00;
$ticket->screenServiceFee = 0.00;
$ticket->netServiceFee = 8.00;
$ticket->saleChannelCode = 'your-channel-code';
$ticket->operation = 1; // 1=售票, 2=退票
$ticket->operationDatetime = '2026-03-31 21:20:00';
$result = $client->reportTicket([$ticket]);
if ($result->isSuccess()) {
echo '上报成功, traceId: ' . $result->traceId . PHP_EOL;
} else {
echo '上报失败: ' . $result->code . ' ' . $result->status . PHP_EOL;
}
// --- 退票上报(operation 改为 2) ---
$ticket->operation = 2;
$ticket->operationDatetime = date('Y-m-d H:i:s');
$client->reportTicket([$ticket]);
// --- 数据比对文件下载 ---
$zipContent = $client->downloadReportRecord('2026-03-01', '2026-03-31');
file_put_contents('report_record.zip', $zipContent);
interface VstkSignerInterface
{
/**
* @param string $certId 证书标识(通常为网售商编码)
* @param string $plainText 签名原文(JSON 字符串)
* @return string V-STK 返回的 Base64 签名字符串
*/
public function p7AttachSign(string $certId, string $plainText): string;
}
use NetsaleReportSdk\Signer\VstkSignerInterface;
class HttpVstkSigner implements VstkSignerInterface
{
public function __construct(private string $url) {}
public function p7AttachSign(string $certId, string $plainText): string
{
$ch = curl_init($this->url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode([
'certId' => $certId,
'plainText' => $plainText,
], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
$response = curl_exec($ch);
if (curl_errno($ch)) {
throw new RuntimeException('V-STK signer error: ' . curl_error($ch));
}
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($httpCode >= 400) {
throw new RuntimeException("V-STK signer HTTP {$httpCode}: {$response}");
}
$decoded = json_decode((string) $response, true);
if (is_array($decoded) && ($decoded['ok'] ?? false) === true) {
return (string) ($decoded['signData'] ?? '');
}
return trim((string) $response);
}
}
bash
# dry-run(不实际请求)
php examples/smoke.php
# 真实上报
php examples/smoke.php --send-report --confirm-submit-test-data
# 下载比对文件(需 V-STK 签名桥)
php examples/smoke.php --download-record --vstk-signer-url=http://127.0.0.1:18080/sign