php - WinHttpRequest POST

标签 php mysql vba winhttp

我正在尝试使用 VBA 中的 WinHttpRequest 从在线 mySQL 服务器读取数据。

Dim objHTTP As New WinHttp.WinHttpRequest
With objHTTP
    .Open "POST", "http://www.dname.com/ruski/php/getNewestPID.php", True
    .SetRequestHeader "Content-Type", "application/x-www-form-urlencoded; charset=""UTF-8"""
    .Send "PID=" & lngPID
    .WaitForResponse
    Debug.Print .ResponseText
End With
<?php
$PID = $_POST['PID'];

require_once ('config.php'); 

if(!$PID>0){
    die("##### Error: getNewestPID failed - PID='" . $PID . "'  #####");
}

$phrases = mysql_query("SELECT * FROM phrases WHERE (PID > '$PID')");

if(!$phrases){
    die("##### Error: getNewestPID SELECT failed - PID=" . $PID . " - " . mysql_error() . "  #####");
}

mysql_close($db);

echo "data=";
while($row = mysql_fetch_array($phrases)) {
    echo $row[0] . "|" . $row[1] . "|" . $row[2] . "|" . $row[3];
}
?>

一切正常,但返回西里尔文字,如下所示:

data=21361|105||||||||||||||||||||||||||||||||||||| на ÑкÑ?ип

我的数据库使用 UTF-8 Unicode。我知道西里尔字母可以在表格和表单中使用,但在 VB 编辑器中查看垃圾内容。

我尝试过这个:

Set FileStream = CreateObject("ADODB.Stream")
FileStream.Open
FileStream.Type = 1 'Binary
FileStream.Write objHTTP.ResponseBody
FileStream.Position = 0
FileStream.Type = 2 'adTypeText
FileStream.Charset = "Windows-1251"  ' https://msdn.microsoft.com/en-us/library/ms526296(v=exchg.10).aspx
strText = FileStream.ReadText
FileStream.Close

最终记录中出现垃圾:

最好的结果是:

mb_convert_encoding($row[3], "Windows-1251", "UTF-8")

Ìàëü÷èê èãðàåò íà ñêðèïêå â ñâîåé êîìíàòå。

最佳答案

这不是很优雅,但当我遇到类似问题时它对我有用。它只是一个手动从 1251 转换为 Unicode 的函数。这适用于俄语和乌克兰语,但根据您使用的语言,可能会有不同的字符,您需要将它们添加到 if 语句中。还可以考虑添加 else if ,如果找不到您要查找的字符,则会抛出问号。

Public Function AsciitoUni(base_cell As String) As String

AsciitoUni = ""

For i = 1 To Len(base_cell)

Dim s As String
Dim u As Integer
Dim a As Integer


s = Mid(base_cell, i, 1)
a = Asc(s)

If (a <= 127) Then
u = a
ElseIf (a >= 192) And (a <= 255) Then
u = a + 848
ElseIf (a = 184) Then
u = 1105
ElseIf (a = 186) Then
u = 1108
ElseIf (a = 179) Then
u = 1110
ElseIf (a = 191) Then
u = 1111
ElseIf (a = 168) Then
u = 1025
ElseIf (a = 170) Then
u = 1028
ElseIf (a = 178) Then
u = 1030
ElseIf (a = 175) Then
u = 1031
ElseIf (a = 185) Then
u = 8470
End If

AsciitoUni = AsciitoUni & ChrW(u)

Next

End Function

关于php - WinHttpRequest POST,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48191161/

相关文章:

mysql - 错误 "result consisted of more than one row"

r - 如何检查我们在excel中具有相同唯一ID的每个唯一值

VBA Excel UDF保留原始值

php - 如何在 php 中使用 post 方法更新 mySQL 数据库

MySQL 用户定义函数发送 windows 消息

php - PHP PDO 语句可以接受表名或列名作为参数吗?

mysql - SQL 添加行值并据此对结果进行排名

excel - 清除 Excel 中的多个单元格时出错

php - 如何使用PHP和mysql数据库在XCode中成功登录后重定向到新 View

php - 如何使用 php/javascript 从用户浏览器获取突出显示/选择数据?