如何从 PHP 调用 RESTful WCF-Service


How to call RESTful WCF-Service from PHP

>我正在尝试向PHP中带有REST的自承载WCF服务发送请求。我想将对象作为 JSON 对象发送到 WCF 服务。我还没有让它运行。有没有人举个例子,如何从PHP中调用服务?

这是操作协定(该方法是 POST 方法):

[OperationContract]
    [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
    void Method1(AnObject object);

PHP 中最好的工作代码如下:

$url = "http://localhost:8000/webservice/Method1?object=$object"; 
    $url1 = parse_url($url);
// extract host and path:
$host = $url1['host'];
$path = $url1['path'];
$port = $url1['port'];
// open a socket connection on port 80 - timeout: 30 sec
$fp = fsockopen($host, $port, $errno, $errstr, 30);
if($fp)
{
    // send the request headers:
    fputs($fp, "POST $path HTTP/1.1'r'n");
    fputs($fp, "Host: $host'r'n");
    fputs($fp, "Content-type: application/json 'r'n");
    fputs($fp, "Content-length: ". strlen($object) ."'r'n");
    fputs($fp, "Connection: close'r'n'r'n");
    fputs($fp, $object);
//
//  $result = ''; 
//  while(!feof($fp)) {
//          // receive the results of the request
//          $result .= fgets($fp, 128);
//  }
}
else { 
    return array(
        'status' => 'err', 
        'error' => "$errstr ($errno)"
    );
}
// close the socket connection:
    fclose($fp);

但此代码不会发送对象。在调试模式下,对象为"空"。我只是看到,它进入了方法。

我找到了自己问题的解决方案:

$url = "http://localhost:1234/service/PostMethod"; 
$jsonObject = json_encode($transmitObject);
    $options = array(
    CURLOPT_HTTPHEADER => array(
        "Content-Type:application/json; charset=utf-8",
        "Content-Length:".strlen($jsonObject)));
    $defaults = array( 
        CURLOPT_POST => 1, 
        CURLOPT_HEADER => 0, 
        CURLOPT_URL => $url, 
        CURLOPT_FRESH_CONNECT => 1, 
        CURLOPT_RETURNTRANSFER => 1, 
        CURLOPT_FORBID_REUSE => 1, 
        CURLOPT_TIMEOUT => 4, 
        CURLOPT_POSTFIELDS => $jsonObject
    ); 
    $ch = curl_init(); 
    curl_setopt_array($ch, ($options + $defaults)); 
    curl_exec($ch);
    curl_close($ch); 

在 WCF Rest 服务上执行 POST 时,原始请求应如下所示:

POST http://localhost:8000/webservice/Method1 HTTP 1.1
Content-Type: application/json
Host: localhost
{"object":{"ObjectId":1,"ObjectValue":60}

假设您的AnObject如下所示:

[DataContract]
public class AnObject 
{
    [DataMember]
    public int ObjectId {get;set;}
    [DataMember]
    public int ObjectValue {get;set;}
} 

从您的 php 代码中,您尝试将对象作为查询字符串发送,这是行不通的。而是构建代码以将 json 字符串添加到 http 正文。

使用一些工具,如Fiddler或WireShark,您可以在其中拦截请求/响应并检查它们。您甚至可以使用它们通过生成原始请求来测试 WCF Rest 服务。

找到一些可能有帮助的链接:

  1. 创建一个 php 客户端来调用 REST 服务

  2. PHP REST API 调用

通过在$path旁边添加参数,我得到了"der_chirurg"解决方案,如下所示

源语言:

$url = "http://localhost:8000/webservice/Method1?object=$object"; 
fputs($fp, "POST $path HTTP/1.1'r'n");

改为:

fputs($fp, "POST $path**?object=$object** HTTP/1.1'r'n");

和而不是在$url

$url = "http://localhost:8000/webservice/Method1

最后:

 url = "http://localhost:8000/webservice/Method1";
        $url1 = parse_url($url);
    // extract host and path:
    $host = $url1['host'];
    $path = $url1['path'];
    $port = $url1['port'];
    // open a socket connection on port 80 - timeout: 30 sec
    $fp = fsockopen($host, $port, $errno, $errstr, 30);
    if($fp)
    {
    // send the request headers:
    fputs($fp, "POST $path?value=test HTTP/1.1'r'n");
    fputs($fp, "Host: $host'r'n");
    fputs($fp, "Content-type: application/json 'r'n");
    fputs($fp, "Content-length: ". strlen($param) ."'r'n");
    fputs($fp, "Connection: close'r'n'r'n");
 $jsonData = json_encode($object, JSON_PRETTY_PRINT);
 $options = array(
     'http'=>array(
            'method'        =>  "POST",
            'ignore_errors' =>  true,
            'content'       =>  $jsonData,
            'header'        =>  "Content-Type: application/json'r'n" .
                                "Content-length: ".strlen($jsonData)."'r'n".
                                "Expect: 100-continue'r'n" .
                                "Connection: close"
            )
        );
        $context = stream_context_create($options);
        $result = @file_get_contents($requestUrl, false, $context);

非常重要的是JSON格式。