It's Spann, not spam

Technical blog to provide content that developers find useful.

Building REST-based Services Using WCF - (Part 3)

Building REST-based Services Using WCF - (Part 3)

  • Comments 1

Overview:

This how-to post will introduce you to building REST-based services using WCF.  This series is split into three parts.  Each part is built on the previous part.  Here is a link back to Part 1 and Part 2 if you need a refresher.

Prerequisites:

WCF From PHP

In this post, we will create a PHP page that uses the JSON-based WCF Service we built in the last post to get data to build its page.

Go back to Visual Studio 2008Right-click on the project in the Solution Explorer and select Add New Item.

In the Add New Item dialog, select Text File as the template type, and use PHPUsingWCF.php as the Name.  Press the Add button.

Paste the code below into the PHP file.

<html>

<body>

<h1><?php

$request =

http://localhost/MovieWeb/DirectorJSONService.svc/GetAllMovies;

$response = file_get_contents($request);

$jsonobj = json_decode($response);

echo("<select>");

foreach($jsonobj->d as $value)

{

echo("<option>");

echo($value->DirectorsName);

echo("</option>");

}

echo("</select>");

?></h1>

</body>

</html>

Save the file.  Go to Internet Explorer and navigate to the URL (http://localhost/MovieWeb/PHPUsingWCF.php)

NOTE:  You can't right click and select View in Browser with php file in Visual Studio 2008.

Verify that the page has rendered with a single select (dropdown) with the name of all the directors.

You may note in the Web Development Helper that the PHP request to the Service doesn't appear.  This is because that request is happening on the server side (not the client).

Modify $request item in the PHP file so that the URL for the request goes to port 8080.

$request = http://localhost:8080/MovieWeb/DirectorJSONService.svc/GetAllMovies;

Save the file

Use Windows Explorer to go to the location you saved TcpTrace.exe file.

Double-click on TcpTrace.exe.  You should see a dialog box.

Note:  TcpTrace will act as a proxy so you can see the PHP request.

Set TcpTrace to listen on port 8080, Destination Server of localhost, and Destination Port of 80.

Press OK

Go back to Internet Explorer and refresh the PHP page.

Switch to the TcpTrace window.  You should see the request the PHP code is making to the JSON-encoded WCF Service.

Summary

In this post you used a WCF .NET 3.5 Web Programming Model based Service that returned JSON-encoded data to a PHP page.  This shows that WCF AJAX based services can be consumed by clients other than .NET or ASP.NET AJAX.

Page 1 of 1 (1 items)
Leave a Comment
  • Please add 6 and 8 and type the answer here:
  • Post