2012-05-22 84 views
2

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

这是工作合同(该方法是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); 

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

回答

3

我发现我自己的问题的解决方案:

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

当您执行上一个WCF休息服务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

+0

这是一个很好的提示,但不是解决方案。我现在修好了。看到我以下的帖子。 –

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

改为:我通过添加参数旁边$路径如下

原来有工作 “der_chirurg” 解决方案

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

和 ,而不是在$网址

$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"); 
0
$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格式。