在 PHP 中通过 fsockopen() 传递资源
我有两台服务器。服务器 A 拥有所有数据库。服务器B如何查询服务器A的数据库并使用fsockopen()
接收结果? 目前,我只能通过套接字传递实际文本(即,如果服务器 A 的 php 代码打印出如 echo
中的内容)。如何在这个打开的套接字中传递查询结果(资源)?
I have two servers. Server A has all the databases. How can server B query server A's database and receive the results with fsockopen()
?
currently, I can only pass actual text through the socket (i.e. if server A's php code prints out something as in echo
). How can I pass through the query results (the resource) in this open socket?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果使用 fsockopen 是与服务器 B 的数据库通信的唯一方式,则可以序列化查询结果或将其编码为 JSON 格式并将其写入套接字。
是否无法仅从服务器A授予对mysql服务器的只读访问权限,以便您可以直接查询?否则,REST 服务将是 fsockopen 的一个很好的替代方案。
如前所述,您无法通过套接字传递资源,因为它在服务器 A 上没有任何意义。如果您尝试通过套接字传递它,它可能会返回一个字符串(即资源#1)
If using fsockopen is the only way you are able to communicate with Server B's database, serializing the results of the query or encoding them in JSON format and writing them to the socket are possibilities.
Is it not possible to grant read-only access to the mysql server from server A only so you can query directly? Otherwise a REST service would be a good alternative to fsockopen.
As stated, you cannot pass the resource over the socket as it wouldn't have any meaning on server A anyway. If you tried to pass it over the socket it would probably return a string (i.e. Resource #1)
您无法传递 PHP 资源类型*Docs* 通过 fsockopen,因为 fwrite/read 会将数据转换为(二进制)字符串。看:
将资源转换为其他内容(例如字符串)也是如此。
You can't pass the PHP resource type*Docs* via fsockopen, because fwrite/read will convert the data into a (binary) string. See:
Same is true for converting a resource into something else, e.g. a string.
你在干什么?只需允许远程 mysql 并让数据库服务器完成其工作即可。
What are you doing? Just allow remote mysql and let the database server do its job.