从客户端发送到服务器,再发送到另一台服务器
有几件事我需要帮助理解。长话短说:客户有第 3 方应用程序来处理用户注册/邮件列表/礼品卡,我的工作是收集用户数据,将其格式化为特定的 JSON 对象并发送到他们的服务器。另一项要求是传递使用 base64 加密的用户名:密码标头。
现在,我有一个 PHP 脚本,它可以按照他们的规范正确构建 JSON 对象,但我需要将 POST 数据以及提到的 HTTP 用户身份验证标头发送到他们的服务器(外部 Web 服务器)。
那么呃,我该怎么办?我原本想将 PHP 标头发送到客户端,如下所示:
Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==
但这并不意味着发送到客户端。这意味着要转到外部服务器。我应该让客户端发送这个标头(使用javascript)吗? PHP 如何与另一台服务器通信?最后,我如何将 JSON 对象发送到该服务器,显然与身份验证标头在同一消息中?
There are a few things I need help understanding. Long story short: Client has 3rd party application to handle user registration/mailing list/gift cards, it is my job to collect user data, format it in specific JSON object and send to their server. One additional requirement is to pass a username:password header encrypted with base64.
Now, I have a PHP script which is correctly building the JSON object as per their specifications, but I am required to send the POST data to their server, an external web server, along with the mentioned HTTP user authentication header.
So uh, what do I do? I had originally thought to send a PHP header to the client, like so:
Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==
But that's not meant to go to the client. That is meant to go to an external server. Am I supposed to make the client send this header (using javascript)? How can PHP communicate with another server? And finally, how would I send the JSON object to that server, apparently in the same message as the authentication header?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为 cURL 就是你所需要的。您可以在这里阅读:
http://www.php.net/manual/en/book.curl.php
使用 cURL,您可以轻松发出 HTTP 请求,包括 Post、Cookie 和许多其他内容。 JSON 对象可能只是作为 Post 值之一发送,但这取决于他们为您提供的 API。
希望有帮助。
I think cURL is what you need. You can read about it here:
http://www.php.net/manual/en/book.curl.php
With cURL you can easily make HTTP requests including Post, Cookies and lots of other stuff. The JSON object would probably just be sent as one of the Post values, that depends on the API they've given you, though.
Hope it helps.