【问题标题】:How to call RESTful WCF-Service from PHP如何从 PHP 调用 RESTful WCF-Service
【发布时间】:2012-05-29 00:06:54
【问题描述】:

我正在尝试使用 PHP 中的 REST 向自托管 WCF 服务发送请求。 我想将对象作为 JSON 对象发送到 WCF 服务。 我还没有让它运行。 有没有人举例说明如何从 PHP 中调用服务?

这是 Operation 契约(方法是 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);

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

【问题讨论】:

    标签: php json wcf rest


    【解决方案1】:

    当您在 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 正文。

    使用FiddlerWireShark 等工具,您可以拦截请求/响应并检查它们。您甚至可以通过构建原始请求来使用它们来测试 WCF Rest 服务。

    找到一些可能有用的链接:

    1. Create a php Client to invoke a REST Service

    2. php rest api call

    【讨论】:

    • 这是一个很好的提示,但不是解决方案。我现在确实修好了。请参阅我的以下帖子。
    【解决方案2】:

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

    $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); 
    

    【讨论】:

      【解决方案3】:

      我通过在 $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");
      

      【讨论】:

        【解决方案4】:
         $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 格式非常重要。

        【讨论】:

          猜你喜欢
          • 2015-03-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-08-08
          • 2016-02-14
          • 2011-10-26
          • 1970-01-01
          • 2016-02-15
          相关资源
          最近更新 更多