I m trying to send an request to an Self-Hosted WCF-Service with REST in PHP. I want to send the object to the WCF service as an JSON Object. I did not get it running yet. Has anyone an example how to call the service out of 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
");
fputs($fp, "Host: $host
");
fputs($fp, "Content-type: application/json
");
fputs($fp, "Content-length: ". strlen($object) ."
");
fputs($fp, "Connection: close
");
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 ” 。 我只是看到它进入了方法 。