PHP 网络服务 [英] PHP Web Service

查看:27
本文介绍了PHP 网络服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 PHP 和编码领域的新手,因为它们帮助我回答了我发布的许多问题.我有机会深入了解,但是看到我对 PHP 和 Web 服务一无所知,我正在寻求您的帮助.如果您能指出我正确的方向,我将不胜感激,因为我找不到任何示例或有关它的详细信息.

I am new to the world of PHP and coding as those have helped me answer a number of questions I have posted know. I have the opportunity to jump into the deep end however seeing I have no clue about PHP and web services, I am seeking your help. I would appreciate it if you could point me in the right direction as I have not been able to find any examples nor detailed information regarding it.

实际上,有一个 PHP 页面需要将某些值传递给 .NET Web 服务,即姓名、电子邮件、手机号码,并且在收到此类信息后,Web 服务会发送一条文本消息.这是我目前掌握的信息

Effectively there is a PHP page that needs to pass certain values to a .NET web service i.e. name, email, mobile number and upon the receipt of such information the web service sends out a text message. This is information I have so far

分配服务使用基本的 HTTP 绑定和 TransportWithMessageCredential 安全性.

Sample C# code using a reference generated with Visual Studio tooling
Allocation.AllocationServiceV3Client Client = new Allocation.AllocationServiceV3Client();
Allocation.AllocateResult Result;
string VoucherCode;
DateTime ExpiryDate;
string DisplayMessageText;
Client.ClientCredentials.UserName.UserName = ? ;
Client.ClientCredentials.UserName.Password = ? ;
 Result = Client.AllocateToConsumerMobileAndSendSMS(out VoucherCode,
                                                               out ExpiryDate,
                                                               out DisplayMessageText,
                                                               ClientID,
                                                               ClientReference,
                                                               CampaignID,
                                                               ActivityID,
                                                               MobileNumber);
if (Result != Allocation.AllocateResult.Success)
{
}

推荐答案

我对 PHP 和网络服务一无所知

I have no clue about PHP and web services

首先,将您的大脑从 C# 汤中取出,然后静置几个小时使其干燥.PHP 是一种用于简单事物的简单语言.如果您不熟悉它,您不想尝试让它做复杂的事情,而且您当然不想想尝试用 PHP 编写 C# 风格的代码.

First things first, pick your brain up out of the C# soup and sit it out to dry off for a few hours. PHP is a simple language for simple things. If you're new to it, you don't want to try and make it do complex things, and you certainly don't want to try writing C# style code in PHP.

让我们以网络服务"作为复杂"事物的示例.在某些圈子里,这意味着像 SOAP 这样设计得很糟糕的怪物.VS 使构建到现有类的 SOAP 绑定变得相对容易,因此这对您来说不是一个痛苦的解决方案.

Let's take "web services" as an example of a "complex" thing. In some circles, this means horribly designed monstrosities like SOAP. VS makes it relatively easy to build SOAP bindings to your existing classes, so it's not a painful solution for you.

SOAP 对于 PHP 来说是一个非常痛苦的解决方案.

SOAP is a very painful solution for PHP.

实际上有一个 PHP 页面需要将某些值传递给 .NET Web 服务

Effectively there is a PHP page that needs to pass certain values to a .NET web service

听起来您已经设置了现有的网络服务,对吗?

It sounds like you have an existing web service set up then, correct?

可以尝试使用 内置的 SOAP客户端,但它是一个不可调试且不可配置的二进制恐怖事件,如果它不能立即为您工作,它会让您想杀死某人.

You could try and use the built in SOAP client, but it's an undebuggable and unconfigurable binary blob of horrors that will make you want to kill someone if it doesn't work for you immediately.

假设双方都在您的控制之下,以下部分被错误地编写.由于情况并非如此,您可以忽略其余的答案.我放弃它是因为它可能对其他人有价值.

我建议采用不同的方法.在您的 .NET 应用程序中设置一个端点 (URL),它需要您指定为 POST 值的数据,并让 PHP POST 数据.这具有难以置信简单的明显优势.因为您是 PHP 新手,所以简单就是一大胜利.

I would advise a different approach. Set up an endpoint (URL) in your .NET application that expects the data you've specified as POST values, and have PHP POST the data. This has the distinct advantage of being incredibly simple. Because you're new to PHP, simple is a big win.

(换句话说,放弃网络服务"——或者如果其他东西已经使用了该服务,则制作一个并行副本.)

(In other words, ditch the "web service" -- or make a parallel copy if other things consume that service already.)

PHP 附带一个名为 PEAR 的服务,这是一个有用类的存储库.您可以使用 HTTP_Request2 快速构建 HTTP POST 请求.很有可能它已经安装了.如果不是,则可以使用 pear 命令在系统级别或在您的项目本地安装.

PHP comes with a service called PEAR, a repository of helpful classes. You can use HTTP_Request2 to quickly build the HTTP POST request. Chances are that it's already installed. If it isn't, it's easy to install either at the system level using the pear command, or locally in your project.

以下是直接从参考指南中复制的一些快速示例代码:

Here's some quick example code copied straight out of the reference guide:

$request = new HTTP_Request2('http://www.example.com/your/endpoint/url.foo');
$request->setMethod(HTTP_Request2::METHOD_POST)
    ->addPostParameter('name', '...')
    ->addPostParameter('email', '...')
    ->addPostParameter('mobile', '...');
$response = $request->send();

然后您可以阅读回复根据需要.

现在,这不是一个完美的解决方案.您的 C# 代码提到了用户名和密码,我想可以包含在 POST 数据中.您也可以改用 HTTP 身份验证(由 HTTP_Request2 支持).

Now, this isn't a perfect solution. Your C# code mentions a username and password, which I suppose could be included in the POST data. You could also use HTTP authentication (supported by HTTP_Request2) instead.

这篇关于PHP 网络服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆